]> Raphaƫl G. Git Repositories - tools/blob - vidpack
Add video packing script
[tools] / vidpack
1 #! /bin/sh
2
3 #Set temporary directory
4 tmpdir=/tmp/vidpack
5
6 #Without temporary directory
7 if [ ! -d "$tmpdir" ]; then
8 #Create temporary directory or die
9 mkdir -p "$tmpdir" || exit 1;
10 fi
11
12 #Set time as utc
13 export TZ="utc"
14
15 #Iterate on each video in cur dir older than 1h
16 #for i in `find $docdir -maxdepth 1 -type f,l -name '*.mov' -o -name '*.mp4' -mmin +60`; do
17 #for i in `find . -maxdepth 1 -type f \( -name '*.mp4' -o -name '*.avi' -o -name '*.mpg' -o -name '*.mkv' \)`; do
18 find . -maxdepth 1 -type f \( -iname '*.mov' -o -iname '*.mp4' -o -iname '*.avi' -o -iname '*.mpg' -o -iname '*.mkv' \) -print0 | while read -d $'\0' i; do
19 #Set file date
20 fdate=$(date -u --rfc-3339=ns -r "$i" | perl -pne 's/\..*$/.000000Z/; s/ /T/');
21
22 #Without file content
23 if [ -s "$i" ]; then
24 #File must not be empty
25 continue;
26 fi
27
28 #Without file date
29 if [ -z "$fdate" ]; then
30 #File date must exists
31 exit 1;
32 fi
33
34 #Set name date
35 #XXX: may miss if filename don't match /^(.*[^0-9])?YYYYMMDD[^0-9]?HHMMSS([^0-9].*)?$/
36 ndate=$(echo "$i" | perl -ne 'print if s/^(?:.*[^0-9])?([0-9]{4})([0-9]{2})([0-9]{2})(?:[^0-9])?([0-9]{2})([0-9]{2})([0-9]{2})(?:[^0-9].*)?$/\1-\2-\3T\4:\5:\6.000000Z/');
37
38 #Without name date
39 if [ -z "$ndate" ]; then
40 #Set name date
41 #XXX: catch when filename match only /^(.*[^0-9])?20YYMMDD([^0-9].*)?$/
42 ndate=$(echo "$i" | perl -ne 'print if s/^(?:.*[^0-9])?(20[0-9]{2})([0-9]{2})([0-9]{2})(?:[^0-9].*)?$/\1-\2-\3T00:00:00.000000Z/');
43 fi
44
45 #Set create date
46 cdate=$(ffprobe -hide_banner -v error -show_entries format_tags=date,creation_time,com.apple.quicktime.creationdate -of default=nokey=1:noprint_wrappers=1 -i "$i" 2> /dev/null | perl -ne '$l = $_ unless /^$/; END { chomp $l; print $l }');
47
48 #Set best date
49 date=$(echo -e "$fdate\n$ndate\n$cdate" | perl -ne '$l = $_ unless /^(0000:00:00 00:00:00|)$/; END { chomp $l; print $l }');
50
51 #Set dest filename
52 dest="$(echo "$i" | perl -pne 's/\.(?:mp4|avi|mpg|mkv)$//').webm";
53
54 #Set passlog filename
55 passlog="$tmpdir/$(echo "$i" | perl -pne 's%(?:.*/)([^/]+)\.(?:mp4|avi|mpg|mkv)$%\1%;s% %_%g')";
56
57 #With passlog file
58 #if [ -f "$passlog-0.log" ]; then
59 # #Display error
60 # echo "Operation in progress: $passlog-0.log exists"
61 #
62 # #Exit with failure
63 # exit 1;
64 #fi
65
66 #Set source rate
67 sourcerate=$(ffprobe -hide_banner -v quiet -select_streams v:0 -show_entries stream=bit_rate -of default=nokey=1:noprint_wrappers=1 "$i" | perl -ne 'chomp; print');
68
69 #Set bitrate
70 bitrate=256k
71
72 #With sourcerate >= 5120k (1080p)
73 if [ $sourcerate -ge 5120000 ]; then
74 #Set bitrate
75 bitrate=5120k
76 #With sourcerate >= 2560k (720p)
77 elif [ $sourcerate -ge 2560000 ]; then
78 #Set bitrate
79 bitrate=2560k
80 #With sourcerate >= 1024k
81 elif [ $sourcerate -ge 1024000 ]; then
82 #Set bitrate
83 bitrate=1024k
84 #With sourcerate >= 512k
85 elif [ $sourcerate -ge 512000 ]; then
86 #Set bitrate
87 bitrate=512k
88 fi
89
90 #With dest existing
91 if [ ! -f "$dest" ]; then
92 #Without passlog file
93 if [ ! -f "$passlog-0.log" ]; then
94 #First pass
95 #ffmpeg -i "$i" -v error -passlogfile "$passlog" -c:v libaom-av1 -b:v "$bitrate" -pass 1 -an -f null /dev/null < /dev/null;
96 ffmpeg -i "$i" -v error -passlogfile "$passlog" -c:v libvpx-vp9 -b:v "$bitrate" -pass 1 -an -f null /dev/null < /dev/null;
97
98 #With first pass
99 if [ $? != 0 ]; then
100 #Skip to next run
101 continue;
102 fi
103 fi
104
105 #With passlog file
106 if [ -f "$passlog-0.log" ]; then
107 #Second pass
108 #XXX: see http://wiki.webmproject.org/webm-metadata/global-metadata#TOC-Official-Tags
109 #ffmpeg -y -i "$i" -v error -passlogfile "$passlog" -c:v libaom-av1 -b:v "$bitrate" -pass 2 -pix_fmt yuv420p -c:a libopus -movflags +faststart -metadata creation_time="$date" -fflags +bitexact "$dest" < /dev/null;
110 ffmpeg -y -i "$i" -v error -passlogfile "$passlog" -c:v libvpx-vp9 -b:v "$bitrate" -pass 2 -pix_fmt yuv420p -c:a libopus -movflags +faststart -metadata creation_time="$date" -fflags +bitexact "$dest" < /dev/null;
111
112 #With second pass pass
113 if [ $? = 0 ]; then
114 #With non empty dest file
115 #if [ -s "$dest" ]; then
116 # #Remove source file
117 # unlink "$i";
118 #fi
119
120 #With passlog file
121 if [ -f "$passlog-0.log" ]; then
122 #Remove passlog file
123 unlink "$passlog-0.log";
124 fi
125 fi
126 fi
127 fi
128 done
129
130 #Remove temporary directory
131 rmdir "$tmpdir" || true;
132
133 #Exit with success
134 exit 0;