updated on Wed Jan 25 16:08:47 UTC 2012
[aur-mirror.git] / vdr-mplayer / mplayer.sh
blob806c335b82a14161e6dbaa7c3c22dbabc72e65ce
1 #!/bin/bash
3 # $Id: mplayer.sh,v 1.41.2.7 2007/02/11 15:33:29 juri Exp $
6 unset LANG
7 declare VERSION="0.8.7"
9 # History (as always in the source code :-)
10 # 0.8.7 - added 544x480 as valid NTSC resolution (thanks to C.Y.M)
11 # - removed 688x576 from the valid PAL resolutions;
12 # - calculate USE_SPEED with 30 fps instead of 29.97
13 # - use '-vf' and '-af' instead of '-vop' and '-aop', as the
14 # latter are deprecated
16 # 0.8.6 - fixed a copy'n'paste bug regarding the subtitle background
17 # alpha color (thanks to Christian Haider)
18 # - changed the fps values from rational values to integer ones
19 # - fixed a typo setting DETC (thanks to Soeren Sonnenburg)
20 # - disabled the use of -forceidx for RealVideos, because since
21 # mplayer-1.0pre5 it isn't necessary anymore
23 # 0.8.5 - changed the check for the existence of the video file to check also
24 # that it can actually be read
25 # - added a new PAL height: 544
26 # - [experimental] use detelicining (detc) filter to play NTSC as PAL;
27 # thanks to Martin Hoffmann for this one
28 # - [experimental] make the detelecinig filter configurable via conf-file
29 # - changed the warning messages about wrong DVD options to be displayed
30 # only when DEBUG is set to true
31 # - added a comment to mplayer.sh.conf that the DVD dummy files MUST be
32 # named DVD/VCD in capital letters
33 # - removed '-afm 9' from AO defaults, because audio filters by numbers are
34 # removed from MPlayer (9 meant hwac, it is handeled differently anyway)
35 # - changed the the use of the -speed parameter: the man pages says one
36 # should use a value between 0.01 and 100, not something like 25:24
38 # 0.8.4 - added some more debugging info
39 # - changed the MPlayer option that is used to play DVDs and VCDs from
40 # -vcd/-dvd to VCD:// and DVD://
41 # - added cuefile support for MPlayer version 1.0x
42 # - added new config option to distinguish MPlayer versions (0.9x or 1.0)
43 # important for playing cuefiles
44 # - fixed bug in detecting empty CACHE option
45 # - fixed a bug where USE_SPEED is ignored and '-speed' always used
46 # - added a check for the return value of 'mplayer -identify'
47 # - added support for AC3 sound when playing a DVD
48 # - make audio output configurable via config file (e.g. for DXR3)
50 # 0.8.3 - changed the default value of USERDEF from '-really-quiet' to '-quiet',
51 # because it suppressed the progressbar messages from mplayer
52 # - added '-dvd 1' to the DVD call to reliably play a DVD
53 # (though only the first one)
55 # 0.8.2 - changed the way mplayer is called to identify files;
56 # should prevent the opening of a X window, that happens
57 # on some systems
58 # - play mpeg files without any scaling if possible
59 # - added config line for AC3 out via DVB card (thanks to Christian Jacobsen)
60 # - added a new config option to choose the video out device;
61 # intended for dx3 users
62 # - added config options for subtitles (thanks to Martin Åkerström)
63 # - fixed a bug when SLOW_CPU is set to "false" instead of being
64 # just commented out
65 # - minor code clean-up
66 # - cleanup some comments and messages
68 # 0.8.1 - fixed a bug when DVDFiles or DVD not set correctly
69 # - if path to mplayer is incorrect, log an error and exit
70 # - cue file support; you can now choose a cue file as input;
71 # after I did this on user request, I asked myself, for what
72 # do one needs this, as mplayer can play .bin files directly...
73 # - support for 16/9 TV sets (thanks to Vaclav Janecek)
75 # 0.8.0 - bumped the version number to be more flexible
76 # - added VCD/DVD support
78 # 0.0.7 - clear LANG variable
79 # - added support for slow computers: reduces the Y
80 # resolution to the half of the possible maximum
81 # - use kind of a window for detecting the frames_per_sec
83 # 0.0.6a-jha
84 # - fixed a bug that prevented the SLAVE mode from working
86 # 0.0.6-jha
87 # - major rework
88 # - added config option to disable either NTSC or PAL support
89 # - added mplayer's '-speed' option; is disabled by default (see
90 # config file), because it didn't work as expected for me
91 # - rudimentary sub titles support:
92 # if a sub titles file (.sub suffix) with exactly the
93 # same name as the played video file exists, it is used
94 # (not really useful yet)
95 # - changed the was the config file is parsed:
96 # let's do it the simple way, just source the file
97 # =================================================
98 # ---> IMPORTANT! you MUST use the new config file
99 # =================================================
101 # 0.0.5d-jha
102 # - added AC3 support (thanks to Christian Jacobsen
103 # <christian.jacobsen -at- stageholding.de>)
104 # - better support for mplayers SLAVE mode
105 # - fixed some scaling problems
106 # - some minor clean-ups
108 # 0.0.5c-jha
109 # - play videos with 24 or 23.976 fps as NTSC, not PAL
111 # 0.0.5b-jha
112 # - Improved getvidxy - thanks to new mplayer option '-identify'
113 # it gets now the correct values for X-/Y-resolution
114 # and the framerate (if defined in the video file)
115 # for all media files
116 # - reworked the scaling calculation to scale only if needed
117 # and if so, scale as little as possible to save CPU power
118 # - distinguish between NTSC and PAL files; play them with
119 # the right TV norm
121 # 0.0.4 Improved getvidxy - No Temp Files needed anymore!
123 # 0.0.3 Seems to work fine - still lacks support for X x 432 Files
124 # What about MPEG Files ?
128 function initialize () {
129 # source config file
130 source "$CFGFIL"
132 # Debug Mode ?
133 if test -z "$DEBUG" -o "$DEBUG" != "true"; then DEBUG=false; fi
135 # use AC3?
136 if test -z "$USEAC3" -o "$USEAC3" != "true"; then USEAC3=false; fi
137 debugvar USEAC3 "$USEAC3"
139 # AC3 command line
140 if test -z "$AC3AOUT"; then
141 errorcfg AC3AOUT
142 exit
143 else
144 debugvar AC3AOUT "$AC3AOUT"
147 # TV aspect ratio
148 if test -z "$TV_ASPECT"; then errorcfg TV_ASPECT; fi
149 debugvar TV_ASPECT "$TV_ASPECT"
151 # can do PAL?
152 if test -z "$PAL" -o "$PAL" != "true"; then PAL=false; fi
153 debugvar PAL "$PAL"
155 # can do NTSC?
156 if test -z "$NTSC" -o "$NTSC" != "true"; then NTSC=false; fi
157 debugvar NTSC "$NTSC"
159 if test $NTSC == "false" -a $PAL == "false"; then
160 echolog "*** FATAL: Config Options NTSC and PAL both set to false ... Exiting."
161 exit
164 # use speed setting?
165 SPEED=""
166 if test -z "$USE_SPEED"; then USE_SPEED=false; fi
167 debugvar USE_SPEED "$USE_SPEED"
169 # which detelecining filter should be used, if configured
170 # to play NTSC as PAL
171 if test -z "$DETC_FILTER"; then DETC_FILTER="detc=dr=2:am=1"; fi
172 debugvar DETC_FILTER "$DETC_FILTER"
174 # where is Mplayer
175 if ! test -x "$MPLAYER" -a -f "$MPLAYER"; then
176 echolog "*** Option MPLAYER not found in config file or not set correctly"
177 exit
178 else
179 debugvar MPLAYER "$MPLAYER"
182 if test -z "$VOP"; then errorcfg VOP; exit; else debugvar VOP "$VOP"; fi
184 if test -z "$VO"; then errorcfg VO; exit; else debugvar VO "$VO"; fi
186 if test -z "$AO"; then errorcfg AO; exit; else debugvar AO "$AO"; fi
187 AOUT="-ao $AO"
189 if test -z $CACHE; then
190 echolog "*** Option CACHE not set in config file - calling mplayer without Cache!"
191 CACHESTR="-nocache"
192 else
193 CACHESTR="-cache $CACHE"; debugvar CACHE "$CACHE"; debugvar CACHESTR "$CACHESTR"
196 if test -z "$FRAMEDROP" -o "$FRAMEDROP" != "true"; then FRAMEDROP=false; fi
197 debugvar FRAMEDROP "$FRAMEDROP"
198 if $FRAMEDROP; then FDSTR="-framedrop"; fi
199 debugvar FDSTR "$FDSTR"
201 declare LIRCSTR="" # no extra Lirc option!
202 if ! test -z "$LIRCRC"; then LIRCSTR="-lircconf $LIRCRC"; fi
203 debugvar LIRCRC "$LIRCRC"; debugvar LIRCSTR "$LIRCSTR"
205 if ! test -z "$SUBPOS"; then SUBTITLES=" -subpos $SUBPOS"; fi
206 if ! test -z "$SUBCOLOR"; then SUBTITLES="$SUBTITLES -sub-bg-color $SUBCOLOR"; fi
207 if ! test -z "$SUBALPHA"; then SUBTITLES="$SUBTITLES -sub-bg-alpha $SUBALPHA"; fi
209 debugvar SUBTITLE "$SUBTITLES"
211 if test "$SLAVE" != "SLAVE"; then
212 REMOTE="$LIRCSTR"
213 else
214 REMOTE="-slave -nolirc"
216 debugvar REMOTE "$REMOTE"
218 if ! test -z "$USERDEF"; then echolog "*** Use Option USERDEF at your own risk!"; fi
219 debugvar USERDEF "$USERDEF"
221 if test -z $XResPAL; then errorcfg XResPAL; exit; else debugvar XResPAL "${XResPAL[*]}"; fi
222 if test -z $XResNTSC; then errorcfg XResNTSC; exit; else debugvar XResNTSC "${XResNTSC[*]}"; fi
224 # do we run on a slow computer?
225 if test -z "$SLOW_CPU" -o "$SLOW_CPU" == "false"; then
226 SLOW_CPU=false
227 else
228 XResPAL=(352)
229 XResNTSC=(352)
230 debugvar XResPAL "${XResPAL[*]}"
231 debugvar XResNTSC "${XResNTSC[*]}"
233 debugvar SLOW_CPU "$SLOW_CPU"
235 if ! test -d "$DVDFiles"; then debugmsg "*** Option DVDFiles not set correctly! You will not be able to play VCD/DVD" ""; DVDFiles=""; fi
236 debugvar DVDFiles "$DVDFiles"
237 if ! test -b "$DVD"; then debugmsg "*** Option DVD not set correctly! You will not be able to play VCD/DVD" ""; DVD=""; fi
238 debugvar DVD "$DVD"
239 if test -z $DVDLANG; then DVDLANG="en"; fi
240 debugvar DVDLANG "$DVDLANG"
242 # extra DVD options
243 debugvar DVDOPTIONS "$DVDOPTIONS"
245 # extra VCD options
246 debugvar VCDOPTIONS "$VCDOPTIONS"
248 # play MPEG files without rescaling?
249 if test -z "$MPEG_DIRECT" -o "$MPEG_DIRECT" != "false"; then MPEG_DIRECT="true"; fi
250 debugvar MPEG_DIRECT "$MPEG_DIRECT"
252 # get the file extension of the video
253 SUFFIX=$(echo -e "${FILE:$[${#FILE}-4]:4}" | tr [A-Z] [a-z])
254 debugvar SUFFIX $SUFFIX
256 if test -z "$MPLAYER_V1"; then
257 echolog "*** MPLAYER_V1 not set! Setting it to 'false'."
258 echolog "*** If you cannot play cue-files, then set it to 'true'."
259 MPLAYER_V1="false"
261 debugvar MPLAYER_V1 "$MPLAYER_V1"
263 return;
267 function getvidxy () {
269 # call: getvidxy
270 # determine x and y resolution of the file!
271 # output: variable ORIG_X and ORIG_Y (global)
273 # variable definitions
274 local TEMP1 MPLAYER_RETURN
275 ORIG_X=0; ORIG_Y=0
277 TEMP1=`$MPLAYER -identify -vo null -ao null -frames 0 "$FILE" 2>/dev/null | grep -i -e "^ID_"`
278 MPLAYER_RETURN=$?
279 debugmsg "OutputFromMPLAYER:" "$TEMP1"
280 debugmsg "MPLAYER_RETURN: " "$MPLAYER_RETURN"
282 if test $MPLAYER_RETURN -ne 0; then
283 echolog "*** FATAL: something went wrong analyzing the video; mplayer reported an error!"
284 echolog "*** FATAL: check your mplayer installation. Exiting..."
285 exit
288 ORIG_X=`echo "$TEMP1"|grep ID_VIDEO_WIDTH|cut -d"=" -f2`
289 debugmsg "parsed output for ORIG_X:" $ORIG_X
291 ORIG_Y=`echo "$TEMP1"|grep ID_VIDEO_HEIGHT|cut -d"=" -f2`
292 debugmsg "parsed output for ORIG_Y:" $ORIG_Y
294 ORIG_FPS=`echo "$TEMP1"|grep ID_VIDEO_FPS|cut -d"=" -f2`
295 debugmsg "parsed output for ORIG_FPS:" $ORIG_FPS
297 ORIG_ASPECT=`echo "$TEMP1"|grep ID_VIDEO_ASPECT|cut -d"=" -f2`
298 debugmsg "parsed output for ORIG_ASPECT:" $ORIG_ASPECT
300 VIDEO_FORMAT=`echo "$TEMP1"|grep ID_VIDEO_FORMAT|cut -d"=" -f2`
301 debugmsg "parsed output for VIDEO_FORMAT:" $VIDEO_FORMAT
303 AUDIO_CODEC=`echo "$TEMP1"|grep ID_AUDIO_CODEC|cut -d"=" -f2`
304 debugmsg "parsed output for AUDIO_CODEC:" $AUDIO_CODEC
306 return;
310 function calcnewxy () {
311 local -i MAX_X TEMP_ASPECT
312 # TEMP_ASPECT=`echo $ORIG_ASPECT|sed 's/\.//'`
313 # if test $TEMP_ASPECT -eq 0; then TEMP_ASPECT=$((4000/3)); fi
315 if test $ORIG_X -gt 352 -o $ORIG_Y -gt $HALF_Y; then
316 MAX_X=$(($FULL_Y*$TV_ASPECT))
317 else
318 MAX_X=$(($HALF_Y*$TV_ASPECT))
320 debugvar MAX_X $MAX_X
322 NEW_Y=$(($MAX_X*$ORIG_Y/$ORIG_X)); debugvar NEW_Y $NEW_Y
323 echolog "*** INFO: For Sqare Pixels we would scale to $MAX_X x $NEW_Y ..."
324 test $NEW_Y -gt $FULL_Y && NEW_Y=$FULL_Y # force full screen/ ignore aspect ratio!
326 return;
330 function choosebestx () {
331 local -i MAX_X INDEX
332 local XResTEMP
333 declare repeat=true
334 INDEX=0
336 if $SLOW_CPU; then
337 XResTEMP=(352)
338 else
339 if test $HALF_Y -eq 240; then
340 XResTEMP=(${XResNTSC[*]})
341 else
342 if test $HALF_Y -eq 288; then
343 XResTEMP=(${XResPAL[*]})
344 else
345 echolog "*** FATAL: HALF_Y not set or unknown: \"$HALF_Y\" ... Exiting."
346 exit
350 debugvar XResTEMP "${XResTEMP[*]}"
352 ANZAHL=${#XResTEMP[*]}
353 debugvar AnzahlVonXResTEMP $ANZAHL
355 until ! $repeat; do
356 if test ${XResTEMP[$INDEX]} -ge $ORIG_X; then repeat=false; NEW_X=${XResTEMP[$INDEX]}; fi
357 INDEX=$(($INDEX+1))
358 if test $INDEX -ge $ANZAHL; then repeat=false; NEW_X=${XResTEMP[$ANZAHL-1]}; fi
359 done
360 debugvar NEW_X $NEW_X
362 return;
366 function choosebesty () {
367 if test \( $ORIG_Y -eq $HALF_Y -a $ORIG_X -eq 352 \) -o \( $ORIG_Y -eq $FULL_Y -a ! $SLOW_CPU \); then
368 REAL_Y=$ORIG_Y
369 NEW_Y=$ORIG_Y
370 NEW_X=$ORIG_X
371 debugmsg "setting NEW = ORIG"
372 else
373 if test $ORIG_X -gt 352 -o $ORIG_Y -gt $HALF_Y; then
374 REAL_Y=$FULL_Y
375 debugmsg "setting REAL_Y = FULL_Y"
376 else
377 REAL_Y=$HALF_Y
378 debugmsg "setting REAL_Y = HALF_Y"
381 return;
385 function set_ntsc () {
386 HALF_Y=240
387 if $SLOW_CPU; then
388 FULL_Y=$HALF_Y
389 else
390 FULL_Y=480
392 NEW_FPS="30"
393 return;
397 function set_pal () {
398 HALF_Y=288
399 if $SLOW_CPU; then
400 FULL_Y=$HALF_Y
401 else
402 FULL_Y=576
404 NEW_FPS="25"
405 return;
409 function checkforac3 () {
410 if test "X$AUDIO_CODEC" == "Xa52" && $USEAC3; then AOUT="$AC3AOUT"; fi
411 return;
415 function checktvnorm () {
416 local -i TEMP_FPS
417 TEMP_FPS=`echo $ORIG_FPS|sed 's/\.//'`
418 if test $TEMP_FPS -ge "23000" -a $TEMP_FPS -le "24499"; then
419 debugmsg "Film"
420 if $NTSC; then
421 set_ntsc
422 else
423 set_pal
424 $USE_SPEED && SPEED="-speed 1.04" # 25/24 = 1.04
426 else
427 if test \( $TEMP_FPS -ge "14000" -a $TEMP_FPS -le "16000" \) -o \( $TEMP_FPS -ge "29000" -a $TEMP_FPS -le "30499" \); then
428 debugmsg "NTSC"
429 if $NTSC; then
430 set_ntsc
431 else
432 set_pal
433 if $USE_SPEED; then
434 SPEED="-speed 1.04" # 25/24 = 1.04
435 DETC=",$DETC_FILTER"
438 else
439 debugmsg "PAL or unknown" $ORIG_FPS
440 if $PAL; then
441 set_pal
442 else
443 set_ntsc
444 $USE_SPEED && SPEED="-speed 1.20" # 30/25 = 1.20
448 return;
452 function echolog () {
453 # prints the string on stdout and into /var/log/messages using logger!
454 logger -s -- "$1"
455 return;
459 function errorcfg () {
460 echolog "*** FATAL: Config Option $1 not found in config file ... Exiting."
461 exit;
465 function debugvar () {
466 if $DEBUG; then echolog "*** DEBUG: Variable $1 has value \"$2\""; fi
467 return;
471 function debugmsg () {
472 if $DEBUG; then echolog "*** DEBUG: $1 \"$2\""; fi
473 return;
477 # begin main!
480 # ---------------------------------------------------------------------------
484 declare -i ORIG_X ORIG_Y
485 declare -i NEW_X NEW_Y
486 declare -i HALF_Y FULL_Y
487 declare ORIG_FPS NEW_FPS ORIG_ASPECT VIDEO_FORMAT AUDIO_CODEC
488 declare CMDLINE AOUT REMOTE USERDEF SUFFIX
489 declare FILE="$1"
490 declare SLAVE="$2"
491 declare -a XResPAL[0]
492 declare -a XResNTSC[0]
493 declare FDSTR=""
494 declare SUBTITLES=""
495 declare DETC=""
497 echolog "*** Starting mplayer.sh Version $VERSION"
499 if test -z "$FILE"; then echolog "*** USAGE: mplayer.sh <File_to_be_played>"; exit; fi
500 if ! test -r "$FILE"; then echolog "*** ERROR: Make sure $FILE exists and is readable - otherwise it cannot be played ;-)"; exit; fi
502 # Check if config file exists!
503 declare CFGFIL="/etc/vdr/plugins/mplayer/mplayer.sh.conf"
504 debugvar CFGFIL $CFGFIL
505 if ! test -f $CFGFIL; then echolog "*** FATAL: mplayer.sh.conf not found!!! Exiting." ; exit; fi
507 # read config file and initialize the variables
508 initialize
511 if test \( "$FILE" == "$DVDFiles/DVD" -o "$FILE" == "$DVDFiles/VCD" \) -a -n "$DVDFiles" -a -n "$DVD"; then
512 if test "$FILE" == "$DVDFiles/DVD"; then
513 $USEAC3 && AOUT="$AC3AOUT"
514 CMDLINE="$MPLAYER -vo $VO $AOUT -alang $DVDLANG $DVDOPTIONS $FDSTR $CACHESTR $REMOTE $USERDEF -dvd-device $DVD dvd://"
516 if test "$FILE" == "$DVDFiles/VCD"; then
517 CMDLINE="$MPLAYER -vo $VO $AOUT $VCDOPTIONS $FDSTR $CACHESTR $REMOTE $USERDEF -cdrom-device $DVD vcd://"
519 unset FILE
520 elif test "${SUFFIX}" == ".cue"; then
521 if $MPLAYER_V1; then
522 CMDLINE="$MPLAYER -vo $VO $AOUT $FDSTR $CACHESTR $REMOTE $USERDEF cue://$FILE:2"
523 unset FILE
524 else
525 CMDLINE="$MPLAYER -vo $VO $AOUT $FDSTR $CACHESTR $REMOTE $USERDEF -vcd 2 -cuefile"
527 else
528 # Try to determine the video attributes
529 if $DEBUG; then echolog "*** DEBUG: Calling getvidxy function to analyze source video stream ..."; fi
530 getvidxy
531 echolog "*** INFO: Source Video has Resolution of $ORIG_X x $ORIG_Y ..."
532 if test $ORIG_X -eq 0 -o $ORIG_Y -eq 0; then echolog "*** FATAL: something went wrong analyzing the video; check your mplayer version ..."; exit; fi
534 NEW_FPS=$ORIG_FPS
536 # if `echo "$VIDEO_FORMAT"| egrep -q "RV??"`; then
537 # FORCEIDX="-forceidx"
538 # else
539 # FORCEIDX=""
540 # fi
542 checkforac3
543 checktvnorm
544 calcnewxy
545 choosebestx
546 choosebesty
547 if test $NEW_X -eq 0 -o $NEW_Y -eq 0; then echolog "*** FATAL: Illegal Resolution ..."; exit; fi
549 if test $MPEG_DIRECT == "true" -a \( $VIDEO_FORMAT == "0x10000001" -o $VIDEO_FORMAT == "0x10000002" \) -a \( \
550 \( $PAL -a $NEW_FPS == "25" -a \( \( $ORIG_X == "352" -a $ORIG_Y == "288" \) -o \( $ORIG_Y == "576" -a \
551 \( $ORIG_X == "352" -o $ORIG_X == "480" -o $ORIG_X == "528" -o $ORIG_X == "544" -o $ORIG_X == "704" -o $ORIG_X == "720" \) \) \) \) -o \
552 \( $NTSC -a $NEW_FPS == "30" -a \( \( $ORIG_X == "352" -a $ORIG_Y == "240" \) -o \( $ORIG_Y == "480" -a \
553 \( $ORIG_X == "352" -o $ORIG_X == "480" -o $ORIG_X == "512" -o $ORIG_X == "544" -o $ORIG_X == "640" -o $ORIG_X == "704" -o $ORIG_X == "720" \) \) \) \) \
554 \) ; then
555 CMDLINE="$MPLAYER -vo $VO $AOUT $FDSTR $CACHESTR $REMOTE $USERDEF"
556 else
557 CMDLINE="$MPLAYER -vo $VO $AOUT -vf scale=$NEW_X:${NEW_Y}${DETC},expand=$NEW_X:$REAL_Y:-1:-1:1,$VOP:$NEW_FPS $SPEED $FDSTR $CACHESTR $REMOTE $SUBTITLES $USERDEF $FORCEIDX"
561 debugvar CMDLINE "$CMDLINE"
562 exec $CMDLINE "$FILE"
563 exit