Skip to content

Instantly share code, notes, and snippets.

@dck-jp
Created March 26, 2013 16:41
Show Gist options
  • Select an option

  • Save dck-jp/5246934 to your computer and use it in GitHub Desktop.

Select an option

Save dck-jp/5246934 to your computer and use it in GitHub Desktop.
Exiv2を用いてExifを出力するテスト
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string checkPath = @"F:\Pictures\";
string outputFileName = @"C:\Users\Daisuke\Desktop\output.csv";
string exiv2ExePath = @"C:\Users\Daisuke\Desktop\exiv2-0.23-win\exiv2.exe";
//1.指定したディレクトリ下のjpgを拾う
foreach (string file in Directory.EnumerateFiles(checkPath, "*.jpg", SearchOption.AllDirectories))
{
if (file.Contains(".Picasa3Temp")) { continue; }
//2.各jpgに対して
//2-1.exifデータを取得
//標準出力を横取り
string exiv2Output = StartExiv2(exiv2ExePath, file);
//各行を:でsplit後、Trim
var sb = new StringBuilder();
new List<string>(exiv2Output.Replace("\r","").Split('\n')).ForEach( data =>
{
if (data.Contains("File name") || data.Contains("Camera model")
|| data.Contains("Image timestamp") || data.Contains("Focal length"))
{
sb.Append(data.Split(new char[]{':'}, 2)[1].Trim()).Append("\t");
}
});
sb.AppendLine();
//2-2.tsv形式でファイルに追記
File.AppendAllText(outputFileName, sb.ToString());
}
}
private static string StartExiv2(string exiv2ExePath, string targetFilePath)
{
Process process = new Process();
process.StartInfo.FileName = exiv2ExePath;
process.StartInfo.Arguments = "\"" + targetFilePath + "\"";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = false;
process.Start();
process.WaitForExit();
string output = process.StandardOutput.ReadToEnd();
process.Close();
return output;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment