kostya572
kostya572

Reputation: 169

FFMPEG Replacing black color fragments with alpha channel transparency

I have file.mov video. It has a lot of blank black color fragments that have different durations.

I need to replace black color fragments that show up for more than 5 seconds with transparency. Is that possible to add some alpha channel for that purpose?

Comment to Mulvya's answer:

Amazing solution. With #2 section everything ok.

With #1 section I created this code:

output=$(ffprobe -f lavfi -i "movie=file.mov,blackdetect=d=3.5" -show_entries tags=lavfi.black_start,lavfi.black_end -of compact=p=0 -v 0|awk '!/^$/')
echo $output

using awk '!/^$/' to remove empty lines.

Here is the output I get:

output

tag:lavfi.black_start repeats with same value several times, not having right structure with closing tag:lavfi.black_end

If I change blackdetect=d=3.5 to other value, for exaple d=10, it outputs the same result as d=3.5. How could I solve this issue having right tag:lavfi.black_start,tag:lavfi.black_end synthax with correct grepping d= value?

Upvotes: 1

Views: 1800

Answers (1)

Gyan
Gyan

Reputation: 93369

#1 Run blackdetect to identify fragments

ffprobe -f lavfi -i "movie=file.mov,blackdetect=d=3.5" -show_entries tags=lavfi.black_start,lavfi.black_end -of compact=p=0 -v 0

Edit: Due to a quirk in the filter, one has to use ffmpeg to get this data

The command below saves the data to a text file.

ffmpeg -f lavfi -i movie=file.mov,blackdetect=d=3.5,metadata=print:file=- -f null - -hide_banner -v 0 | grep lavfi > times.txt 

This will print a set of timecodes of black segments lasting atleast 3.5 seconds.

#2 Add alpha and change alpha of fragments to 0

ffmpeg -i file.mov -vf format=rgba,colorchannelmixer=aa=0:enable='between(t,12.4,16.1)+between(t,55.1,60.0)+between(t,62.9,69.2)' -c:v libvpx-vp9 -crf 10 -b:v 0 out.mkv

In each between expression is the start time and end time of a black fragment, in seconds.

If you want to save the result to file, you'll need to pick a codec which supports alpha, like the one in the above command. If your file already has alpha, skip the format filter.

Upvotes: 1

Related Questions