Skip to content

Instantly share code, notes, and snippets.

@FarisHijazi
Last active October 18, 2022 16:25
Show Gist options
  • Save FarisHijazi/eff7a7979440faa84a63657e085ec504 to your computer and use it in GitHub Desktop.
Save FarisHijazi/eff7a7979440faa84a63657e085ec504 to your computer and use it in GitHub Desktop.
detect and remove black parts of a video using ffmpeg `trim`.
print("""
this file is has migrated to https://github.com/FarisHijazi/deblack/blob/master/deblack.py
Please go head over to the repository and contribute to the project
""")
@AurelieEspoir
Copy link

Hi again, actually it looks that it only trim it the first 6 times, but there are more blacks in the video. why is this?

ffprobe_cmd: ffprobe -f lavfi -i "movie=video2.mp4,blackdetect[out0]" -show_entries tags=lavfi.black_start,lavfi.black_end -of default=nw=1 -v quiet
ffmpeg -i "video2.mp4" -y -filter_complex "[0:v]trim=start=0:end=20.88,setpts=PTS-STARTPTS,format=yuv420p[0v]; [0:a]atrim=start=0:end=20.88,asetpts=PTS-STARTPTS[0a]; [0:v]trim=start=22.08:end=50.68,setpts=PTS-STARTPTS,format=yuv420p[1v]; [0:a]atrim=start=22.08:end=50.68,asetpts=PTS-STARTPTS[1a]; [0:v]trim=start=101.44:end=111.12,setpts=PTS-STARTPTS,format=yuv420p[2v]; [0:a]atrim=start=101.44:end=111.12,asetpts=PTS-STARTPTS[2a]; [0:v]trim=start=191.2:end=191.72,setpts=PTS-STARTPTS,format=yuv420p[3v]; [0:a]atrim=start=191.2:end=191.72,asetpts=PTS-STARTPTS[3a]; [0:v]trim=start=193.6:end=194.52,setpts=PTS-STARTPTS,format=yuv420p[4v]; [0:a]atrim=start=193.6:end=194.52,asetpts=PTS-STARTPTS[4a]; [0:v]trim=start=195.16:end=196.6,setpts=PTS-STARTPTS,format=yuv420p[5v]; [0:a]atrim=start=195.16:end=196.6,asetpts=PTS-STARTPTS[5a]; [0:v]trim=start=215.2:end=221.24,setpts=PTS-STARTPTS,format=yuv420p[6v]; [0:a]atrim=start=215.2:end=221.24,asetpts=PTS-STARTPTS[6a]; [0v][0a][1v][1a][2v][2a][3v][3a][4v][4a][5v][5a][6v][6a]concat=n=7:v=1:a=1[outv][outa]" -map [outv] -map [outa] "video2.out.mp4"
ffmpeg version 5.1 Copyright (c) 2000-2022 the FFmpeg developers
built with Apple clang version 12.0.0 (clang-1200.0.32.29)

@FarisHijazi
Copy link
Author

it's possible that this is a limitation with ffmpeg, remember that this tool I made is just a script which generates a command for ffmpeg, if ffmpeg has this limitation than that's kind of out of the scope here, what I could suggest (which isn't optimal) is to run it on the video, then run it on the output, then run it on the output, and so-on, until you reach 0 black segments

The issues is that it's inefficient + you may lose information as the video is being processed multiple times (it could be getting compressed again and again)

but this is the fastest thing i could think of

@AurelieEspoir
Copy link

yes, thats actually the way to go, or cut it first in segments then run all the segments . thanks a lot for your reply!

@Stanley-Michael
Copy link

Stanley-Michael commented Oct 15, 2022

Hey, thanks for this. I'm recording things with Nvidia shadow play and seem to get a lot of black flickering. I tried this solution however and I get no error message but the output file does not appear to be getting written. Any help would be greatly appreciated.:

ffprobe_cmd: ffprobe -f lavfi -i "movie=vid1.mp4,blackdetect[out0]" -show_entries tags=lavfi.black_start,lavfi.black_end -of default=nw=1 -v quiet
ffmpeg -i "vid1.mp4" -y -filter_complex "[0:v]trim=start=0:end=0.0333556,setpts=PTS-STARTPTS,format=yuv420p[0v]; [0:a]atrim=start=0:end=0.0333556,asetpts=PTS-STARTPTS[0a]; [0:v]trim=start=0.0662667:end=0.100756,setpts=PTS-
............
[671a][672v][672a][673v][673a][674v][674a][675v][675a][676v][676a][677v][677a][678v][678a][679v][679a][680v][680a][681v][681a][682v][682a][683v][683a][684v][684a][685v][685a][686v][686a][687v][687a][688v][688a][689v][689a][690v][690a][691v][691a][692v][692a][693v][693a][694v][694a][695v][695a][696v][696a][697v][697a][698v][698a][699v][699a][700v][700a][701v][701a][702v][702a][703v][703a][704v][704a][705v][705a][706v][706a][707v][707a][708v][708a][709v][709a][710v][710a][711v][711a][712v][712a][713v][713a][714v][714a][715v][715a][716v][716a][717v][717a][718v][718a][719v][719a]concat=n=720:v=1:a=1[outv][outa]"
-map [outv] -map [outa] "vid1.out.mp4"

@FarisHijazi
Copy link
Author

FarisHijazi commented Oct 18, 2022

since so many people are using this, I created an entire repository based around this https://github.com/FarisHijazi/deblack, I haven't had much time to go over these issues, but I plan to do that in the future

Please go ahead and post your issues there and also maybe star the repository

@combs
Copy link

combs commented Oct 18, 2022

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment