-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-mediafiles.sh
More file actions
executable file
·230 lines (208 loc) · 7.7 KB
/
generate-mediafiles.sh
File metadata and controls
executable file
·230 lines (208 loc) · 7.7 KB
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
#!/bin/sh
##################################################################################
# DEFAULT VALUES
##################################################################################
VIDEO_SAMPLE_URL=https://archive.org/download/e-dv548_lwe08_christa_casebeer_003.ogg/e-dv548_lwe08_christa_casebeer_003.mp4
WIDTH=640
HEIGHT=480
VIDEO_DURATION=00:00:30
PADDING_DURATION_SEC=5
AUDIO_SAMPLE_RATE_HZ=48000
TONE_FREQUENCY_HZ=1000
AUDIO_CHANNELS_NUMBER=2
FFMPEG_LOG="-loglevel error"
TARGET_VIDEO=./test.y4m
TARGET_AUDIO=./test.wav
GENERATE_DEFAULT_REF=false
DEFAULT_VIDEO_REF=./test-no-padding.yuv
DEFAULT_AUDIO_REF=./test-no-padding.wav
FONT=./mitramono.ttf
CLEANUP=true
GET_ORIGINAL_STATS=false
USAGE="Usage: `basename $0` [-d=duration] [-p=padding_duration_sec] [--game] [--generate_default_ref] [--no_cleanup] [--clean] [-v=video_url] [-w=width] [-h=height] [-a=audio_url] [-f=fps] [--original]"
##################################################################################
# FUNCTIONS
##################################################################################
cleanup() {
echo "Deleting temporal files"
rm -rf test-no-frame-number.mp4 test-no-padding.mp4 padding.mp4 test.mp4 test-mixed.mp4
}
##################################################################################
# PARSE ARGUMENTS
##################################################################################
for i in "$@"; do
case $i in
--game)
VIDEO_SAMPLE_URL=https://ia802808.us.archive.org/6/items/ForniteBattle8/fornite%20battle%202.mp4
WIDTH=1280
HEIGHT=720
shift
;;
--generate_default_ref)
GENERATE_DEFAULT_REF=true
shift
;;
-d=*|--duration=*)
VIDEO_DURATION="${i#*=}"
shift
;;
-p=*|--padding=*)
PADDING_DURATION_SEC="${i#*=}"
shift
;;
--no_cleanup)
CLEANUP=false
shift
;;
--clean)
cleanup
exit 0
shift
;;
-v=*)
VIDEO_SAMPLE_URL="${i#*=}"
shift
;;
-w=*)
WIDTH="${i#*=}"
shift
;;
-h=*)
HEIGHT="${i#*=}"
shift
;;
-a=*)
AUDIO_SAMPLE_URL="${i#*=}"
shift
;;
-f=*)
FPS="${i#*=}"
shift
;;
--original)
GET_ORIGINAL_STATS=true
shift
;;
*) # unknown option
echo $USAGE
exit 0
;;
esac
done
##################################################################################
# INIT
##################################################################################
##########################
# 1. Download video sample
##########################
VIDEO_SAMPLE_NAME=$(echo ${VIDEO_SAMPLE_URL##*/} | sed -e 's/%20/ /g')
if [ ! -f "$VIDEO_SAMPLE_NAME" ]; then
echo "Content video ($VIDEO_SAMPLE_NAME) not exists ... downloading"
curl -O $VIDEO_SAMPLE_URL
if [ "${VIDEO_SAMPLE_NAME##*.}" = "zip" ]; then
echo "Unzipping video file ($VIDEO_SAMPLE_NAME)"
unzip "$VIDEO_SAMPLE_NAME"
VIDEO_SAMPLE_NAME=$(unzip -Z1 "$VIDEO_SAMPLE_NAME" | head -n 1)
fi
else
if [ "${VIDEO_SAMPLE_NAME##*.}" = "zip" ]; then
UNZIPPED_VIDEO_NAME=$(unzip -Z1 "$VIDEO_SAMPLE_NAME" | head -n 1)
if [ ! -f "$UNZIPPED_VIDEO_NAME" ]; then
echo "Content video ($UNZIPPED_VIDEO_NAME) not exists ... unzipping"
unzip "$VIDEO_SAMPLE_NAME"
VIDEO_SAMPLE_NAME=$UNZIPPED_VIDEO_NAME
else
echo "Content video ($UNZIPPED_VIDEO_NAME) already exists"
VIDEO_SAMPLE_NAME=$UNZIPPED_VIDEO_NAME
fi
else
echo "Content video ($VIDEO_SAMPLE_NAME) already exists"
fi
fi
if [ -z "$FPS" ]; then
FPS=$(ffprobe -v error -select_streams v -show_entries stream=r_frame_rate -of default=noprint_wrappers=1:nokey=1 "$VIDEO_SAMPLE_NAME")
fi
####################################
# 1.1. Get original stats (optional)
####################################
if [ "$GET_ORIGINAL_STATS" = true ]; then
WIDTH=$(ffprobe -v error -show_entries stream=width -of default=noprint_wrappers=1:nokey=1 "$VIDEO_SAMPLE_NAME")
HEIGHT=$(ffprobe -v error -show_entries stream=height -of default=noprint_wrappers=1:nokey=1 "$VIDEO_SAMPLE_NAME")
VIDEO_DURATION=$(ffprobe -v error -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 -sexagesimal "$VIDEO_SAMPLE_NAME")
OUTPUT="$VIDEO_SAMPLE_NAME"
fi
#######################
# 2. Cut original video
#######################
if [ "$GET_ORIGINAL_STATS" = false ]; then
echo "Cutting original video (duration $VIDEO_DURATION)"
ffmpeg $FFMPEG_LOG -y -i "$VIDEO_SAMPLE_NAME" -ss 00:00:00 -t $VIDEO_DURATION -vf scale="$WIDTH:$HEIGHT",setsar=1:1 -r $FPS test-no-frame-number.mp4
OUTPUT="test-no-frame-number.mp4"
fi
#####################################
# 2.1. Mix video and audio (optional)
#####################################
if [ -n "$AUDIO_SAMPLE_URL" ]; then
echo "External audio source found, downloading and mixing"
AUDIO_SAMPLE_NAME=$(echo ${AUDIO_SAMPLE_URL##*/} | sed -e 's/%20/ /g')
if [ ! -f "$AUDIO_SAMPLE_NAME" ]; then
echo "Content audio ($AUDIO_SAMPLE_NAME) not exits ... downloading"
wget $AUDIO_SAMPLE_URL
else
echo "Content audio ($AUDIO_SAMPLE_NAME) already exits"
fi
echo "Mixing audio and video"
FINAL_OUTPUT=test-mixed.mp4
ffmpeg $FFMPEG_LOG -i $OUTPUT -i $AUDIO_SAMPLE_NAME -c:v copy -c:a aac $FINAL_OUTPUT
else
FINAL_OUTPUT=$OUTPUT
fi
#########################
# 3. Overlay frame number
#########################
echo "Overlaying frame number in the video content"
ffmpeg $FFMPEG_LOG -y -i $FINAL_OUTPUT -vf drawtext="fontfile=$FONT:text='\ %{frame_num} \ ':start_number=1:x=(w-tw)/2:y=h-(2*lh):fontcolor=black:fontsize=32:box=1:boxcolor=white:boxborderw=4" -c:a aac -ac 2 test-no-padding.mp4
#################################################
# 4. Create padding video based on a test pattern
#################################################
if [ "$PADDING_DURATION_SEC" -gt 0 ]; then
echo "Creating padding video ($PADDING_DURATION_SEC seconds)"
ffmpeg $FFMPEG_LOG -y -f lavfi -i testsrc=duration=$PADDING_DURATION_SEC:size="$WIDTH"x"$HEIGHT":rate=$FPS -f lavfi -i sine=frequency=$TONE_FREQUENCY_HZ:duration=$PADDING_DURATION_SEC padding.mp4
else
echo "Skipping padding video creation as duration is 0"
fi
############################
# 5. Concatenate final video
############################
if [ "$PADDING_DURATION_SEC" -gt 0 ]; then
echo "Concatenating padding and content videos"
ffmpeg $FFMPEG_LOG -y -i padding.mp4 -i test-no-padding.mp4 -i padding.mp4 -filter_complex concat=n=3:v=1:a=1 test.mp4
else
echo "Skipping concatenation as padding duration is 0"
cp test-no-padding.mp4 test.mp4
fi
#########################
# 6. Convert video to Y4M
#########################
echo "Converting resulting video to Y4M ($TARGET_VIDEO)"
ffmpeg $FFMPEG_LOG -y -i test.mp4 -pix_fmt yuv420p $TARGET_VIDEO
#########################
# 7. Convert audio to WAV
#########################
echo "Converting resulting audio to WAV ($TARGET_AUDIO)"
ffmpeg $FFMPEG_LOG -y -i test.mp4 -vn -acodec pcm_s16le -ar $AUDIO_SAMPLE_RATE_HZ -ac $AUDIO_CHANNELS_NUMBER $TARGET_AUDIO
###############################
# 8. Generate default reference
###############################
if $GENERATE_DEFAULT_REF; then
echo "Generating default video reference ($DEFAULT_VIDEO_REF)"
ffmpeg $FFMPEG_LOG -y -i test-no-padding.mp4 -pix_fmt yuv420p -c:v rawvideo -an $DEFAULT_VIDEO_REF
echo "Generating default audio reference ($DEFAULT_AUDIO_REF)"
ffmpeg $FFMPEG_LOG -y -i test-no-padding.mp4 -vn -acodec pcm_s16le -ar $AUDIO_SAMPLE_RATE_HZ -ac $AUDIO_CHANNELS_NUMBER $DEFAULT_AUDIO_REF
fi
################################
# 9. Delete temporal video files
################################
if $CLEANUP; then
cleanup
fi