How to crop a video of its black borders
1 Attachment(s)
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 - Code:
Metadata: Code:
/PROC$ awk '/crop=/ {print $14}' CROPDATA.txt The next step is put this all into a script and this is it Code:
#!/bin/bash |
Instead of relying on a fragile detection algorithm, I'd simply take a screenshot of the video (assuming that the black borders remain unchanged throughout the video), calculate the pixel dimensions I need to crop, and crop it with ffmpeg.
|
Quote:
|
Quote:
What is the output of: Code:
ffprobe CBS_Justice-10282020-0348.mts This will reencode the video. Look at what ffmpeg's defaults are. Code:
ffmpeg -i file.ts -vf crop=700:400:0:2 out.mp4 Do you want to crop without reencoding or resizing? Code:
ffmpeg -i Input.ts -c copy -bsf:v h264_metadata=crop_left=20:crop_right=20:crop_top=10:crop_bottom=10 output.ts |
Quote:
Things that an go wrong: 1 You detect the wrong bit of video 2 The crop=nn:nn:nn:nn isn't the 14th column 3 There are multiple crop= sequences in the data output file 4 Many things I havn't thought of BTW I do assume the black borders remain the same throughout the video |
Quote:
I do need a script I can the just loop through my collection of videos and just leave it to run. |
Quote:
You're dead right I should have used -acodec copy, I just rushed the last bit - my evening meal was waiting for me and the missus would have been upset if I was late - know what I mean? |
Quote:
And you cannot use copy when re-encoding - here's the error message Code:
Filtergraph 'crop=400:576:72:0' was defined for video output stream 0:0 but codec copy was selected. It does take time but I run this through a driver script that will process about 20 or 30 videos - takes a little while which is why I do it this way. Setting up the driver script only takes 5 mins or so then the 'puter does all the work - neat innit! |
Code:
And you cannot use copy when re-encoding |
Okay folks, I've added this autodetect feature into my script, it's pretty ugly and I'm sure I could improve it but it works and does what I want
Basically I user a driver script Code:
#!/bin/bash Code:
#!/bin/bash |
|
Quote:
And those links are empty! |
Quote:
I could just re-encoe the entie video, then rop and copy the resulting mp4 file, it may be quicker, but I don't think there will be much in it, worth trying tho, |
Quote:
You are asking a lot of video editing questions, AVSynth might be very helpful to you. |
You did not answer post #4. I think that you have not started at the start. The first thing to do it find out what audio/video is in that transport stream. Then you will know what your options are with ffmpeg, and how to reencode it.
|
All times are GMT -5. The time now is 05:08 AM. |