]> Raphaƫl G. Git Repositories - binary/blob - vidpack
ed0abf6e2a052b52914a1a8d6480d57e72659ed7
[binary] / vidpack
1 #! /bin/sh
2
3 # Brighten
4 #XXX: ffplay -vf "curves=all='0/0 0.25/0.375 0.5/0.75 0.75/0.875 1/1'"
5
6 #Set document directory
7 docdir=/var/www/doc
8
9 #Set video directory
10 viddir=/var/www/video
11
12 #Set temporary directory
13 tmpdir=/tmp/vidpack
14
15 #Without temporary directory
16 if [ ! -d "$tmpdir" ]; then
17 #Create temporary directory or die
18 mkdir -p "$tmpdir" || exit 1;
19 fi
20
21 #Set time as utc
22 export TZ=utc
23
24 #Change to document directory
25 pushd "$docdir" > /dev/null || exit 1
26
27 #Iterate on each mov or mp4 in doc dir older than 1h
28 for i in `find $docdir -maxdepth 1 -type f,l -name '*.mov' -o -name '*.mp4' -mmin +60`; do
29 #Set file date
30 fdate=$(date -u --rfc-3339=ns -r $i | perl -pne 's/\..*$/.000000Z/; s/ /T/');
31
32 #Without file date
33 if [ -z "$fdate" ]; then
34 #File date must exists
35 exit 1;
36 fi
37
38 #Set name date
39 #XXX: may miss if filename don't match /^(.*[^0-9])?YYYYMMDD[^0-9]?HHMMSS([^0-9].*)?$/
40 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/');
41
42 #Without name date
43 if [ -z "$ndate" ]; then
44 #Set name date
45 #XXX: catch when filename match only /^(.*[^0-9])?20YYMMDD([^0-9].*)?$/
46 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/');
47 fi
48
49 #Set create date
50 cdate=$(ffprobe -hide_banner -v quiet -show_entries format_tags=date,creation_time,com.apple.quicktime.creationdate -of default=nokey=1:noprint_wrappers=1 -i $i | perl -ne '$l = $_ unless /^$/; END { chomp $l; print $l }');
51
52 #Set best date
53 date=$(echo -e "$fdate\n$ndate\n$cdate" | perl -ne '$l = $_ unless /^(0000:00:00 00:00:00|)$/; END { chomp $l; print $l }');
54
55 #Set dest filename
56 dest="$viddir/$(echo $date | perl -pne 's%^([0-9]{4})-([0-9]{2})-([0-9]{2}).*$%\1/\2/\3%')/$(echo ${i#$docdir/} | perl -pne 's/\.(?:mov|mp4)$//')";
57
58 #Without dest directory
59 if [ ! -d "$(dirname $dest)" ]; then
60 #Create dest directory or die
61 mkdir -p "$(dirname $dest)" || exit 1;
62 fi
63
64 #Set passlog filename
65 passlog="$tmpdir/$(echo $i | perl -pne 's%(?:.*/)([^/]+)\.(?:mov|mp4)$%\1%')";
66
67 #With passlog lock file
68 if [ -f "$passlog.lock" ]; then
69 #Display error
70 echo "Operation in progress: $passlog-0.log exists" 1>&2
71
72 #Exit with failure
73 exit 0;
74 fi
75
76 #Set location
77 #XXX: see https://stackoverflow.com/questions/65231616/what-is-the-difference-between-location-and-location-eng-metadata-of-a-mp4-f
78 #XXX: drop location name after /
79 location=$(ffprobe -hide_banner -v quiet -show_entries format_tags=location,location-eng,com.apple.quicktime.location.ISO6709 -of default=nokey=1:noprint_wrappers=1 -i $i 2> /dev/null | perl -ne 's/\/.*$/\//g; $l = $_ unless /^$/; END { chomp $l; print $l }')
80
81 #With location
82 if [ ! -z "$location" ]; then
83 #Replace with location arguments
84 location=" -metadata location=\"$location\"";
85 #Without location
86 else
87 #Prevent empty location
88 location="";
89 fi
90
91 #Set source rate
92 #TODO: use instead which works on webm: ffprobe -hide_banner -v quiet -select_streams v:0 -show_entries format=bit_rate -of default=nokey=1:noprint_wrappers=1 -i "$i" | perl -ne 'chomp; print'
93 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');
94
95 #Set bitrate
96 bitrate=256k
97
98 #With sourcerate >= 5120k (1080p)
99 if [ $sourcerate -ge 5120000 ]; then
100 #Set bitrate
101 bitrate=5120k
102 #With sourcerate >= 2560k (720p)
103 elif [ $sourcerate -ge 2560000 ]; then
104 #Set bitrate
105 bitrate=2560k
106 #With sourcerate >= 1024k
107 elif [ $sourcerate -ge 1024000 ]; then
108 #Set bitrate
109 bitrate=1024k
110 #With sourcerate >= 512k
111 elif [ $sourcerate -ge 512000 ]; then
112 #Set bitrate
113 bitrate=512k
114 fi
115
116 #With dest.webm existing
117 if [ ! -f "$dest.webm" ]; then
118 #Create passlog lock file
119 touch "$passlog.lock"
120
121 #With 1080p sourcerate
122 if [ $sourcerate -ge 5120000 ]; then
123 #Set bitrate
124 bitrate=5120k
125
126 #First pass
127 ffmpeg -i $i -v error -passlogfile $passlog.1080p -c:v libvpx-vp9 -b:v $bitrate -pass 1 -an -f null /dev/null < /dev/null;
128
129 #With first pass
130 if [ $? = 0 ]; then
131 #Second pass
132 ffmpeg -y -i $i -v error -passlogfile $passlog.1080p -c:v libvpx-vp9 -b:v $bitrate -pass 2 -pix_fmt yuv420p -c:a libopus -movflags +faststart -metadata creation_time="$date"$location -fflags +bitexact $dest.1080p.webm < /dev/null;
133 fi
134
135 #With passlog file
136 if [ -f "$passlog.1080p-0.log" ]; then
137 #Remove passlog file
138 unlink "$passlog.1080p-0.log";
139 fi
140
141 #Set bitrate
142 bitrate=1024k
143 fi
144
145 #With sourcerate >= 2560k (720p)
146 if [ $sourcerate -ge 2560000 ]; then
147 #Set bitrate
148 bitrate=2560k
149
150 #First pass
151 ffmpeg -i $i -v error -passlogfile $passlog.720p -c:v libvpx-vp9 -b:v $bitrate -pass 1 -an -f null /dev/null < /dev/null;
152
153 #With first pass
154 if [ $? = 0 ]; then
155 #Second pass
156 ffmpeg -y -i $i -v error -passlogfile $passlog.720p -c:v libvpx-vp9 -b:v $bitrate -pass 2 -pix_fmt yuv420p -c:a libopus -movflags +faststart -metadata creation_time="$date"$location -fflags +bitexact $dest.720p.webm < /dev/null;
157 fi
158
159 #With passlog file
160 if [ -f "$passlog.720p-0.log" ]; then
161 #Remove passlog file
162 unlink "$passlog.720p-0.log";
163 fi
164
165 #Set bitrate
166 bitrate=1024k
167 fi
168
169 #First pass
170 ffmpeg -i $i -v error -passlogfile $passlog -c:v libvpx-vp9 -b:v $bitrate -pass 1 -an -f null /dev/null < /dev/null;
171
172 #With first pass
173 if [ $? = 0 ]; then
174 #Second pass
175 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"$location -fflags +bitexact $dest.webm < /dev/null;
176 fi
177
178 #With passlog file
179 if [ -f "$passlog-0.log" ]; then
180 #Remove passlog file
181 unlink "$passlog-0.log";
182 fi
183
184 #With passlog lock file
185 if [ -f "$passlog.lock" ]; then
186 #Remove passlog lock file
187 unlink "$passlog.lock";
188 fi
189
190 fi
191 done
192
193 #Revert to old dir
194 popd > /dev/null || exit 1;
195
196 #Remove temporary directory
197 rmdir "$tmpdir" || true;
198
199 #Exit with success
200 exit 0;