2006-04-25 Hendrik Brandt <heb@gnome-de.org>
[beagle.git] / Filters / FilterMPlayerVideo.cs
blob9b26ead85ecdd4aedc4fa64a6952abed607361bb
1 //
2 // FilterMPlayerVideo.cs
3 //
4 // Copyright (C) 2006 Alexander Macdonald <alex@alexmac.cc>
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 using System;
28 using System.IO;
29 using System.Diagnostics;
31 using Beagle.Util;
32 using Beagle.Daemon;
34 namespace Beagle.Filters {
36 public class FilterMPlayerVideo : Beagle.Daemon.Filter {
38 private int width = -1;
39 private int height = -1;
40 private float aspect = 0.0f;
41 private float fps = 0.0f;
42 private int length_seconds = -1;
43 private int audio_channels = 0;
45 public FilterMPlayerVideo ()
47 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/dl"));
48 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/dv"));
49 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/fli"));
50 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/gl"));
51 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/mpeg"));
52 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/mp4"));
53 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/quicktime"));
54 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/mp4v-es"));
55 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/parityfec"));
56 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/pointer"));
57 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/vnd.fvt"));
58 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/vnd.motorola.video"));
59 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/vnd.motorola.videop"));
60 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/vnd.mpegurl"));
61 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/vnd.mts"));
62 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/vnd.nokia.interleaved-multimedia"));
63 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/vnd.vivo"));
64 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/x-la-asf"));
65 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/x-mng"));
66 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/x-ms-asf"));
67 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/x-ms-wm"));
68 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/x-ms-wmv"));
69 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/x-ms-wmx"));
70 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/x-ms-wvx"));
71 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/x-msvideo"));
72 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("video/x-sgi-movie"));
74 PreLoad = false;
77 private string AspectString(float aspect) {
78 // work out what the human form of the aspect ratio is (e.g 16x9)
79 float rounded_aspect = (float) Math.Round (aspect, 3);
81 // FIXME: comparing floats is a bit dodgy, but i round the input
82 // so it might be ok
83 if (rounded_aspect == 1.778f)
84 return "16x9";
85 else if (rounded_aspect == 1.6f)
86 return "16x10";
87 else if (rounded_aspect == 1.333f)
88 return "4x3";
89 else if (rounded_aspect == 1.25f)
90 return "5x4";
91 else if (rounded_aspect == 0.0f)
92 return "unknown";
93 else
94 return "other";
97 protected override void DoPullProperties ()
99 SafeProcess pc = new SafeProcess ();
100 pc.Arguments = new string [] { "mplayer", "-vo", "null", "-ao", "null", "-frames", "0", "-identify", FileInfo.FullName };
101 pc.RedirectStandardOutput = true;
102 pc.RedirectStandardError = true;
104 try {
105 pc.Start ();
106 } catch (SafeProcessException e) {
107 Log.Warn (e.Message);
108 Error ();
109 return;
112 StreamReader pout = new StreamReader (pc.StandardOutput);
113 string str;
114 string name = "";
116 while ((str = pout.ReadLine ()) != null) {
117 if (!str.StartsWith ("ID_"))
118 continue;
120 string[] tokens = str.Split ('=');
122 if (tokens.Length != 2)
123 continue;
125 if (str.StartsWith ("ID_CLIP_INFO_NAME"))
126 name = tokens [1].ToLower ();
127 else if (str.StartsWith ("ID_CLIP_INFO_VALUE")) {
128 switch (name) {
130 case "":
131 break;
133 case "name":
134 AddProperty (Beagle.Property.New ("dc:title", tokens [1]));
135 break;
137 case "language":
138 AddProperty (Beagle.Property.NewUnsearched ("dc:language", tokens [1]));
139 break;
141 case "copyright":
142 AddProperty (Beagle.Property.NewUnsearched ("dc:copyright", tokens [1]));
143 break;
145 case "comments":
146 AddProperty (Beagle.Property.New ("dc:description", tokens [1]));
147 break;
149 default:
150 AddProperty (Beagle.Property.NewUnsearched ("fixme:info:" + name, tokens [1]));
151 break;
153 } else {
154 switch (tokens [0]) {
156 case "ID_VIDEO_WIDTH":
157 width = Convert.ToInt32 (tokens [1]);
158 break;
160 case "ID_VIDEO_HEIGHT":
161 height = Convert.ToInt32 (tokens [1]);
162 break;
164 case "ID_VIDEO_ASPECT":
165 aspect = Convert.ToSingle (tokens [1]);
166 break;
168 case "ID_VIDEO_FPS":
169 fps = Convert.ToSingle (tokens [1]);
170 break;
172 case "ID_VIDEO_FORMAT":
173 AddProperty (Beagle.Property.NewKeyword ("fixme:video:codec", tokens [1]));
174 break;
176 case "ID_LENGTH":
177 length_seconds = (int) Convert.ToSingle (tokens [1]);
178 break;
180 case "ID_AUDIO_NCH":
181 audio_channels = Convert.ToInt32 (tokens [1]);
182 break;
184 case "ID_AUDIO_CODEC":
185 AddProperty (Beagle.Property.NewKeyword ("fixme:audio:codec", tokens [1]));
186 break;
188 case "ID_DEMUXER":
189 AddProperty (Beagle.Property.NewUnsearched ("fixme:video:container", tokens [1]));
190 break;
195 pout.Close ();
196 pc.Close ();
198 // If an aspect ratio wasn't set in the file then work out the
199 // pixel aspect ratio
200 if (aspect <= 0.0f) {
201 if (width > 0 && height > 0)
202 aspect = (float) width / (float) height;
205 if (aspect > 0.0f)
206 AddProperty (Beagle.Property.NewUnsearched ("fixme:video:aspect", aspect));
208 AddProperty (Beagle.Property.NewUnsearched ("fixme:video:aspect", AspectString (aspect)));
210 if (width > 0)
211 AddProperty (Beagle.Property.NewUnsearched ("fixme:video:width", width));
213 if (height > 0)
214 AddProperty (Beagle.Property.NewUnsearched ("fixme:video:height", height));
216 if (fps > 0.0f)
217 AddProperty (Beagle.Property.NewUnsearched ("fixme:video:fps", fps));
219 if (length_seconds > 0)
220 AddProperty (Beagle.Property.NewUnsearched ("dc:extent", length_seconds));
222 switch (audio_channels) {
224 case 0:
225 AddProperty (Beagle.Property.NewUnsearched ("fixme:audio:channel_setup", "none"));
226 break;
227 case 1:
228 AddProperty (Beagle.Property.NewUnsearched ("fixme:audio:channel_setup", "mono"));
229 break;
230 case 2:
231 AddProperty (Beagle.Property.NewUnsearched ("fixme:audio:channel_setup", "stereo"));
232 break;
233 case 5:
234 AddProperty (Beagle.Property.NewUnsearched ("fixme:audio:channel_setup", "4.1"));
235 break;
236 case 6:
237 AddProperty (Beagle.Property.NewUnsearched ("fixme:audio:channel_setup", "5.1"));
238 break;
239 case 7:
240 AddProperty (Beagle.Property.NewUnsearched ("fixme:audio:channel_setup", "6.1"));
241 break;
242 case 8:
243 AddProperty (Beagle.Property.NewUnsearched ("fixme:audio:channel_setup", "7.1"));
244 break;
247 AddProperty (Beagle.Property.NewUnsearched ("fixme:audio:channels", audio_channels));
249 Finished ();