-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary2video
More file actions
executable file
·126 lines (112 loc) · 3.49 KB
/
binary2video
File metadata and controls
executable file
·126 lines (112 loc) · 3.49 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
#!/usr/bin/env bash
usage_and_exit () {
echo "Usage: binary2video [-f fps] [-w width] [-h height] [-c (vp9 | h264 | ffv1 | ffv1l3)] [-v] infile outfile"
echo " Defaults: fps=1 width=320 height=240 codec=vp9"
exit $1
}
check_executable_or_exit() {
if [ ! -x $1 ]; then
echo "error: no $(basename $1) at $gzip"
exit 2
fi
}
if [ \( $# -eq 1 \) -a \( "$1" = '--help' \) ]; then
usage_and_exit 0
fi
# check needed binaries
gzip=/usr/bin/gzip
check_executable_or_exit $gzip
ffmpeg=/usr/bin/ffmpeg
check_executable_or_exit $ffmpeg
# check parameters
width=320
height=240
framerate=1
codec=vp9
verbose=0
while getopts 'f:w:h:c:v' option; do
case "$option" in
f) framerate="$OPTARG";;
w) width="$OPTARG";;
h) height="$OPTARG";;
c) codec="$OPTARG";;
v) verbose=1;;
?) usage_and_exit 1;;
esac
done
shift $(($OPTIND - 1))
# verify codec
case "$codec" in
h264) ;;
vp9) ;;
ffv1) ;;
ffv1l3) ;;
*)
echo "error: unknown codec '$codec'"
usage_and_exit 3
;;
esac
if [ $# != 2 ]; then
usage_and_exit 1
fi
if [ ! -r "$1" ]; then
echo "error: file '$1' does not exist or is not readable"
exit 4
fi
if [ $verbose = 1 ]; then
echo "parameters: fps=$framerate width=$width height=$height codec=$codec"
fi
tmpfile_gzipped=$(mktemp --tmpdir binary2video.gz.XXXXXXXXXX)
tmpfile_padded=$(mktemp --tmpdir binary2video.padded.XXXXXXXXXX)
infile="$1"
outfile="$2"
pixel_format=rgb24
blocksize=$(($width * $height * 3))
if [ $verbose = 1 ]; then
echo "blocksize is $blocksize bytes"
echo -n "input file MD5: "
md5sum "${infile}" | cut -d\ -f1
fi
$gzip --fast --to-stdout "${infile}" > ${tmpfile_gzipped}
size_gzipped=$(stat --format=%s ${tmpfile_gzipped})
if [ $verbose = 1 ]; then
echo "gzipped file has $size_gzipped bytes"
fi
padding_needed=$(($blocksize - ($size_gzipped % $blocksize)))
if [ $verbose = 1 ]; then
echo "padding needed: $padding_needed bytes"
fi
(cat ${tmpfile_gzipped}; dd if=/dev/zero bs=$padding_needed count=1 status=none) >> ${tmpfile_padded}
size_padded=$(stat --format=%s ${tmpfile_padded})
if [ $verbose = 1 ]; then
echo "padded file has $size_padded bytes"
fi
if [ $codec = "vp9" ]; then
$ffmpeg -y -hide_banner -loglevel error \
-f rawvideo -pixel_format $pixel_format -video_size "${width}x${height}" -framerate ${framerate} -i "${tmpfile_padded}" \
-c:v libvpx-vp9 -lossless 1 \
"${outfile}"
elif [ $codec = "h264" ]; then
$ffmpeg -y -hide_banner -loglevel error \
-f rawvideo -pixel_format $pixel_format -video_size "${width}x${height}" -framerate ${framerate} -i "${tmpfile_padded}" \
-c:v libx264rgb -preset ultrafast -qp 0 \
"${outfile}"
elif [ $codec = "ffv1" ]; then
$ffmpeg -y -hide_banner -loglevel error \
-f rawvideo -pixel_format $pixel_format -video_size "${width}x${height}" -framerate ${framerate} -i "${tmpfile_padded}" \
-c:v ffv1 -level 1 -coder 1 -context 1 -g 1 \
"${outfile}"
elif [ $codec = "ffv1l3" ]; then
$ffmpeg -y -hide_banner -loglevel error \
-f rawvideo -pixel_format $pixel_format -video_size "${width}x${height}" -framerate ${framerate} -i "${tmpfile_padded}" \
-c:v ffv1 -level 3 -threads 8 -coder 1 -context 1 -g 1 -slices 24 -slicecrc 1 \
"${outfile}"
else
echo "error: unknown codec '$codec'"
usage_and_exit 5
fi
if [ $verbose = 1 ]; then
echo "output file has $(stat --format=%s "${outfile}") bytes"
fi
rm ${tmpfile_gzipped}
rm ${tmpfile_padded}