-
Notifications
You must be signed in to change notification settings - Fork 2
/
ff
executable file
·63 lines (53 loc) · 1.23 KB
/
ff
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
#!/bin/bash -e
usage() {
cat <<EOF
Convert input to video suitable for TikTok
Synopsis:
$(basename $0) [options] input.mp4 [output.mp4]
Options:
-p Pan camera left/right (defaults to 0)
-q Quality from 0 to 50 (defaults to 17)
-r Aspect ratio of output (defaults to 9/16)
-s Speed of transcoding (defaults to ultrafast, results in larger file size)
EOF
exit 1
}
pan=0
quality=17
ratio=9/16
speed=ultrafast
# Parse options
while getopts "p:q:r:s:" opt; do
case $opt in
p) pan="$OPTARG";;
q) quality="$OPTARG";;
r) ratio="$OPTARG";;
s) speed="$OPTARG";;
*) usage;;
esac
done
shift "$((OPTIND-1))"
if [[ -z "$1" ]]
then
usage
fi
input="$1"
output="${2:-out.mp4}"
out_w=2160
out_h="$((out_w * ${ratio#*/}/${ratio%/*}))"
crop="min(in_h*$ratio\,in_w):in_h:0.5*in_w-0.5*out_w+$pan:0"
scale="$out_w:max($out_h\,in_h*($out_w/in_w))"
pad="ceil($out_w/2)*2:ceil(max($out_h/2\,in_h*($out_w/in_w)/2))*2"
# Call ffmpeg
ffmpeg \
-i "$input" \
-c:v libx264 \
-preset "$speed" \
-crf "$quality" \
-c:a aac \
-filter:v "crop=$crop,scale=$scale,pad=$pad,format=yuv420p" \
-movflags +faststart \
-r 30 \
"$output"
# Loop the video
ffplay -loop 0 "$output"