-
Notifications
You must be signed in to change notification settings - Fork 0
/
Encode.sh
executable file
·140 lines (114 loc) · 2.73 KB
/
Encode.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
#!/bin/bash
# Video parameters
PRESET="slower"
PROFILE="high"
LEVEL="42"
RESOLUTION="1080"
BITRATE="8192k"
PIXELFORMAT="yuvj420p"
# Audio parameters
FREQUENCY="48"
AUDIOBITRATE="128"
STRATEGY="VBRC"
STRATEGY_NUM="2"
CHANNELS="2"
case $CHANNELS in
1)
LAYOUT="Mono" ;;
2)
LAYOUT="Stereo" ;;
4)
LAYOUT="Quadraphonic" ;;
*)
echo -e "\x1B[00;31mError: Strange number of channels.\x1B[00m"
exit 1
esac
function cleanup()
{
echo ""
echo -e "\x1B[00;32mRemoving temporary files.\x1B[00m"
# Remove temporary directory
[ -d tmp ] && rm -rf tmp
# Remove FFMPEG temporary files
ls ffmpeg2* &> /dev/null && rm -rf ffmpeg2*
}
# Always remove temporary files on exit
trap cleanup EXIT
# Create temporary and output directories
[ -d tmp ] || mkdir tmp
[ -d MP4 ] || mkdir MP4
for INPUT in $@
do
NAME=${INPUT%.*}
VIDEO="tmp/${NAME}.mp4"
AUDIOSOURCE="tmp/${NAME}.wav"
AUDIO="tmp/${NAME}.m4a"
OUTPUT="MP4/${NAME}.mp4"
OUTPUT_FASTSTART="MP4/${NAME}_faststart.mp4"
echo -e ""
echo -e "\x1B[00;32mEncoding ${INPUT}\x1B[00m" >&2
# Encode video
ffmpeg -i $INPUT -c:v libx264 \
-preset $PRESET -profile:v $PROFILE -b:v $BITRATE -level:v $LEVEL \
-refs 6 \
-pix_fmt $PIXELFORMAT \
-an \
-pass 1 -threads 4 -y $VIDEO
if [ $? -ne 0 ]; then
echo -e "\x1B[00;31mError: Failed to encode video.\x1B[00m" >&2
exit 1
fi
ffmpeg -i $INPUT -c:v libx264 \
-preset $PRESET -profile:v $PROFILE -b:v $BITRATE -level:v $LEVEL \
-refs 6 \
-pix_fmt $PIXELFORMAT \
-an \
-pass 2 -threads 4 -y $VIDEO
if [ $? -ne 0 ]; then
echo -e "\x1B[00;31mError: Failed to encode video.\x1B[00m" >&2
exit 1
fi
# Make audio source
ffmpeg -i $INPUT \
-c:a pcm_s16le -vn -y \
-ac $CHANNELS \
$AUDIOSOURCE
if [ $? -ne 0 ]; then
echo -e "\x1B[00;31mError: Failed to convert audio to lossless source.\x1B[00m" >&2
exit 1
fi
# Encode audio
afconvert -v \
-f m4af -q 127 \
-d aac@${FREQUENCY}'000' \
-b ${AUDIOBITRATE}'000' \
-s $STRATEGY_NUM \
-c $CHANNELS \
-l $LAYOUT \
$AUDIOSOURCE \
$AUDIO
if [ $? -ne 0 ]; then
echo -e "\x1B[00;31mError: Failed to encode audio.\x1B[00m" >&2
exit 1
fi
# Mux audio and video
ffmpeg \
-i $VIDEO \
-i $AUDIO \
-map 0:0 -map 1:0 \
-c:v copy -c:a copy \
-y \
$OUTPUT
if [ $? -ne 0 ]; then
echo -e "\x1B[00;31mError: Failed to multiplex audio and video.\x1B[00m" >&2
exit 1
fi
# Run faststart to put the ATOM at the beginning for streaming
qt-faststart $OUTPUT $OUTPUT_FASTSTART
if [ $? -ne 0 ]; then
echo -e "\x1B[00;31mError: Failed to move ATOM to beginning of file.\x1B[00m" >&2
exit 1
fi
mv $OUTPUT_FASTSTART $OUTPUT || exit 1
done
exit 0