Rename MediaInfo() to GetMediaInfoString() to make it more sense
[marukotoolbox.git] / mp4box / Supplements.cs
blob8f4f4842f02cdefab8ada599c617f871dd8da67d
1 // ------------------------------------------------------------------
2 // Copyright (C) 2011-2016 Maruko Toolbox Project
3 //
4 // Authors: LunarShaddow <aflyhorse@hotmail.com>
5 //
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
9 //
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 // -------------------------------------------------------------------
21 using System;
22 using System.Collections.Generic;
23 using System.ComponentModel;
24 using System.Drawing;
25 using System.Windows.Forms;
26 using System.Text;
27 using System.Text.RegularExpressions;
29 namespace mp4box
31 namespace Extension
33 /// <summary>
34 /// A wrapper to make Invoke more easy by using Method Extension.
35 /// </summary>
36 static class Invoker
38 public static void InvokeIfRequired(this ISynchronizeInvoke control, MethodInvoker action)
40 if (control.InvokeRequired)
41 control.Invoke(action, null);
42 else
43 action();
48 public static class FormatExtractor
50 /// <summary>
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>
56 /// </summary>
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;
68 public string Format;
69 public string RawFormat;
71 /// <summary>
72 /// Helper method to turn string into StreamType Enum
73 /// </summary>
74 /// <param name="strType">StreamType in string</param>
75 /// <returns>StreamType Enum</returns>
76 public static StreamType Str2Type(string strType)
78 try
80 return (StreamType)Enum.Parse(typeof(StreamType), strType);
82 catch
84 return StreamType.Unknown;
88 /// <summary>
89 /// deliminators of raw format string
90 /// </summary>
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)
98 Type = type;
99 RawFormat = rawFormat;
100 Format = rawFormat.Split(delim)[0];
104 /// <summary>
105 /// Extract stream infomation from a media file
106 /// </summary>
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();
121 return list;
124 /// <summary>
125 /// ffmpeg output wrapper
126 /// </summary>
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();
140 proc.WaitForExit();
141 return output;