b87961403a0510b41e214d7adcaf1f59793962e5
[BABOOSHKA.TV] / playmovie.sh
1 #!/bin/bash
2
3 if [ $# -lt 1 ]; then
4         echo "IM Media Player. Created for Interplaymediumâ„¢ project (https://interplaymedium.org)"
5         echo "Copyright Â© 2013 Dmitry Shalnov [interplaymedium.org]"
6         echo "Licensed under the Apache License, Version 2.0"
7
8         echo "Usage: $0 -i <media url or file> [-m <video mode>] [-s <shift in min>] [-v <volume amp in dB>] [-c <crop vertically in px>] [-r <aspect mode>] [-d <deinterlace mode>]"
9         echo "Dependences: omxplayer, youtube-dl, ttf-freefont, [bc]"
10         exit 1
11 fi
12
13 # ------------------ settings ----------------------------------------------
14
15 # set screen resolution
16
17 # media box
18 #WIDTH=1024
19 #HEIGHT=580 # 620
20 #VERTICAL_COEFF="1.3"   # my screen
21
22 # IMAX edition TODO: stratching the image, need to be improved
23 # HEIGHT=720
24
25 # babooshka.tv
26
27 WIDTH=720
28 HEIGHT=576
29 VERTICAL_COEFF="1.12"   # babooshka SONY TV
30
31 # other default settings 
32
33 SHIFTTIME=0
34 VOLUME=0
35 AUDIOTRACK=2
36 SUBTITLES_SET=''
37 DEINTERLACE_SET=''
38 WINMODE_SET=''
39 timeTotal=0
40
41 # ------------ set environment ------------------------------------------------
42
43 # if [ "$VIDEOMODE" != "0" ]; then
44 #       ./lght.sh 3
45 #       ./prjctr.sh 1
46 # fi
47
48 # ------------ parse commandline ----------------------------------------------
49
50 while [ "$#" -gt 0 ]; do
51         case "$1" in
52                 -i) MEDIAFILE="$2"; shift 2;;   # media file
53                 -m) VIDEOMODE="$2"; shift 2;;   # video mode
54                 -s) SHIFTTIME="$2"; shift 2;;   # shift in minutes
55                 -v) VOLUME="$2"; shift 2;;      # volume amp
56                 -a) AUDIOTRACK="$2"; shift 2;;  # number fo track
57                 -t) SUBTITLES="$2"; shift 2;;   # number fo track
58                 -c) CROP="$2"; shift 2;;        # pixels froms all sides
59                 -r) ASPECTMODE="$2"; shift 2;;  # aspect mode   (stretch, letterbox...)
60                 -d) DEINTERLACE="$2"; shift 2;; # deinterlace
61
62                 -*) echo "unknown option: $1" >&2; exit 1;;
63 #               *) handle_argument "$1"; shift 1;;
64         esac
65 done
66
67 # VIDEOMODE="720x576"
68
69 VOLUME=$(( $VOLUME * 100 ))
70
71 echo Audiotrack: $AUDIOTRACK
72
73 # ------------ preliminary calculation ---------------------------------------
74
75 # set aspect move 
76
77 if [ "$ASPECTMODE" != "" ]; then ASPECTMODE_SET=$ASPECTMODE; else ASPECTMODE_SET="stretch"; fi
78
79 # set deinterlace move 
80
81 if [ "$DEINTERLACE" != "" ]; then DEINTERLACE_SET="-d --advanced $DEINTERLACE"; fi
82
83 # set subtitles
84
85 if [ "$SUBTITLES" != "" ]; then 
86         SUBTITLES_SET="-t $SUBTITLES"
87         echo Subtitles: $SUBTITLES
88 fi
89
90 SHIFT=$(($SHIFTTIME*60))
91
92 echo "Time shift: $SHIFT"
93
94 startTime=$(date +%s)
95 #interruptTime=$(($bt+$SHIFT))
96 interruptTime=$(($bt))
97
98 # ---------- helpful functions ----------------------------------------------
99
100 round() {
101         echo $1 | awk '{split($0,a,"."); print a[1]}'
102 }
103
104 calc() {
105         # echo "$1" | bc -l
106         EXPR=$(echo "$1" | sed 's/ //g')
107         awk 'BEGIN { print ('$EXPR') }'
108 }
109
110 getMode () {
111
112 #       --- zona test workaround ---
113 #       width=720
114 #       height=360
115
116         if [ "$VIDEOMODE" != "" ]; then
117                 width=$( echo $VIDEOMODE | awk '{split($0,a,"x"); print a[1]}')
118                 height=$( echo $VIDEOMODE | awk '{split($0,a,"x"); print a[2]}')
119         else 
120                 width=$1
121                 height=$2
122         fi
123
124 #       zoomCoeff=1
125 #       VERTICAL_COEFF="1.3"    # my screen
126
127         vShift=0
128         hShift=0
129
130         height=$( calc "$VERTICAL_COEFF * $height" )
131         
132         comp=$( calc " $width/$height > $WIDTH/$HEIGHT" )
133
134         if [ "$comp" == "1" ]; then
135 #               echo "calculate by width" >&2
136
137                 zoomCoeff=$( calc "$WIDTH / $width" )
138                 height=$( calc "$zoomCoeff * $height" )
139 #               width=1024
140                 width=$WIDTH
141
142                 vShift=$( calc "($HEIGHT - $height) / 2")
143                 height=$( calc "$height + $hShift")
144         else 
145 #               echo "calculate by height" >&2
146
147                 zoomCoeff=$( calc "$HEIGHT / $height" )
148                 width=$( calc "$zoomCoeff * $width" )
149 #               height=620
150                 height=$HEIGHT
151
152                 hShift=$( calc "($WIDTH - $width) / 2")
153                 width=$( calc "$width + $hShift")
154
155         fi 
156
157         vShift=$( round $vShift )
158         hShift=$( round $hShift )
159
160         height=$( round $height )
161         width=$( round $width )
162
163 #       echo "$hShift, $vShift, $width, $height" >&2
164
165         echo "--win $hShift,$vShift,$width,$height"
166 }
167
168 getTotalDuration(){
169                 totalDuration=$( echo "$metaData" | grep 'Duration:' | head -1 | awk '{gsub(/\([^\)]*\)/, "");$1=$1}1' | awk '{split($0,a,","); print a[1]}' | awk '{split($0,a," "); print a[2]}' )
170                 totalDurationInSec=$(echo "$totalDuration" | awk -F. '{print $1}' | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')
171
172 #               echo "metaData :: \"$metaData\"" >&2
173 #               echo "$metaData" | grep 'Duration:' | head -1 >&2
174                 echo $totalDurationInSec
175 }
176
177 getModeByMedia(){
178
179                 metaData=$1
180
181                 res=$( echo "$metaData" | grep 'Video:' | head -1 | awk '{gsub(/\([^\)]*\)/, "");$1=$1}1' | awk '{split($0,a,","); print a[3]}' | awk '{split($0,a," "); print a[1]}' )
182
183                 width=$( echo $res | awk '{split($0,a,"x"); print a[1]}' | tr -d -c 0-9 )
184                 height=$( echo $res | awk '{split($0,a,"x"); print a[2]}' | tr -d -c 0-9 )
185
186 #               echo ">>> $res, $width, $height" >&2
187
188                 if [[ $CROP != "" ]]; then
189
190                         cX=$(( 0 ))
191                         cY=$(( $CROP ))
192                         cX2=$(( $width ))
193                         cY2=$(( $height - $CROP ))
194         
195                         cropMode=$(echo "--crop" $cX,$cY,$cX2,$cY2)
196
197                         width=$(( $width + $CROP * 4 ))
198
199                 fi 
200
201                 WINMODE_SET=$( getMode $width $height )
202
203                 echo "$cropMode" "$WINMODE_SET"
204 }
205
206 # ---------------------- playback ----------------------------------------------
207
208         
209         if [[ $MEDIAFILE == *"youtu.be"* ]]; then
210
211                 # -------------- extracting youtube URL ------
212
213                 echo "Extracting: $MEDIAFILE"
214                 MEDIAFILE=$(wget -qO- "$MEDIAFILE" | grep "canonical" | awk '{string = index($0, "canonical"); print substr($0, string + 17, index($0, "alternate") - string - 13 - 17) }')
215                 title=$(wget -qO- "$MEDIAFILE" | grep "canonical" | awk '{string = index($0, "<title>"); print substr($0, string + 7, index($0, "</title>") - string - 8 - 9) }')       
216                 echo "url:      $MEDIAFILE"
217                 echo "title:    $title"
218         fi
219
220         if [[ $MEDIAFILE == *"vimeo"* ]]; then
221
222                 # -------------- playing vimeo -------------
223
224                 echo "Media file (vimeo): $MEDIAFILE"
225
226                 vimeoID=$(echo "$MEDIAFILE" | awk -F '/' '{print $4}')
227
228                 vimeoData=$( curl -k "https://player.vimeo.com/video/$vimeoID" )
229
230                 # get video resolution and URL
231
232                 widthHeight=$( echo $vimeoData | sed -e 's/[{}]/''/g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | grep "\"width\"\|\"height\"" )
233
234                 width=$( echo $widthHeight | awk -F ' ' '{print $1}' | awk -F ':' '{print $2}' )
235                 height=$( echo $widthHeight | awk -F ' ' '{print $2}' | awk -F ':' '{print $2}' )
236
237                 if [ "$VIDEOMODE" != "0" ]; then
238                         WINMODE_SET=$( getMode $width $height )
239                 fi
240                         
241                 echo "URL: $myUrl"
242                 echo "Video: $WINMODE_SET, $ASPECTMODE_SET, $timeTotal"
243
244                 myUrl=$( echo $vimeoData | sed -e 's/[{}]/''/g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | grep ".mp4" | grep "url" | awk -F '"' '{print $4}' | tail -1)
245
246                 # play file
247
248                 curl -k "$myUrl" | omxplayer pipe:0 $DEINTERLACE_SET --no-keys -b -o local --amp 0 --vol $VOLUME $WINMODE_SET --aspect-mode $ASPECTMODE_SET
249
250         elif [[ $MEDIAFILE == *"youtube"* ]]; then
251
252                 # -------------- playing youtube -------------
253
254                 echo "Media file (youtube): $MEDIAFILE"
255                 
256                 if [ "$VIDEOMODE" != "0" ]; then
257                         myUrl=$(youtube-dl -f 'bestvideo[vcodec=avc1][height<=720]+bestaudio/best[abr<=120]' -g "$MEDIAFILE") # best video + audio
258                         metaData=$( omxplayer -i "$myUrl" 2>&1 )
259                         WINMODE_SET=$( getModeByMedia "$metaData" );
260                 else 
261                         myUrl=$(youtube-dl -f 'bestaudio/best[abr<=120]' -g "$MEDIAFILE") # best audio only
262                         WINMODE_SET=''
263                 fi 
264
265                 timeTotal=$(getTotalDuration "$metaData")
266
267                 echo "URL: $myUrl"
268                 echo "Video: $WINMODE_SET, $ASPECTMODE_SET, $timeTotal"
269
270                 # play file
271
272                 omxplayer -l 0:0:0 -b $DEINTERLACE_SET --amp 0 --vol $VOLUME -n $AUDIOTRACK -o local "$myUrl" $WINMODE_SET --aspect-mode $ASPECTMODE_SET --align center $SUBTITLES_SET
273
274         elif [[ $MEDIAFILE == *"http"* ]]; then
275
276                 # -------------- playing remote file via http(s) ---------------
277
278                 echo "Media file (URL): $MEDIAFILE"
279
280                 # get video resolution
281
282                 metaData=$( omxplayer -i "$MEDIAFILE" 2>&1 )
283                 timeTotal=$(getTotalDuration "$metaData")
284
285                 if [ "$VIDEOMODE" != "0" ]; then
286                         WINMODE_SET=$( getModeByMedia "$metaData" )
287                 fi 
288
289                 echo "WINMODE_SET (http): $WINMODE_SET"
290
291                 if [[ $MEDIAFILE != *"24video"* ]] || [[ $MEDIAFILE != *"vibio"* ]]; then
292                         MEDIAFILE="${MEDIAFILE/http:/https:}"
293                 fi
294
295                 echo "Video: $WINMODE_SET, $ASPECTMODE_SET, $timeTotal"
296         
297                 # play file (loop is broken stream workaround)
298
299                 for iter in {1..6}; do 
300                         SKIPSEC=$(( ($interruptTime - $startTime + $SHIFT) * 128000 ))  # ~128kbs ? TODO: get datarate from file
301                         curl -k "$MEDIAFILE" | tail -c +$SKIPSEC | omxplayer pipe:0 $DEINTERLACE_SET -b --no-keys -o local $WINMODE_SET --amp 0 --vol $VOLUME $WINMODE_SET --aspect-mode $ASPECTMODE_SET
302                         interruptTime=$(date +%s)
303                 done    
304
305         else    
306
307                 # -------------- playing local file ---------------
308
309                 echo "Media file (local): $MEDIAFILE"
310
311                 metaData=$( omxplayer -i "$MEDIAFILE" 2>&1 )
312                 timeTotal=$(getTotalDuration "$metaData")
313
314                 if [ "$VIDEOMODE" != "0" ]; then
315                         WINMODE_SET=$( getModeByMedia "$metaData" )
316                 fi 
317
318                 echo "Video: $WINMODE_SET, $ASPECTMODE_SET, $timeTotal"
319
320                 # play file
321
322                 omxplayer $DEINTERLACE_SET -l $SHIFT -b --amp 0 --vol $VOLUME -n $AUDIOTRACK -o local "$MEDIAFILE" $WINMODE_SET --aspect-mode $ASPECTMODE_SET --align center $SUBTITLES_SET
323         
324         fi 
325
326
327 # -------------- environment  ----------------------------------------------
328
329 # turn off projector if playback time > 60 min
330
331 endTime=$(date +%s)
332 timeDif=$(( $endTime - $startTime ))
333 echo "Playback duration: $timeDif/$timeTotal"
334
335 # if [ "$timeDif" -gt "3600" ]; then
336 #       ./prjctr.sh 0
337 # fi
338
Contact me: dev (at) shalnoff (dot) com
PGP fingerprint: A6B8 3B23 6013 F18A 0C71 198B 83D8 C64D 917A 5717