-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcreate_test_video.sh
executable file
·170 lines (131 loc) · 5.1 KB
/
create_test_video.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
#!/bin/sh
#
# Copyright 2015 British Broadcasting Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# --------------------------------------------------------------------------
# this script shows how to run the sequence generator and then encode the
# resulting audio and video frame images into MP4, MPEG TS and MPEG PS files
# Temporary image files are put into a directory whose name begins "build-tmp"
# the metadata describing flash/beep timings and the video files are put
# into the current directory
# tested with ffmpeg 2.5.1
#
# known to NOT work with avconv from ubuntu 14.04
FFMPEG=`which ffmpeg avconv | head -n 1`
if [ -z "$FFMPEG" ]; then
echo "Need to have ffmpeg or avconv installed and in the path to run." >&2
exit 1
fi
# --------------------------------------------------------------------------
# parameters
FPS=50
WINDOW_LEN=7
SIZE=854x480
## alternative parameters for a quick-to-create video
#FPS=25
#WINDOW_LEN=3
#SIZE=320x180
# --------------------------------------------------------------------------
# define file and directory names based on parameters
NAME_FRAGMENT="$FPS"fps_"$WINDOW_LEN"bitseq_"$SIZE"pixels
TMPDIR="build/tmp.""$NAME_FRAGMENT"
OUTDIR="build"
METADATA_FILE="$OUTDIR""/metadata.""$NAME_FRAGMENT"".json"
VIDEO_FILE="$OUTDIR""/video.""$NAME_FRAGMENT"
FRAME_FILE_PATTERN="$TMPDIR"/"img_%09d.png"
WAV_FILE="$TMPDIR"/"audio.wav"
# --------------------------------------------------------------------------
# create directories for temp files
mkdir -p "$TMPDIR"
mkdir -p "$OUTDIR"
rm -f "$TMPDIR"/*.png
rm -r "$TMPDIR"/*.wav
# --------------------------------------------------------------------------
# run the sequence generator
./src/generate.py \
--fps "$FPS" --size "$SIZE" --window-len "$WINDOW_LEN" \
--frame-filename "$FRAME_FILE_PATTERN" \
--wav-filename "$WAV_FILE" \
--metadata-filename "$METADATA_FILE" \
if [ $? -ne 0 ]; then
echo "Error during creation of wav/png/metadata. Aborting" >&2
exit 2
fi
# --------------------------------------------------------------------------
# encode using ffmpeg
# MP4 using H264 and AAC audio
# NOTE: by default, FFMPEG (as available for Ubuntu 14.04) appears to introduce
# a 21.333ms delay to the audio (relative to the video) when writing
# to an MP4 container.
#
# The 'itsoffset' option and the 'asyncts' audio filter work together to
# compensate for this by offsetting the audio and ensuring samples at the
# beginning are trimmed. This ensures correct alignment.
$FFMPEG -y \
-r "$FPS" -i "$FRAME_FILE_PATTERN" -f wav -itsoffset -0.0213333 -i "$WAV_FILE" \
-b 2M -r "$FPS" -vcodec libx264 -vf "fps=$FPS,format=yuv420p" \
-preset:v slow -profile:v baseline -level 3.1 -crf 23 \
-af asyncts=min_delta=0:compensate=1:first_pts=0 \
-ac 2 -ar 48k -ab 128k -acodec aac -strict -2 \
-map 0:v:0 -map 1:a:0 \
"$VIDEO_FILE".mp4
if [ $? -ne 0 ]; then
echo "Failure when creating MP4. Aborting" >&2
exit 3
fi
# MPEG TRANSPORT STREAM and MPEG PROGRAM STREAM
# ... using MPEG2 video and mp2 audio
# we carefully control the PTS value of the first frame.
# note that FFMPEG needs it to be greater than zero because the decode timestamp
# is a smaller value and FFMPEG cannot cope with the idea of a negative DTS (!)
START_PTS_IN_SECONDS=10.0 # PTS will be this * 90000
PTS_TB=1/90000
VPTS_FUNC="N/(TB*FRAME_RATE) + ""$START_PTS_IN_SECONDS""/TB"
APTS_FUNC="N/(TB*SAMPLE_RATE) + ""$START_PTS_IN_SECONDS""/TB"
$FFMPEG -y \
-r "$FPS" -i "$FRAME_FILE_PATTERN" -f wav -i "$WAV_FILE" \
-b 2M -r "$FPS" -vcodec mpeg2video -vf settb="$PTS_TB" -vf setpts="$VPTS_FUNC" \
-ac 2 -ar 48k -ab 128k -acodec mp2 -af asettb="$PTS_TB" -af asetpts="$APTS_FUNC" \
-map 0:v:0 -map 1:a:0 \
-mpegts_copyts 1 \
"$VIDEO_FILE".ts
if [ $? -ne 0 ]; then
echo "Failure when creating MPEG TS. Aborting" >&2
exit 4
fi
$FFMPEG -y \
-r "$FPS" -i "$FRAME_FILE_PATTERN" -f wav -i "$WAV_FILE" \
-b 2M -r "$FPS" -vcodec mpeg2video -vf settb="$PTS_TB" -vf setpts="$VPTS_FUNC" \
-ac 2 -ar 48k -ab 128k -acodec mp2 -af asettb="$PTS_TB" -af asetpts="$APTS_FUNC" \
-map 0:v:0 -map 1:a:0 \
"$VIDEO_FILE".mpg
if [ $? -ne 0 ]; then
echo "Failure when creating MPEG PS. Aborting" >&2
exit 5
fi
echo ""
echo "--------------------------------------------------------------------------"
echo ""
echo "Completed successfully!"
echo ""
echo " Intermediate image and audio files in: $TMPDIR/"
echo " Video files:"
echo " $VIDEO_FILE.mp4"
echo " $VIDEO_FILE.ts"
echo " $VIDEO_FILE.mpg"
echo " Metadata file:"
echo " $METADATA_FILE"
echo ""
echo "--------------------------------------------------------------------------"
echo ""