1
// ------------------------------------------------------------------
2 // Copyright (C) 2011-2016 Maruko Toolbox Project
4 // Authors: LunarShaddow <aflyhorse@hotmail.com>
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
15 // express or implied.
16 // See the License for the specific language governing permissions
17 // and limitations under the License.
18 // -------------------------------------------------------------------
22 using System
.Collections
.Generic
;
23 using System
.ComponentModel
;
25 using System
.Windows
.Forms
;
27 using System
.Text
.RegularExpressions
;
34 /// A wrapper to make Invoke more easy by using Method Extension.
38 public static void InvokeIfRequired(this ISynchronizeInvoke control
, MethodInvoker action
)
40 if (control
.InvokeRequired
)
41 control
.Invoke(action
, null);
48 public static class FormatExtractor
51 /// Regex pattern. ffmpeg -i sample output:
52 /// <para>Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 320 kb/s (default)</para>
53 /// <para>Stream #0:1: Audio: mp3, 44100 Hz, stereo, s16p, 320 kb/s</para>
54 /// <para>Stream #0:2: Video: h264 (High), yuv420p, 1280x960, SAR 1:1 DAR 4:3, 24 fps, 24 tbr, 1k tbn, 48 tbc (default)</para>
55 /// <para>Stream #0:3: Subtitle: ass (default)</para>
57 private static readonly Regex ffmpegReg
58 = new Regex(@"Stream #0:\S+: (?<type>.+): (?<metadata>.+)");
60 public enum StreamType
62 Video
, Audio
, Subtitle
, Unknown
65 public class StreamProperty
67 public StreamType Type
;
69 public string RawFormat
;
72 /// Helper method to turn string into StreamType Enum
74 /// <param name="strType">StreamType in string</param>
75 /// <returns>StreamType Enum</returns>
76 public static StreamType
Str2Type(string strType
)
80 return (StreamType
)Enum
.Parse(typeof(StreamType
), strType
);
84 return StreamType
.Unknown
;
89 /// deliminators of raw format string
91 private static readonly char[] delim
= { ' ', ',' }
;
93 public StreamProperty(string strType
, string rawFormat
)
94 : this(Str2Type(strType
), rawFormat
) { }
96 public StreamProperty(StreamType type
, string rawFormat
)
99 RawFormat
= rawFormat
;
100 Format
= rawFormat
.Split(delim
)[0];
105 /// Extract stream infomation from a media file
107 /// <param name="workPath">work path which contains ffmpeg</param>
108 /// <param name="filename">target media file</param>
109 /// <returns>a list of StreamProperty containing stream type and format.</returns>
110 public static List
<StreamProperty
> Extract(string workPath
, string filename
)
112 var output
= GetFFmpegOutput(workPath
, filename
);
113 var list
= new List
<StreamProperty
>(5);
114 var match
= ffmpegReg
.Match(output
);
115 while (match
.Success
)
117 list
.Add(new StreamProperty(
118 match
.Groups
["type"].Value
, match
.Groups
["metadata"].Value
));
119 match
= match
.NextMatch();
125 /// ffmpeg output wrapper
127 /// <param name="workPath">work path which contains ffmpeg</param>
128 /// <param name="filename">target media file</param>
129 /// <returns>ffmpeg output info</returns>
130 public static string GetFFmpegOutput(string workPath
, string filename
)
132 var processInfo
= new System
.Diagnostics
.ProcessStartInfo(
133 System
.IO
.Path
.Combine(workPath
, "ffmpeg.exe"), "-i " + Util
.FormatPath(filename
));
134 processInfo
.WorkingDirectory
= System
.IO
.Directory
.GetCurrentDirectory();
135 processInfo
.CreateNoWindow
= true;
136 processInfo
.UseShellExecute
= false;
137 processInfo
.RedirectStandardError
= true;
138 var proc
= System
.Diagnostics
.Process
.Start(processInfo
);
139 string output
= proc
.StandardError
.ReadToEnd();