forked from petrkotek/chromecastize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchromecastize.sh
executable file
·558 lines (486 loc) · 20.6 KB
/
chromecastize.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#!/bin/bash
# uncomment the following if you want to output every line
#set -x
# the ffmpeg options were taken from the following webpage :
# http://superuser.com/questions/724204/optimum-parameters-for-ffmpeg-to-keep-file-size
# the ffmpeg progress bar is inspired from the following webpage :
# https://gist.github.com/MarcosBL/9568091
# this is a forkof the chromecastize repo from bc-petrkotek :
# https://github.com/bc-petrkotek/chromecastize
# Config
TEMPDIR="/tmp/chromecastize"
HOME=~/.chromecastize
SUPPORTED_EXTENSIONS=('mkv' 'avi' 'mp4' '3gp' 'mov' 'mpg' 'mpeg' 'qt' 'wmv' 'm2ts' 'flv')
# Initialization
APPDIR=`dirname "${BASH_SOURCE[0]}"`
. $APPDIR/common.sh
CURRENTDIR=`pwd`
CURRENTIFS=$IFS
IFS=$(echo -en "\n\b")
DRYRUN=false
AUTODELETE=false
SHOWIGNORED=false
CLEANHOME=false
SCRIPT_START_TIME=`date +%s`
TotalSizeTranscoded=0
TotalSizeInflation=0
TotalNbFileTranscoded=0
if ! [ -d "$HOME" ]; then
mkdir $HOME
fi
touch $HOME/transcode_failed.log
touch $HOME/transcode_success.log
touch $HOME/check_and_remove.sh
chmod +x $HOME/check_and_remove.sh
trap ctrl_c INT
function ctrl_c() {
echo -e "\n$bldwht$bakred Trapped CTRL-C. Doing some cleanup before quitting...$txtrst"
# kill ffmpeg if it's running
if [ -e /proc/$PID ]; then
echo -e "$bldred Killing ffmpeg..\c"
while [ -e /proc/$PID ]; do
echo -e ".\c"
kill $PID
sleep 3
done
echo -e "$txtrst\n"
fi
# remove incomplete transcoded file
if [ -f $OUTPUT ] ; then
rm -f $OUTPUT
echo -e "$bldred Removed incomplete transcoded file ($bldgrn$OUTPUT$bldred).$txtrst"
fi
# Cleaning temp directory
echo -e "$bldred Cleaning temp directory.$txtrst"
cleanup_temp
echo -e "$bldwht$bakred Cleanup complete. Exiting.$txtrst"
exit 1
}
is_supported_ext() {
EXT=`echo $1 | tr '[:upper:]' '[:lower:]'`
in_array "$EXT" "${SUPPORTED_EXTENSIONS[@]}"
}
cleanup_temp() {
if [ -d "$TEMPDIR" ]; then
rm -f $TEMPDIR/*
else
mkdir $TEMPDIR
fi
}
getVCodec() {
echo $($mediainfo --Output=XML "$1" 2>&1 |sed -n '/track type="Video"/,/track/p' | grep "Format" | head -n1 | sed -n '/Format/,/Format/p' | cut -d\> -f2 |cut -d\< -f1 | sed 's/^ *//;s/ *$//')
}
is_supported_vcodec() {
vcodec=$(getVCodec "$1")
if [[ "$vcodec" == "AVC" ]]; then
return 0
else
return 1
fi
}
getACodec() {
echo $($mediainfo --Output=XML "$1" 2>&1 |sed -n '/track type="Audio"/,/track/p' | grep "Format" | head -n1 | sed -n '/Format/,/Format/p' | cut -d\> -f2 |cut -d\< -f1 | sed 's/^ *//;s/ *$//')
}
is_supported_acodec() {
acodec=$(getACodec "$1")
if [[ "$acodec" == "AAC" ]]; then
return 0
else
return 1
fi
}
getVBitrate() {
echo $($mediainfo --Output=XML "$1" 2>&1 |sed -n '/track type="Video"/,/track/p' | grep "Bit_rate" | head -n1 | sed -n '/Bit_rate/,/Bit_rate/p' | cut -d\> -f2 |cut -d\< -f1 | sed 's/ //g' | sed 's/Kbps/k/g' | sed 's/^ *//;s/ *$//')
}
getABitrate() {
abr=$($mediainfo --Output=XML "$1" 2>&1 |sed -n '/track type="Audio"/,/track/p' | grep "Bit_rate__" | head -n1 | sed -n '/Bit_rate/,/Bit_rate/p' | cut -d\> -f2 |cut -d\< -f1 | sed 's/ //g' | sed 's/Kbps/k/g' | sed 's/^ *//;s/ *$//')
if [ -z "$abr" ]; then
abr=$($mediainfo --Output=XML "$1" 2>&1 |sed -n '/track type="Audio"/,/track/p' | grep "Bit_rate>" | head -n1 | sed -n '/Bit_rate/,/Bit_rate/p' | cut -d\> -f2 |cut -d\< -f1 | sed 's/ //g' | sed 's/Kbps/k/g' | sed 's/^ *//;s/ *$//')
fi
echo $abr
}
getFPS() {
fps=$($mediainfo --Output=XML "$1" 2>&1 |sed -n '/track type="Video"/,/track/p' | grep "Frame_rate__" | head -n1 | sed -n '/Frame_rate/,/Frame_rate/p' | cut -d\> -f2 |cut -d\< -f1 | cut -d. -f1 |sed 's/ //g' | sed 's/^ *//;s/ *$//')
if [ -z "$fps" ]; then
fps=$($mediainfo --Output=XML "$1" 2>&1 |sed -n '/track type="Video"/,/track/p' | grep "Frame_rate>" | head -n1 | sed -n '/Frame_rate/,/Frame_rate/p' | cut -d\> -f2 |cut -d\< -f1 | cut -d. -f1 |sed 's/ //g' | sed 's/^ *//;s/ *$//')
fi
echo $fps
}
getDuration() {
dur=$($mediainfo --Output=XML --Full "$1" 2>&1 |sed -n '/track type="Video"/,/track/p' | grep "Duration__" | grep ":..:" | head -n1 | cut -d\> -f2 | cut -d\< -f1 )
if [ -z "$dur" ]; then
dur=$($mediainfo --Output=XML --Full "$1" 2>&1 |sed -n '/track type="Video"/,/track/p' | grep "Duration>" | grep ":" | sed -n '/Duration/,/Duration/p' | cut -d\> -f2 |cut -d\< -f1 | cut -d. -f1 |sed 's/ //g' | sed 's/^ *//;s/ *$//')
fi
echo $dur
}
getFileSizeMB() {
blocks=$(stat "$1" | sed '2q;d' | cut -d: -f2 | sed 's/^ *//;s/ *$//' | cut -d\ -f1)
echo $($blocks/1024/1024)
}
show_prog_bar() {
local c="$1" # Character to use to draw progress bar
local v=$2 # Percentage 0<= value <=100
local t=$3 # Text before
local pbl=50 # Progress bar length in characters
local r=`expr 100 / $pbl` # Ratio between percentage value and progress bar length
local p="$v%" # Percentage value to display in the middle of progress bar
local l=${#p} # Length of string to display
local pbs="" # Progress bar string
local k=`expr \( $pbl - $l \) / 2` # Position where to substitute in progress bar string
local n1=`expr $v / $r`
local n2=`expr $pbl - $n1`
for (( i=0; i<$pbl; i++ )); do
pbs="${pbs}${c}"
done
pbs=`echo "$pbs" | sed 's/^\(.\{'"${k}"'\}\)\(.\{'"${l}"'\}\)\(.*$\)/\1'"${p}"'\3/'`
printf "\r\e[0m${t} \e[1;42m%s\e[1;41m%s\e[0m%s" "${pbs:0:$n1}" "${pbs:$n1:$n2}" "${c}"
}
display () {
START=$(date +%s); FR_CNT=0; ETA=0; ELAPSED=0
while [ -e /proc/$PID ]; do # Is FFmpeg running?
sleep 1
# VSTATS=$(awk -W interactive '{gsub(/frame=/, "")}/./{line=$1-1} END{print line}' $TEMPDIR/vstats) # Parse vstats file.
VSTATS="init"
if [ -f $TEMPDIR/vstats ] ; then
VSTATS=`cat $TEMPDIR/vstats | tail -1 | sed 's/\r/\n/g' | tail -1 | sed "s/.*frame=\( *\)\([^[:blank:]]*\) .*/\2/" | tail -1`
fi
re='^[0-9]+$'
if [[ $VSTATS =~ $re ]] ; then
if [ $VSTATS -gt $FR_CNT ]; then # Parsed sane or no?
FR_CNT=$VSTATS
PERCENTAGE=$(( 100 * FR_CNT / TOT_FR )) # Progbar calc.
ELAPSED=$(( $(date +%s) - START )); echo $ELAPSED > $TEMPDIR/elapsed.value
ETA=$(awk 'BEGIN{print int(('$ELAPSED' / '$FR_CNT') * ('$TOT_FR' - '$FR_CNT'))}') # ETA calc.
fi
show_prog_bar " " $PERCENTAGE "\tFrame: $FR_CNT / $TOT_FR - Done: $(convertsecs $ELAPSED) Remaining: $(convertsecs $ETA)"
fi
done
}
process_file() {
# initialization
cleanup_temp
ARGFILE="$@"
FILE=`$REALPATH "$ARGFILE"`
BASENAME=$(basename "$ARGFILE")
EXTENSION="${BASENAME##*.}"
FOLDER=$(dirname "$FILE")
TRANSCODEVIDEO=true
TRANSCODEAUDIO=true
TRANSCODE=true
FIRSTPASSSUCCESS=false
SECONDPASSSUCCESS=false
SINGLEPASSSUCCESS=false
# test extension
if ! is_supported_ext "$EXTENSION"; then
if ($SHOWIGNORED); then
echo -e "$bldlbl===========Ignored file$bldgrn $BASENAME$bldlbl since it is$bldpur not a supported extension.$bldlbl ( file is in $bldgrn$FOLDER$bldlbl folder) $txtrst"
fi
continue
fi
now=`date +%s`
currentRunningTime=$(($now-$SCRIPT_START_TIME))
now=`date`
echo -e "$bldlbl==========="
echo -e "Processing: $bldgrn $BASENAME$txtlbl (in $FOLDER) - $now - script has been running for $(convertsecs $currentRunningTime)$txtrst"
# test video codec
if is_supported_vcodec "$FILE"; then
echo -e "$txtgrn -$bldgrn video$txtgrn format is$bldgrn already correct$txtgrn (h264=AVC), no transcode required for video$txtrst"
TRANSCODEVIDEO=false
else
vcodec=$(getVCodec "$FILE")
echo -e "$txtgrn -$bldgrn video$txtgrn needs to be transcoded from$bldgrn $vcodec to h264$txtrst"
TRANSCODEVIDEO=true
fi
# test audio codec
if is_supported_acodec "$FILE"; then
echo -e "$txtgrn -$bldgrn audio$txtgrn format is$bldgrn already correct$txtgrn (aac), no transcode required for video$txtrst"
TRANSCODEAUDIO=false
else
acodec=$(getACodec "$1")
echo -e "$txtgrn -$bldgrn audio$txtgrn needs to be transcoded from$bldgrn $acodec to aac$txtrst"
TRANSCODEAUDIO=true
fi
if ( ($TRANSCODEVIDEO) || ($TRANSCODEAUDIO) ); then
echo -e "$txtgrn + file needs to be transcoded$txtrst"
TRANSCODE=true
else
echo -e "$txtgrn + file$bldgrn does not need$txtgrn to be transcoded$txtrst"
TRANSCODE=false
continue
fi
OUTPUT="$FOLDER/${BASENAME%.*}.mp4"
if [ -f "$OUTPUT" ]; then
echo -e "$txtylw The output$bldylw file already exists.$txtylw ($OUTPUT)$bldylw Skipping$txtylw this file.$txtrst"
if ($DRYRUN); then
echo -e "$txtylw This is a$bldylw DRYRUN$txtylw so we will continue. Otherwise, we would haves topped here.$txtrst"
else
continue
fi
fi
# obtaining video and audio bitrate to achieve same file size.
VIDEOBITRATE=$(getVBitrate "$FILE")
AUDIOBITRATE=$(getABitrate "$FILE")
if ($TRANSCODEVIDEO); then # we'll take the "long" 2 pass method
echo -e "$txtylw Starting$bldylw First Pass$txtrst"
start_time=`date +%s`
cd "$TEMPDIR"
if ($DRYRUN); then
echo -e "$txtylw This is a$bldylw DRYRUN$txtylw so we will not transcode the file. Otherwise, we would have executed :$txtrst"
echo -e ffmpeg -i \'"$FILE"\' -c:v libx264 -profile:v high -level 5 -preset slow -b:v $VIDEOBITRATE -an -pass 1 \'"$OUTPUT"\' -loglevel fatal
FIRSTPASSSUCCESS=true
else
# Get duration and PAL/NTSC fps then calculate total frames.
FPS=$(getFPS "$FILE")
DUR=$(getDuration "$FILE")
HRS=$(echo $DUR | cut -d":" -f1)
MIN=$(echo $DUR | cut -d":" -f2)
SEC=$(echo $DUR | cut -d":" -f3)
TOT_FR=$(echo "($HRS*3600+$MIN*60+$SEC)*$FPS" | $bc | cut -d"." -f1)
ffmpeg -vstats_file $TEMPDIR/vstats -i "$FILE" -c:v libx264 -profile:v high -level 5 -preset slow -b:v $VIDEOBITRATE -an -pass 1 "$OUTPUT" 2>/dev/null &
PID=$! &&
echo -e "\tPID of ffmpeg = $PID - Duration: $DUR - Frames: $TOT_FR"
display # Show progress.
if [ -f $OUTPUT ] ; then
FIRSTPASSSUCCESS=true
else
FIRSTPASSSUCCESS=false
fi
fi
firstpass_endtime=`date +%s`
firstpass_duration=$(($firstpass_endtime-$start_time))
if ! ($FIRSTPASSSUCCESS) ; then
echo -e "$bldred First pass has failed ! Doing some cleanup and skipping.$txtrst";
if [ -f "$OUTPUT" ]; then
rm -f "$OUTPUT"
fi
echo "$BASENAME ; FIRST pass failed ; $FILE" >> $HOME/transcode_failed.log
continue
fi
echo -e "$bldylw Fist pass completed$txtylw in $(convertsecs $firstpass_duration).$txtrst"
echo -e "$txtylw Starting$bldylw Second Pass$txtrst"
secondpass_starttime=`date +%s`
if ($DRYRUN); then
echo -e "$txtylw This is a$bldylw DRYRUN$txtylw so we will not transcode the file. Otherwise, we would have executed :$txtrst"
echo -e ffmpeg -y -i \'"$FILE"\' -c:v libx264 -profile:v high -level 5 -preset slow -b:v $VIDEOBITRATE -b:a $AUDIOBITRATE -pass 2 \'"$OUTPUT"\' -loglevel fatal
SECONDPASSSUCCESS=true
else
#some cleanup from firstpass :
rm -f $TEMPDIR/vstats
rm -f $TEMPDIR/elapsed.value
# Get duration and PAL/NTSC fps then calculate total frames.
FPS=$(getFPS "$FILE")
DUR=$(getDuration "$FILE")
HRS=$(echo $DUR | cut -d":" -f1)
MIN=$(echo $DUR | cut -d":" -f2)
SEC=$(echo $DUR | cut -d":" -f3)
TOT_FR=$(echo "($HRS*3600+$MIN*60+$SEC)*$FPS" | $bc | cut -d"." -f1)
ffmpeg -vstats_file $TEMPDIR/vstats -y -i "$FILE" -c:v libx264 -profile:v high -level 5 -preset slow -b:v $VIDEOBITRATE -b:a $AUDIOBITRATE -pass 2 "$OUTPUT" 2>/dev/null &
PID=$! &&
echo -e "\tPID of ffmpeg = $PID - Duration: $DUR - Frames: $TOT_FR"
display # Show progress.
if [ -f $OUTPUT ] ; then
SECONDPASSSUCCESS=true
else
SECONDPASSSUCCESS=false
fi
fi
if ! ($SECONDPASSSUCCESS) ; then
echo -e "$bldred Second pass has failed ! Doing some cleanup and skipping.$txtrst";
if [ -f "$OUTPUT" ]; then
rm -f "$OUTPUT"
fi
echo "$BASENAME ; SECOND pass failed ; $FILE" >> $HOME/transcode_failed.log
continue
fi
cd "$CURRENTDIR"
end_time=`date +%s`
secondpass_duration=$(($end_time-$firstpass_endtime))
total_duration=$(($end_time-$start_time))
echo -e "$bldylw Second pass completed$txtylw in $(convertsecs $secondpass_duration).$txtrst"
echo -e "$bldgrn Total transcoding time is $(convertsecs $total_duration)."
TotalNbFileTranscoded=$(($TotalNbFileTranscoded + 1))
sizeoriginal=0
sizetranscoded=0
if [ -f $FILE ] ; then
sizeoriginal=$(getFileSizeMB "$FILE")
if [ -f $OUTPUT ] ; then
sizetranscoded=$(getFileSizeMB "$OUTPUT")
else
sizeoriginal=0
sizetranscoded=0
fi
else
sizeoriginal=0
sizetranscoded=0
fi
TotalSizeInflation=$(($sizetranscoded - $sizeoriginal))
TotalSizeTranscoded=$(($TotalSizeTranscoded + $sizeoriginal))
else # video does not need to be transcoded in this else
echo -e "$bldylw Doing a single pass since video does not need to be transcoded.$txtrst"
start_time=`date +%s`
if ($DRYRUN); then
echo -e "$txtylw This is a$bldylw DRYRUN$txtylw so we will not transcode the file. Otherwise, we would have executed :$txtrst"
echo -e ffmpeg -y -i \'"$FILE"\' -c:v copy -level 5 -preset slow -b:a $AUDIOBITRATE -pass 2 \'"$OUTPUT"\' -loglevel fatal
SINGLEPASSSUCCESS=true
else
FPS=$(getFPS "$FILE")
DUR=$(getDuration "$FILE")
HRS=$(echo $DUR | cut -d":" -f1)
MIN=$(echo $DUR | cut -d":" -f2)
SEC=$(echo $DUR | cut -d":" -f3)
TOT_FR=$(echo "($HRS*3600+$MIN*60+$SEC)*$FPS" | $bc | cut -d"." -f1)
ffmpeg -y -i "$FILE" -c:v copy -level 5 -preset slow -b:a $AUDIOBITRATE "$OUTPUT" 2>$TEMPDIR/vstats &
PID=$! &&
echo -e "\tPID of ffmpeg = $PID - Duration: $DUR - Frames: $TOT_FR"
display # Show progress.
if [ -f $OUTPUT ] ; then
SINGLEPASSSUCCESS=true
else
SINGLEPASSSUCCESS=false
fi
fi
if ! ($SINGLEPASSSUCCESS) ; then
echo -e "$bldred Single (first) pass has failed ! Doing some cleanup and skipping.$txtrst";
if [ -f "$OUTPUT" ]; then
rm -f "$OUTPUT"
fi
echo "$BASENAME ; SINGLE first pass failed ; $FILE" >> $HOME/transcode_failed.log
continue
fi
end_time=`date +%s`
total_duration=$(($end_time-$start_time))
echo -e "$bldgrn Total (single pass) transcoding time is $(convertsecs $total_duration)."
TotalNbFileTranscoded=$(($TotalNbFileTranscoded + 1))
sizeoriginal=$(getFileSizeMB "$FILE")
if ($DRYRUN); then
sizetranscoded=$sizeoriginal
else
sizetranscoded=$(getFileSizeMB "$OUTPUT")
fi
TotalSizeInflation=$(($sizetranscoded - $sizeoriginal))
TotalSizeTranscoded=$(($TotalSizeTranscoded + $sizeoriginal))
fi
if [ -f "$OUTPUT" ]; then
echo "# $BASENAME ; File Has been Transcoded. New file is : ; $OUTPUT" >> $HOME/transcode_success.log
# Delete old file if autodelete is enabled
if ($AUTODELETE); then
if [ -f "$FILE" ]; then
rm -f "$FILE"
echo -e "$bldlbl Old file has been deleted ($bldgrn$FILE$bldlbl) due to autodelete beeing on.$txtrst"
fi
else # we don't want to delete the old file, so build a script to check the new file and offer to delete the old one.
echo "echo -e \"$bldwht =========================================================\" " >> $HOME/check_and_remove.sh
echo "echo -e \"$txtrst File successfully transcoded : \" " >> $HOME/check_and_remove.sh
echo "echo -e \" File : $bldgrn$BASENAME$txtrst\" " >> $HOME/check_and_remove.sh
echo "echo -e \" Orifinal file : $bldgrn$FILE$txtrst\" " >> $HOME/check_and_remove.sh
echo "echo -e \" New File : $bldgrn$OUTPUT$txtrst\" " >> $HOME/check_and_remove.sh
echo "echo -e \"$bldlbl Launching playback of the transcoded file ($bldgrn$OUTPUT$bldlbl)$txtrst\" " >> $HOME/check_and_remove.sh
echo "mplayer -really-quiet \"$OUTPUT\" " >> $HOME/check_and_remove.sh
echo "del=\"\" " >> $HOME/check_and_remove.sh
echo "while true ; do " >> $HOME/check_and_remove.sh
echo " if ([[ \"\$del\" == \"y\" ]] || [[ \"\$del\" == \"n\" ]]); then " >> $HOME/check_and_remove.sh
echo " break " >> $HOME/check_and_remove.sh
echo " else " >> $HOME/check_and_remove.sh
echo " echo -e \"$txtylw Can we$bldylw delete the original$txtylw file ?$bldylw (y|n)$bldgrn$bakred \c\"" >> $HOME/check_and_remove.sh
echo " read -n 1 del " >> $HOME/check_and_remove.sh
echo " fi " >> $HOME/check_and_remove.sh
echo "done " >> $HOME/check_and_remove.sh
echo "if [[ \"\$del\" == \"y\" ]] ; then " >> $HOME/check_and_remove.sh
echo " rm -f \"$FILE\" " >> $HOME/check_and_remove.sh
echo " echo -e \"$txtrst\\n$bldred Original file has been deleted ($bldgrn$FILE$bldred)$txtrst\"" >> $HOME/check_and_remove.sh
echo "else " >> $HOME/check_and_remove.sh
echo " dell=\"\" " >> $HOME/check_and_remove.sh
echo " while true ; do " >> $HOME/check_and_remove.sh
echo " if ([[ \"\$dell\" == \"y\" ]] || [[ \"\$dell\" == \"n\" ]]); then " >> $HOME/check_and_remove.sh
echo " break " >> $HOME/check_and_remove.sh
echo " else " >> $HOME/check_and_remove.sh
echo " echo -e \"$txtrst\\n$txtcyn Since you want to keep the old file, should we$bldcyn delete the new$txtcyn (badly?) transcoded file ?$bldcyn (y|n)$bldgrn$bakred \c\"" >> $HOME/check_and_remove.sh
echo " read -n 1 dell " >> $HOME/check_and_remove.sh
echo " fi " >> $HOME/check_and_remove.sh
echo " done " >> $HOME/check_and_remove.sh
echo " if [[ \"\$dell\" == \"y\" ]] ; then " >> $HOME/check_and_remove.sh
echo " rm -f \"$OUTPUT\" " >> $HOME/check_and_remove.sh
echo " echo -e \"$txtrst\\n$bldred New transcoded file has been deleted ($bldgrn$OUTPUT$bldred)$txtrst\"" >> $HOME/check_and_remove.sh
echo " else " >> $HOME/check_and_remove.sh
echo " echo -e \"$txtrst\\n$bldlbl No file has been deleted.$txtrst\" " >> $HOME/check_and_remove.sh
echo " fi " >> $HOME/check_and_remove.sh
echo "fi " >> $HOME/check_and_remove.sh
echo "echo \" =========================================================\" " >> $HOME/check_and_remove.sh
# echo "# $BASENAME ; File Has been Transcoded. Check and delete the old file : ; $FILE\" " >> $HOME/check_and_remove.sh
fi # $AUTODELETE
else # $OUTPUT does not exist in this else
echo -e "$bldred !!! FAILED to transcode the video !!! (output file were not found)$txtrst"
echo "$BASENAME ; Failed to transcode the video (output not found after transcoding) ; $FILE" >> $HOME/transcode_failed.log
fi # $OUTPUT EXISTS
cleanup_temp
}
print_help() {
echo -e "$bldlbl Usage: $txtred chromecastize.sh$txtylw [--dryrun] [--showignored] [--autodelete] [--cleanhome] $txtred<videofile1> $txtylw[ videofile2 ... ]$txtrst"
}
################
# MAIN PROGRAM #
################
# test if `mediainfo` is available
mediainfo=`which mediainfo`
if [ -z $mediainfo ]; then
mediainfo=/var/packages/mediainfo/target/bin/mediainfo
if (! [ -f $mediainfo ]); then
echo -e "$bldred mediainfo$txtred is not available, please install it"
exit 1
fi
fi
# test if `mediainfo` is available
bc=`which bc`
if [ -z $bc ]; then
bc=/opt/bin/bc
if (! [ -f $bc ]); then
echo -e "$bldred bc$txtred is not available, please install it"
exit 1
fi
fi
# test if `ffmpeg` is available
FFMPEG=`which avconv || which ffmpeg`
if [ -z $FFMPEG ]; then
echo -e "$bldred avconv$txtred (or$bldred ffmpeg$txtred) is not available, please install it"
exit 1
fi
# test if `grealpath` or `realpath` is available
REALPATH=`which realpath || which grealpath`
if [ -z $REALPATH ]; then
echo -e "$bldred grealpath$txtred (or$bldred realpath$txtred) is not available, please install it"
exit 1
fi
# check number of arguments
if [ $# -lt 1 ]; then
print_help
exit 1
fi
for FILENAME in "$@"; do
if [ "$FILENAME" = "--dryrun" ]; then
DRYRUN=true
elif [ "$FILENAME" = "--showignored" ]; then
SHOWIGNORED=true
elif [ "$FILENAME" = "--autodelete" ]; then
AUTODELETE=true
elif [ "$FILENAME" = "--cleanhome" ]; then
echo "" > $HOME/transcode_failed.log
echo "" > $HOME/transcode_success.log
echo "" > $HOME/check_and_remove.sh
elif ! [ -e "$FILENAME" ]; then
echo -e "$bldred File not found ($bldgrn$FILENAME$bldred). Skipping...$txtrst"
elif [ -d "$FILENAME" ]; then
ENTRIES=$(find "$FILENAME" -type f)
for ENTRY in $ENTRIES ; do
process_file $ENTRY
done
elif [ -f "$FILENAME" ]; then
process_file $FILENAME
else
echo -e "$bldredInvalid file ($bldgrn$FILENAME$bldred). Skipping...$txtrst"
fi
done
SCRIPT_END_TIME=`date +%s`
SCRIPT_DURATION=$(($SCRIPT_END_TIME-$SCRIPT_START_TIME))
echo -e "$bldgrn Script completed in in $(convertsecs $SCRIPT_DURATION)$bldgrn.$txtrst"
echo -e "$bldgrn The script has transcoded $bldred$TotalNbFileTranscoded$bldgrn files for a total of $bldred$TotalSizeTranscoded MB$bldgrn with $bldred$TotalSizeInflation MB$bldgrn inflation.$txtrst"
IFS=$CURRENTIFS