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"
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]"
13 # ------------------ settings ----------------------------------------------
15 # set screen resolution
20 #VERTICAL_COEFF="1.3" # my screen
22 # IMAX edition TODO: stratching the image, need to be improved
29 VERTICAL_COEFF="1.12" # babooshka SONY TV
31 # other default settings
41 # ------------ set environment ------------------------------------------------
43 # if [ "$VIDEOMODE" != "0" ]; then
48 # ------------ parse commandline ----------------------------------------------
50 while [ "$#" -gt 0 ]; do
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
62 -*) echo "unknown option: $1" >&2; exit 1;;
63 # *) handle_argument "$1"; shift 1;;
69 VOLUME=$(( $VOLUME * 100 ))
71 echo Audiotrack: $AUDIOTRACK
73 # ------------ preliminary calculation ---------------------------------------
77 if [ "$ASPECTMODE" != "" ]; then ASPECTMODE_SET=$ASPECTMODE; else ASPECTMODE_SET="stretch"; fi
79 # set deinterlace move
81 if [ "$DEINTERLACE" != "" ]; then DEINTERLACE_SET="-d --advanced $DEINTERLACE"; fi
85 if [ "$SUBTITLES" != "" ]; then
86 SUBTITLES_SET="-t $SUBTITLES"
87 echo Subtitles: $SUBTITLES
90 SHIFT=$(($SHIFTTIME*60))
92 echo "Time shift: $SHIFT"
95 #interruptTime=$(($bt+$SHIFT))
96 interruptTime=$(($bt))
98 # ---------- helpful functions ----------------------------------------------
101 echo $1 | awk '{split($0,a,"."); print a[1]}'
106 EXPR=$(echo "$1" | sed 's/ //g')
107 awk 'BEGIN { print ('$EXPR') }'
112 # --- zona test workaround ---
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]}')
125 # VERTICAL_COEFF="1.3" # my screen
130 height=$( calc "$VERTICAL_COEFF * $height" )
132 comp=$( calc " $width/$height > $WIDTH/$HEIGHT" )
134 if [ "$comp" == "1" ]; then
135 # echo "calculate by width" >&2
137 zoomCoeff=$( calc "$WIDTH / $width" )
138 height=$( calc "$zoomCoeff * $height" )
142 vShift=$( calc "($HEIGHT - $height) / 2")
143 height=$( calc "$height + $hShift")
145 # echo "calculate by height" >&2
147 zoomCoeff=$( calc "$HEIGHT / $height" )
148 width=$( calc "$zoomCoeff * $width" )
152 hShift=$( calc "($WIDTH - $width) / 2")
153 width=$( calc "$width + $hShift")
157 vShift=$( round $vShift )
158 hShift=$( round $hShift )
160 height=$( round $height )
161 width=$( round $width )
163 # echo "$hShift, $vShift, $width, $height" >&2
165 echo "--win $hShift,$vShift,$width,$height"
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 }')
172 # echo "metaData :: \"$metaData\"" >&2
173 # echo "$metaData" | grep 'Duration:' | head -1 >&2
174 echo $totalDurationInSec
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]}' )
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 )
186 # echo ">>> $res, $width, $height" >&2
188 if [[ $CROP != "" ]]; then
193 cY2=$(( $height - $CROP ))
195 cropMode=$(echo "--crop" $cX,$cY,$cX2,$cY2)
197 width=$(( $width + $CROP * 4 ))
201 WINMODE_SET=$( getMode $width $height )
203 echo "$cropMode" "$WINMODE_SET"
206 # ---------------------- playback ----------------------------------------------
209 if [[ $MEDIAFILE == *"youtu.be"* ]]; then
211 # -------------- extracting youtube URL ------
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"
220 if [[ $MEDIAFILE == *"vimeo"* ]]; then
222 # -------------- playing vimeo -------------
224 echo "Media file (vimeo): $MEDIAFILE"
226 vimeoID=$(echo "$MEDIAFILE" | awk -F '/' '{print $4}')
228 vimeoData=$( curl -k "https://player.vimeo.com/video/$vimeoID" )
230 # get video resolution and URL
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\"" )
234 width=$( echo $widthHeight | awk -F ' ' '{print $1}' | awk -F ':' '{print $2}' )
235 height=$( echo $widthHeight | awk -F ' ' '{print $2}' | awk -F ':' '{print $2}' )
237 if [ "$VIDEOMODE" != "0" ]; then
238 WINMODE_SET=$( getMode $width $height )
242 echo "Video: $WINMODE_SET, $ASPECTMODE_SET, $timeTotal"
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)
248 curl -k "$myUrl" | omxplayer pipe:0 $DEINTERLACE_SET --no-keys -b -o local --amp 0 --vol $VOLUME $WINMODE_SET --aspect-mode $ASPECTMODE_SET
250 elif [[ $MEDIAFILE == *"youtube"* ]]; then
252 # -------------- playing youtube -------------
254 echo "Media file (youtube): $MEDIAFILE"
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" );
261 myUrl=$(youtube-dl -f 'bestaudio/best[abr<=120]' -g "$MEDIAFILE") # best audio only
265 timeTotal=$(getTotalDuration "$metaData")
268 echo "Video: $WINMODE_SET, $ASPECTMODE_SET, $timeTotal"
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
274 elif [[ $MEDIAFILE == *"http"* ]]; then
276 # -------------- playing remote file via http(s) ---------------
278 echo "Media file (URL): $MEDIAFILE"
280 # get video resolution
282 metaData=$( omxplayer -i "$MEDIAFILE" 2>&1 )
283 timeTotal=$(getTotalDuration "$metaData")
285 if [ "$VIDEOMODE" != "0" ]; then
286 WINMODE_SET=$( getModeByMedia "$metaData" )
289 echo "WINMODE_SET (http): $WINMODE_SET"
291 if [[ $MEDIAFILE != *"24video"* ]] || [[ $MEDIAFILE != *"vibio"* ]]; then
292 MEDIAFILE="${MEDIAFILE/http:/https:}"
295 echo "Video: $WINMODE_SET, $ASPECTMODE_SET, $timeTotal"
297 # play file (loop is broken stream workaround)
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)
307 # -------------- playing local file ---------------
309 echo "Media file (local): $MEDIAFILE"
311 metaData=$( omxplayer -i "$MEDIAFILE" 2>&1 )
312 timeTotal=$(getTotalDuration "$metaData")
314 if [ "$VIDEOMODE" != "0" ]; then
315 WINMODE_SET=$( getModeByMedia "$metaData" )
318 echo "Video: $WINMODE_SET, $ASPECTMODE_SET, $timeTotal"
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
327 # -------------- environment ----------------------------------------------
329 # turn off projector if playback time > 60 min
332 timeDif=$(( $endTime - $startTime ))
333 echo "Playback duration: $timeDif/$timeTotal"
335 # if [ "$timeDif" -gt "3600" ]; then