How to crop a video of its black borders
by GPGAgent from LinuxQuestions.org on (#5BY59)
You can use HandBrakeCLI to encode a video and it will automatically crop the black borders, works well unless something on screen gets in the way, see the attached image.
The CBS-Justice tag spoils this
So you can use ffmpeg to detect the black borders and to give you the crop parameters
Code:ffmpeg -hide_banner -loglevel info -ss 10:00 -i CBS_Justice.mts -vframes 2 -vf "cropdetect=24:16:0" -f null -This will output this Code: Metadata:
encoder : Lavc58.54.100 pcm_s16le
[Parsed_cropdetect_0 @ 0x5567e808af80] x1:69 x2:474 y1:0 y2:575 w:400 h:576 x:72 y:0 pts:74772 t:0.830800 crop=400:576:72:0
frame= 2 fps=0.0 q=-0.0 Lsize=N/A time=00:00:00.80 bitrate=N/A speed=69.5x
video:1kB audio:9kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknownThe next step is to extract the crop=400:576:72:0 bit so you can use awkCode:/PROC$ awk '/crop=/ {print $14}' CROPDATA.txt
crop=400:576:72:0
jonke@charlie:/media/jonke/HITA_500GB/FILES/FUGITIVETEST/PROC$I saved the output from ffmpeg to CROPDATA.txt
The next step is put this all into a script and this is itCode:#!/bin/bash
renice 19 -p $$
# detect the cropping parameters
FFREPORT=file=CROPDATA.txt:level=32 ffmpeg -hide_banner -loglevel quiet -ss 10:00 -i CBS_Justice-10282020-0348.mts -vframes 2 -vf "cropdetect=24:16:0" -f null -
# Extract the cropping paramaters for the -vf switch
CROP=`awk '/crop=/ {print $14}' CROPDATA.txt`
# run ffmpeg using the cropping parameters
FFREPORT=file=cropit.log:level=24 ffmpeg -hide_banner -ss 9:50 -t 10 -i CBS_Justice-10282020-0348.mts -vf $CROP cropit.mp4
# loglevels quiet -8 | fatal 8 | error 16| warning 24 | (info 32) | verbose 40 | debug 48 trace 56
echo "finished okay"I'll be making the start point of the cropdetect, and the input video file an argument to the script, but what I would like is a critique on the script, could it be improved or is there an entirely different way of cropping out black borders?
Attached Thumbnails


The CBS-Justice tag spoils this
So you can use ffmpeg to detect the black borders and to give you the crop parameters
Code:ffmpeg -hide_banner -loglevel info -ss 10:00 -i CBS_Justice.mts -vframes 2 -vf "cropdetect=24:16:0" -f null -This will output this Code: Metadata:
encoder : Lavc58.54.100 pcm_s16le
[Parsed_cropdetect_0 @ 0x5567e808af80] x1:69 x2:474 y1:0 y2:575 w:400 h:576 x:72 y:0 pts:74772 t:0.830800 crop=400:576:72:0
frame= 2 fps=0.0 q=-0.0 Lsize=N/A time=00:00:00.80 bitrate=N/A speed=69.5x
video:1kB audio:9kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknownThe next step is to extract the crop=400:576:72:0 bit so you can use awkCode:/PROC$ awk '/crop=/ {print $14}' CROPDATA.txt
crop=400:576:72:0
jonke@charlie:/media/jonke/HITA_500GB/FILES/FUGITIVETEST/PROC$I saved the output from ffmpeg to CROPDATA.txt
The next step is put this all into a script and this is itCode:#!/bin/bash
renice 19 -p $$
# detect the cropping parameters
FFREPORT=file=CROPDATA.txt:level=32 ffmpeg -hide_banner -loglevel quiet -ss 10:00 -i CBS_Justice-10282020-0348.mts -vframes 2 -vf "cropdetect=24:16:0" -f null -
# Extract the cropping paramaters for the -vf switch
CROP=`awk '/crop=/ {print $14}' CROPDATA.txt`
# run ffmpeg using the cropping parameters
FFREPORT=file=cropit.log:level=24 ffmpeg -hide_banner -ss 9:50 -t 10 -i CBS_Justice-10282020-0348.mts -vf $CROP cropit.mp4
# loglevels quiet -8 | fatal 8 | error 16| warning 24 | (info 32) | verbose 40 | debug 48 trace 56
echo "finished okay"I'll be making the start point of the cropdetect, and the input video file an argument to the script, but what I would like is a critique on the script, could it be improved or is there an entirely different way of cropping out black borders?
Attached Thumbnails