2004-09-14 Brian Cameron <brian.cameron@sun.com
[platform/upstream/gstreamer.git] / tools / gst-launch-ext-m.m
1 #!/usr/bin/perl -w
2 use strict;
3
4 # launch a gst-launch pipeline for the supplied media file
5 # use the extension to determine the gst-launch pipeline
6 # make use of default output sinks
7
8 my (%pipes, %cfg);
9
10 sub extension
11 {
12   my $path = shift;
13   my $ext;
14
15   # get only the bit after the last period.  We don't deal with
16   # .tar.gz extensions do we ?
17   if ($path =~ /\./)
18   {
19     $ext = $path;
20     $ext =~ s/^.*\.//;
21   }
22   else { $ext = ""; }
23
24   return $ext;
25 }
26
27 sub read_config
28 {
29   my $command = shift;
30
31   my $config_file = $ENV{HOME}."/.gst";
32   if (-e $config_file)
33   {
34     open CONFIG, $config_file;
35     while (<CONFIG>)
36     {
37       chomp;
38       s/#.*//;
39       s/\s+$//;
40       next unless length;
41       my ($var, $value) = split (/\s*=\s*/, $_, 2);
42       $cfg{$var} = $value;
43     }
44     if (!($cfg{AUDIOSINK}))
45     {
46       print "Please add an AUDIOSINK to $config_file !\n";
47     }
48     if (!($cfg{VIDEOSINK}))
49     {
50       print "Please add a VIDEOSINK to $config_file !\n";
51     }
52   }
53   else
54   {
55     print "No configuration file $config_file found.  You might want to create one.\n";
56     print "This is not an error, just a friendly reminder... Check the man page.\n\n";
57   }
58   if (!defined $cfg{AUDIOSINK})  { $cfg{AUDIOSINK} = "osssink"; }
59   if (!defined $cfg{VIDEOSINK})  { $cfg{VIDEOSINK} = "ffmpegcolorspace ! xvimagesink"; }
60   if (!defined $cfg{CVS_PATH})   { $cfg{CVS_PATH} =  $ENV{HOME}."/gst/cvs"; }
61
62   if ($command =~ /(.+)\/gst-launch-ext-@GST_MAJORMINOR@$/)
63   { $cfg{COMMAND_PATH} = "$1"; }
64   else
65   { $cfg{COMMAND_PATH} = ""; }
66 }
67
68 sub playfile($$)
69 {
70     my ($file, $ext) = @_;
71     my $command;
72     my $pipe;
73     my $path = "\$PATH:".$cfg{CVS_PATH}."/gstreamer/tools";
74
75     if ($cfg{COMMAND_PATH} ne "") {
76       $path = $cfg{COMMAND_PATH}.":$path";
77     }
78
79     $ext = lc $ext;
80
81     if ($cfg{VISUALIZER} && ($pipe = $pipes{"vis." . $ext}))
82     {
83         $command = "gst-launch-@GST_MAJORMINOR@ filesrc location=\"$file\" ! $pipe";
84         print "Running command-line\n$command\n\n";
85         system ("PATH=$path $command");
86     }
87     elsif ($pipe = $pipes{$ext})
88     {
89         $command = "gst-launch-@GST_MAJORMINOR@ filesrc location=\"$file\" ! $pipe";
90         print "Running command-line\n$command\n\n";
91         system ("PATH=$path $command");
92     }
93     else
94     {
95         print "No suitable pipe found for extension $ext.\n";
96     }
97 }
98
99 ### main
100
101 read_config ($0);
102
103 %pipes = ( 
104   "ac3", "a52dec ! $cfg{AUDIOSINK}",
105   "au", "auparse ! $cfg{AUDIOSINK}",
106   "avi", "avidemux name=demux ! { queue ! spider ! $cfg{VIDEOSINK} } { demux. ! queue ! spider ! $cfg{AUDIOSINK} }",
107   "asf", "asfdemux name=demux ! { queue ! spider ! $cfg{VIDEOSINK} } { demux. ! queue ! spider ! $cfg{AUDIOSINK} }",
108   "flac", "flacdec ! $cfg{AUDIOSINK}",
109   "fli", "flxdec ! colorspace ! $cfg{VIDEOSINK}",
110   "m1v", "mpegdemux ! { queue ! mpeg2dec ! $cfg{VIDEOSINK} }",
111   "m2v", "mpegdemux ! { queue ! mpeg2dec ! $cfg{VIDEOSINK} }",
112   "m4a", "qtdemux .audio_00 ! { queue ! faad ! $cfg{AUDIOSINK} }",
113   "mod", "modplug !  $cfg{AUDIOSINK}",
114   "mp2", "mad ! $cfg{AUDIOSINK}",
115   "mp3", "mad ! $cfg{AUDIOSINK}",
116   "mpeg", "mpegdemux name=demux ! { queue ! mpeg2dec ! $cfg{VIDEOSINK} } { demux. ! queue ! mad ! $cfg{AUDIOSINK} }",
117   "mpg", "mpegdemux name=demux ! { queue ! mpeg2dec ! $cfg{VIDEOSINK} } { demux. ! queue ! mad ! $cfg{AUDIOSINK} }",
118   "ogg", "oggdemux ! vorbisdec ! audioconvert ! $cfg{AUDIOSINK}",
119   "sid", "siddec ! $cfg{AUDIOSINK}",
120   "swf", "swfdec name=swfdec ! { queue ! colorspace ! $cfg{VIDEOSINK} }  { swfdec. ! queue ! $cfg{AUDIOSINK} }",
121   "vob", "mpegdemux name=demux ! { queue ! mpeg2dec ! $cfg{VIDEOSINK} }  { demux. ! queue ! a52dec ! $cfg{AUDIOSINK} }",
122   "wav", "wavparse ! $cfg{AUDIOSINK}",
123   "wm", "asfdemux name=demux ! { queue ! spider ! $cfg{VIDEOSINK} } { demux. ! queue ! spider ! $cfg{AUDIOSINK} }",
124 ### a wma file can use wmav1 or wmav2 codec so we must use spider to decode it  
125   "wma", "asfdemux name=demux ! spider ! $cfg{AUDIOSINK}",
126   "wmv", "asfdemux name=demux ! { queue ! spider ! $cfg{VIDEOSINK} } { demux. ! queue ! spider ! $cfg{AUDIOSINK} }",
127   "mkv", "matroskademux name=demux ! { queue ! spider ! $cfg{VIDEOSINK} } { demux. ! queue ! spider ! $cfg{AUDIOSINK} }",
128   "mka", "matroskademux ! spider ! $cfg{AUDIOSINK}",
129   "mov", "qtdemux name=demux { .video_00 ! queue ! spider ! $cfg{VIDEOSINK} } { demux. !  queue ! spider ! $cfg{AUDIOSINK} }",
130 );
131
132 if ($cfg{VISUALIZER}) {
133   %pipes = (
134     %pipes,
135     "vis.mp3", "mad ! tee name=tee silent=true ! queue leaky=1 ! { $cfg{VISUALIZER} ! colorspace ! $cfg{VIDEOSINK} } tee. ! $cfg{AUDIOSINK}",
136     "vis.ogg", "vorbisdec ! tee name=tee silent=true ! queue leaky=1 ! { $cfg{VISUALIZER} ! colorspace ! $cfg{VIDEOSINK} } tee. ! $cfg{AUDIOSINK}",
137     "vis.wav", "wavparse ! tee name=tee silent=true ! queue leaky=1 ! { $cfg{VISUALIZER} ! colorspace ! $cfg{VIDEOSINK} } tee. ! $cfg{AUDIOSINK}",
138   );
139 }              
140
141 if ($#ARGV == -1) {
142     print STDERR "Usage: gst-launch-ext-@GST_MAJORMINOR@ filename[s]\n";
143     exit 1;
144 }
145
146 my $file;
147 while ($file = shift @ARGV) {
148     my $ext = extension ($file);
149     if (!$ext) { 
150       print "file $file doesn't have an extension !\n";
151       exit;
152     }
153     if ($ext eq 'm3u')
154     {
155         open (PLAYLIST, '<', $file);
156         my $file2;
157         while ($file2 = <PLAYLIST>) {
158             chomp $file2;
159             my $ext2 = extension ($file2);
160             playfile($file2, $ext2);
161         }
162         close PLAYLIST;
163     } else {
164         playfile($file, $ext);
165     }
166 }