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