-
Notifications
You must be signed in to change notification settings - Fork 1
/
svg2mp4.sh
executable file
·84 lines (73 loc) · 1.76 KB
/
svg2mp4.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
#!/usr/bin/env bash
set -euo pipefail
usage() {
echo "Usage: $0 [opts] <filename>
Options:
-w <WIDTH>: Horizontal resolution
-h <HEIGHT>: Vertical resolution
-f <FPS>: Output FPS
-s <NUMBER>: Slowdown factor (creates slowed-down SVG and captures at lower FPS) for smoother results
-t <TIME>: Maximum file duration in seconds
-o <FILENAME>: Output file" 1>&2
exit 1
}
# defaults
FPS=30
TIME=2
RES_X=1280
RES_Y=720
SLOWDOWN=20
JS_PATH=`dirname $0`/svg-to-mp4.js
OUT_FILE=out.mp4
while getopts ":w:h:f:t:o:s:" opt; do
case $opt in
w)
RES_X=$OPTARG
;;
h)
RES_Y=$OPTARG
;;
f)
FPS=$OPTARG
;;
t)
TIME=$OPTARG
;;
o)
OUT_FILE=$OPTARG
;;
s)
SLOWDOWN=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage
;;
esac
done
shift $((OPTIND-1))
if [ $# -ne 1 ]; then
usage
fi
IN_FILE=$1
IN_PATH="file://`pwd`/$IN_FILE"
if [ ! -e $IN_FILE ]; then
echo "ERROR: Can't find input file \"$IN_FILE\"" >&2
exit 1
fi
TMP_PATH=`mktemp`
mv $TMP_PATH $TMP_PATH.svg
TMP_PATH=$TMP_PATH.svg
# so sorry...
perl -pe 's/(smil:(dur|begin))="([\.0123456789]+)s"/$1 . "=\"" . ($3 * '$SLOWDOWN') . "s\""/ge' $IN_FILE > $TMP_PATH
echo "Converting up to $TIME seconds of $IN_PATH at $FPS fps. Output file: $OUT_FILE"
IN_PATH="file://$TMP_PATH"
echo $TMP_PATH
# Note: swapping stdout / stderr to work around ne'er-to-be-fixed PhantomJS bug
(phantomjs $JS_PATH $IN_PATH $TIME $FPS $RES_X $RES_Y $SLOWDOWN 3>&2 2>&1 1>&3-) | ffmpeg -y -c:v png -f image2pipe -framerate $FPS -t $TIME -i - -c:v libx264 -pix_fmt yuv420p -preset slow -vf scale=$RES_X:$RES_Y -r $FPS $OUT_FILE
# Cleanup
rm $TMP_PATH