-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode_videos.sh
executable file
·95 lines (75 loc) · 1.6 KB
/
encode_videos.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
#!/bin/sh
#
# Encodes the video files with FFmpeg.
#
# Arguments:
# $1 - source directory
#
# Dependencies:
# ffmpeg version 4.0.2
#
IFS0=$IFS
IFS=$'\n'
if [[ $1 == "" || ! -d $1 ]]
then
echo "Directory '$1' doesn't exist!"
exit 1
fi
VIDEO_WIDTH=1280
VIDEO_HEIGHT=720
VIDEO_ROTATION=90
VIDEO_EXTENSION=.mp4
for src in $1/*; do
if [ ! -f $src ]
then
echo "File '$src' doesn't exist!"
continue
fi
WIDTH=$VIDEO_WIDTH
HEIGHT=$VIDEO_HEIGHT
res=$( ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 $src )
res=( $( echo $res | grep -E -o '(\d+)' ) )
width=${res[0]}
height=${res[1]}
rot=$( ffprobe -v error -select_streams v:0 -show_entries stream_tags=rotate -of csv=s=x:p=0 $src )
if [[ $rot -eq $VIDEO_ROTATION ]]
then
tmp=$width
width=$height
height=$tmp
tmp=$WIDTH
WIDTH=$HEIGHT
HEIGHT=$tmp
fi
if [[ $width -gt $WIDTH ]]
then
width=$WIDTH
fi
if [[ $height -gt $HEIGHT ]]
then
height=$HEIGHT
fi
dst0=${src%.*}
dst=$1/${dst0##*/}_E$VIDEO_EXTENSION
if [ -f $dst ]
then
dst_old=$dst
idx=1
while true
do
dst=$1/${dst0##*/}_E_"$idx"$VIDEO_EXTENSION
if [ ! -f $dst ]
then
break
fi
((idx++))
done
fi
ffmpeg -i $src \
-vcodec libx264 \
-acodec aac \
-vf scale=$width:$height \
-hide_banner \
$dst
done
IFS=$IFS0