4108deedaf184ba8e6c8b103b2b0aa8f058b6e5d
[platform/upstream/gstreamer.git] / tools / gst-launch-ext
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 = `echo -n ~`."/.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} = "colorspace ! xvideosink"; }
60   if (!defined $cfg{CVS_PATH})   { $cfg{CVS_PATH} =  `echo -n ~`."/gst/cvs"; }
61
62   if ($command =~ /(.+)\/gst-launch-ext$/)
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 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 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 ! ffmpegdecall ! $cfg{VIDEOSINK} } { demux. ! queue ! mad ! $cfg{AUDIOSINK} }",
107   "asf", "asfdemux name=demux ! { queue ! ffmpegdecall ! $cfg{VIDEOSINK} } { demux. ! queue ! mad ! $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   "mod", "modplug !  $cfg{AUDIOSINK}",
113   "mp2", "mad ! $cfg{AUDIOSINK}",
114   "mp3", "mad ! $cfg{AUDIOSINK}",
115   "mpeg", "mpegdemux name=demux ! { queue ! mpeg2dec ! $cfg{VIDEOSINK} } { demux. ! queue ! mad ! $cfg{AUDIOSINK} }",
116   "mpg", "mpegdemux name=demux ! { queue ! mpeg2dec ! $cfg{VIDEOSINK} } { demux. ! queue ! mad ! $cfg{AUDIOSINK} }",
117   "ogg", "vorbisfile ! $cfg{AUDIOSINK}",
118   "sid", "siddec ! $cfg{AUDIOSINK}",
119   "swf", "swfdec name=swfdec ! { queue ! colorspace ! $cfg{VIDEOSINK} }  { swfdec. ! queue ! $cfg{AUDIOSINK} }",
120   "vob", "mpegdemux name=demux ! { queue ! mpeg2dec ! $cfg{VIDEOSINK} }  { demux. ! queue ! a52dec ! $cfg{AUDIOSINK} }",
121   "wav", "wavparse ! $cfg{AUDIOSINK}",
122   "wm", "asfdemux name=demux ! { queue ! ffmpegdecall ! $cfg{VIDEOSINK} } { demux. ! queue ! mad ! $cfg{AUDIOSINK} }",
123   "wma", "asfdemux name=demux ! { queue ! ffmpegdecall ! $cfg{VIDEOSINK} } { demux. ! queue ! mad ! $cfg{AUDIOSINK} }",
124   "wmv", "asfdemux name=demux ! { queue ! ffmpegdecall ! $cfg{VIDEOSINK} } { demux. ! queue ! mad ! $cfg{AUDIOSINK} }",
125 );
126
127 if ($cfg{VISUALIZER}) {
128   %pipes = (
129     %pipes,
130     "vis.mp3", "mad ! tee name=tee silent=true ! queue leaky=1 ! { $cfg{VISUALIZER} ! colorspace ! $cfg{VIDEOSINK} } tee. ! $cfg{AUDIOSINK}",
131     "vis.ogg", "vorbisdec ! tee name=tee silent=true ! queue leaky=1 ! { $cfg{VISUALIZER} ! colorspace ! $cfg{VIDEOSINK} } tee. ! $cfg{AUDIOSINK}",
132     "vis.wav", "wavparse ! tee name=tee silent=true ! queue leaky=1 ! { $cfg{VISUALIZER} ! colorspace ! $cfg{VIDEOSINK} } tee. ! $cfg{AUDIOSINK}",
133   );
134 }              
135
136 if ($#ARGV == -1) {
137     print STDERR "Usage: gst-launch-ext filename[s]\n";
138     exit 1;
139 }
140
141 my $file;
142 while ($file = shift @ARGV) {
143     my $ext = extension ($file);
144     if (!$ext) { 
145       print "file $file doesn't have an extension !\n";
146       exit;
147     }
148     if ($ext eq 'm3u')
149     {
150         open (PLAYLIST, '<', $file);
151         my $file2;
152         while ($file2 = <PLAYLIST>) {
153             chomp $file2;
154             my $ext2 = extension ($file2);
155             playfile($file2, $ext2);
156         }
157         close PLAYLIST;
158     } else {
159         playfile($file, $ext);
160     }
161 }