2 // FilterMPlayerVideo.cs
4 // Copyright (C) 2006 Alexander Macdonald <alex@alexmac.cc>
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.
29 using System
.Diagnostics
;
30 using System
.Globalization
;
35 namespace Beagle
.Filters
{
37 public class FilterMPlayerVideo
: Beagle
.Daemon
.Filter
{
39 private int width
= -1;
40 private int height
= -1;
41 private float aspect
= 0.0f
;
42 private float fps
= 0.0f
;
43 private int length_seconds
= -1;
44 private int audio_channels
= 0;
46 private static string[] mime_types
= {
47 "application/x-matroska",
59 "video/vnd.motorola.video",
60 "video/vnd.motorola.videop",
63 "video/vnd.nokia.interleaved-multimedia",
76 public FilterMPlayerVideo ()
78 foreach (string s
in mime_types
) {
79 FilterFlavor flavor
= FilterFlavor
.NewFromMimeType (s
);
80 flavor
.Priority
= -1; // Prefer Totem over this one
82 AddSupportedFlavor (flavor
);
88 private string AspectString(float aspect
) {
89 // work out what the human form of the aspect ratio is (e.g 16x9)
90 float rounded_aspect
= (float) Math
.Round (aspect
, 3);
92 // FIXME: comparing floats is a bit dodgy, but i round the input
94 if (rounded_aspect
== 1.778f
)
96 else if (rounded_aspect
== 1.6f
)
98 else if (rounded_aspect
== 1.333f
)
100 else if (rounded_aspect
== 1.25f
)
102 else if (rounded_aspect
== 0.0f
)
108 protected override void DoPullProperties ()
110 SafeProcess pc
= new SafeProcess ();
111 pc
.Arguments
= new string [] { "mplayer", "-vo", "null", "-ao", "null", "-frames", "0", "-identify", FileInfo.FullName }
;
112 pc
.RedirectStandardOutput
= true;
113 pc
.RedirectStandardError
= true;
117 } catch (SafeProcessException e
) {
118 Log
.Warn (e
.Message
);
123 StreamReader pout
= new StreamReader (pc
.StandardOutput
);
127 while ((str
= pout
.ReadLine ()) != null) {
128 if (!str
.StartsWith ("ID_"))
131 string[] tokens
= str
.Split ('=');
133 if (tokens
.Length
!= 2)
136 if (str
.StartsWith ("ID_CLIP_INFO_NAME"))
137 name
= tokens
[1].ToLower ();
138 else if (str
.StartsWith ("ID_CLIP_INFO_VALUE")) {
145 AddProperty (Beagle
.Property
.New ("dc:title", tokens
[1]));
149 AddProperty (Beagle
.Property
.NewUnsearched ("dc:language", tokens
[1]));
153 AddProperty (Beagle
.Property
.NewUnsearched ("dc:copyright", tokens
[1]));
157 AddProperty (Beagle
.Property
.New ("dc:description", tokens
[1]));
161 AddProperty (Beagle
.Property
.NewUnsearched ("fixme:info:" + name
, tokens
[1]));
165 switch (tokens
[0]) {
167 case "ID_VIDEO_WIDTH":
168 width
= Convert
.ToInt32 (tokens
[1]);
171 case "ID_VIDEO_HEIGHT":
172 height
= Convert
.ToInt32 (tokens
[1]);
175 case "ID_VIDEO_ASPECT":
176 aspect
= Convert
.ToSingle (tokens
[1], CultureInfo
.InvariantCulture
);
180 fps
= Convert
.ToSingle (tokens
[1], CultureInfo
.InvariantCulture
);
183 case "ID_VIDEO_FORMAT":
184 AddProperty (Beagle
.Property
.NewKeyword ("fixme:video:codec", tokens
[1]));
188 length_seconds
= (int) Convert
.ToSingle (tokens
[1], CultureInfo
.InvariantCulture
);
192 audio_channels
= Convert
.ToInt32 (tokens
[1]);
195 case "ID_AUDIO_CODEC":
196 AddProperty (Beagle
.Property
.NewKeyword ("fixme:audio:codec", tokens
[1]));
200 AddProperty (Beagle
.Property
.NewUnsearched ("fixme:video:container", tokens
[1]));
209 // If an aspect ratio wasn't set in the file then work out the
210 // pixel aspect ratio
211 if (aspect
<= 0.0f
) {
212 if (width
> 0 && height
> 0)
213 aspect
= (float) width
/ (float) height
;
217 AddProperty (Beagle
.Property
.NewUnsearched ("fixme:video:aspect", aspect
));
219 AddProperty (Beagle
.Property
.NewUnsearched ("fixme:video:aspect", AspectString (aspect
)));
222 AddProperty (Beagle
.Property
.NewUnsearched ("fixme:video:width", width
));
225 AddProperty (Beagle
.Property
.NewUnsearched ("fixme:video:height", height
));
228 AddProperty (Beagle
.Property
.NewUnsearched ("fixme:video:fps", fps
));
230 if (length_seconds
> 0)
231 AddProperty (Beagle
.Property
.NewUnsearched ("dc:extent", length_seconds
));
233 switch (audio_channels
) {
236 AddProperty (Beagle
.Property
.NewUnsearched ("fixme:audio:channel_setup", "none"));
239 AddProperty (Beagle
.Property
.NewUnsearched ("fixme:audio:channel_setup", "mono"));
242 AddProperty (Beagle
.Property
.NewUnsearched ("fixme:audio:channel_setup", "stereo"));
245 AddProperty (Beagle
.Property
.NewUnsearched ("fixme:audio:channel_setup", "4.1"));
248 AddProperty (Beagle
.Property
.NewUnsearched ("fixme:audio:channel_setup", "5.1"));
251 AddProperty (Beagle
.Property
.NewUnsearched ("fixme:audio:channel_setup", "6.1"));
254 AddProperty (Beagle
.Property
.NewUnsearched ("fixme:audio:channel_setup", "7.1"));
258 AddProperty (Beagle
.Property
.NewUnsearched ("fixme:audio:channels", audio_channels
));