[C#] FFMPEG.exe 을 이용해서 동영상 프레임 추출하기
Application Tech/C#,VB.NET2011. 2. 7. 09:56
1: /// <summary>
2: /// FFMPEG을 이용해서 프레임을 추출함
3: /// FFMPEGManager Class 1.0
4: /// Program by kunmin00
5: /// </summary>
6: public class FFMpegManager
7: {
8: const int IMG_COUNT = 60;
9:
10: /// <summary>
11: /// FFMPEG 을 이용해서 프레임 추출하기 kunmin00
12: /// </summary>
13: /// <param name="mediaFileInfo"></param>
14: /// <returns></returns>
15: public static Boolean GrapImageFromMediafile(MediaFile mediaFileInfo)
16: {
17:
18: TimeSpan duration = mediaFileInfo.DurationTime;
19:
20: LogHelper.LogMessage("[FFMPEG 이미지 추출 시작] " + mediaFileInfo.FilePath);
21:
22: Process myProcess = new Process();
23: FileInfo mediafileinfo = new FileInfo(mediaFileInfo.FilePath);
24: string mediafilename = mediafileinfo.Name.Replace(mediafileinfo.Extension, "");
25:
26: string LogPath = Application.StartupPath +"\\Temp";
27:
28: GlobalFunction.Current_Mainform.ChangeProgressBar(0, IMG_COUNT);
29: //디렉토리 체크
30: if (!Directory.Exists(LogPath))
31: {
32: LogHelper.LogMessage("[템프 폴더 생성]");
33: Directory.CreateDirectory(LogPath);
34: }
35:
36: try
37: {
38: for (int i = 0; i < IMG_COUNT; i++)
39: {
40: myProcess.StartInfo.UseShellExecute = false;
41: // You can start any process, HelloWorld is a do-nothing example.
42: myProcess.StartInfo.FileName = "FFMPEG\\ffmpeg.exe";
43: TimeSpan time = TimeSpan.FromSeconds((duration.TotalSeconds / IMG_COUNT) * i + 1);
44: myProcess.StartInfo.Arguments = "-ss " + GetTimeStringFromTimeSpan(time) + " -vframes 1 -i \"" + mediaFileInfo.FilePath + "\" -y -f image2 " + ".\\Temp\\" + mediafilename + "_" + time.TotalSeconds.ToString() + ".png" + " -threads 4 -an";
45: myProcess.StartInfo.CreateNoWindow = true;
46: myProcess.StartInfo.RedirectStandardOutput = false;
47: myProcess.Start();
48:
49: GlobalFunction.Current_Mainform.ChangeProgressBar((i+1) * 100 / IMG_COUNT, IMG_COUNT);
50: LogHelper.LogMessage("[" + (i+1) + "번째 이미지 추출] " + mediafilename + "_" + time.TotalSeconds.ToString() + ".png");
51:
52: myProcess.WaitForExit();
53:
54: Thread.Sleep(1);
55:
56: }
57:
58: LogHelper.LogMessage("[FFMPEG 이미지 추출 종료] " + mediaFileInfo.FilePath);
59:
60: myProcess.Close();
61: myProcess = null;
62: }
63: catch (Exception ex)
64: {
65: ExceptionHelper.ShowException(ex);
66: return false;
67: }
68:
69: return true;
70: }
71:
72: public static String GetTimeStringFromTimeSpan(TimeSpan duration)
73: {
74: return duration.Hours.ToString("00") + ":" + duration.Minutes.ToString("00") + ":" + duration.Seconds.ToString("00");
75: }
76:
77: }
'Application Tech > C#,VB.NET' 카테고리의 다른 글
MS의 Project Oxford (0) | 2015.05.07 |
---|