Issue
Case :
I'm converting an old archive of 18000 commercials I'd like to automate this reconversion from DV to some lighter and uptodate h264 mp4
The whole process is ready but I'm stuck at the first part: cropping the black bars...
Tools used :
The thing is : all videos are different, I cannot use one static value, thus I use the wonderful cropdetect tool by ffmpeg which outputs me this kind of lines in the cli:
[Parsed_cropdetect_0 @ 0xbe5e960] x1:22 x2:697 y1:93 y2:483 w:672 h:384 x:24 y:98 pts:63 t:2.520000 crop=672:384:24:98
[Parsed_cropdetect_0 @ 0xbe5e960] x1:22 x2:697 y1:93 y2:483 w:672 h:384 x:24 y:98 pts:64 t:2.560000 crop=672:384:24:98
[Parsed_cropdetect_0 @ 0xbe5e960] x1:22 x2:697 y1:93 y2:484 w:672 h:384 x:24 y:98 pts:65 t:2.600000 crop=672:384:24:98
[Parsed_cropdetect_0 @ 0xbe5e960] x1:22 x2:697 y1:93 y2:496 w:672 h:400 x:24 y:96 pts:66 t:2.640000 crop=672:400:24:96
[Parsed_cropdetect_0 @ 0xbe5e960] x1:2 x2:717 y1:80 y2:496 w:704 h:416 x:8 y:80 pts:67 t:2.680000 crop=704:416:8:80
[Parsed_cropdetect_0 @ 0xbe5e960] x1:1 x2:718 y1:80 y2:496 w:704 h:416 x:8 y:80 pts:68 t:2.720000 crop=704:416:8:80
[Parsed_cropdetect_0 @ 0xbe5e960] x1:1 x2:718 y1:80 y2:496 w:704 h:416 x:8 y:80 pts:69 t:2.760000 crop=704:416:8:80
[Parsed_cropdetect_0 @ 0xbe5e960] x1:1 x2:718 y1:80 y2:496 w:704 h:416 x:8 y:80 pts:70 t:2.800000 crop=704:416:8:80
[Parsed_cropdetect_0 @ 0xbe5e960] x1:1 x2:718 y1:80 y2:496 w:704 h:416 x:8 y:80 pts:71 t:2.840000 crop=704:416:8:80
So the last part : crop=
gives the result of the remaining frame (width, hight, starting x point starting y point. to crop the video..
But these values are not always the same inside one video how can I extract 'the biggest values' out of these hundreds of lines?
In this case, just keep crop=704:416:8:80
Edit
Actually, the result should take in account the lowest x1, highest x2, lowest y1 and highest y2, then create a rectangle multiple of 16 inside it...
Solution
There are better methods for sure, but maybe this can serve as a trampoline for you:
#!/bin/bash
IFS=':'
while read a b c d; do
w+=("$a")
h+=("$b")
x+=("$c")
y+=("$d")
done < <(sed 's/.*crop=\(.*\)/\1/g' file)
IFS=$'\n'
echo "w_max=$(echo "${w[*]}" | sort -nr | head -n1)"
echo "w_min=$(echo "${w[*]}" | sort -n | head -n1)"
echo "h_max=$(echo "${h[*]}" | sort -nr | head -n1)"
echo "h_min=$(echo "${h[*]}" | sort -n | head -n1)"
echo "x_max=$(echo "${x[*]}" | sort -nr | head -n1)"
echo "x_min=$(echo "${x[*]}" | sort -n | head -n1)"
echo "y_max=$(echo "${y[*]}" | sort -nr | head -n1)"
echo "y_min=$(echo "${y[*]}" | sort -n | head -n1)"
# do your math here
The output is:
w_max=704
w_min=672
h_max=416
h_min=384
x_max=24
x_min=8
y_max=98
y_min=80
You should still do your "multiple of 16" math at the end.
Answered By - Eugeniu Rosca Answer Checked By - Willingham (WPSolving Volunteer)