Search code examples
ffmpegalphamov

ffmpeg: How can a MOV with transparent background be created?


I'm trying - with no success at all - to convert the green pixels of a background into transparent ones and output the result as clip with ffmpeg. N.b. I do not want to lay the clip over anything; I'm not having a problem with that. What I want is a clip with transparent background for the OpenShot video editor (the chromakey filter of which doesn't work satisfyingly).

What I have tried (amongst 1 zillion other things over the last 15 hrs.) was

ffmpeg.exe -i in.mov -vf chromakey=0x008001:0.115:0.0 -c:v qtrle out.mov

but the pixels simply would not be transparent. Seemingly, nothing happens. I reckon the filter is ok, because it works fine in a complex chain (overlaying a background image).

The output of ffprompt -show_stream -show_format of out.mov is as follows:

[STREAM]
index=0
codec_name=qtrle
codec_long_name=QuickTime Animation (RLE) video
profile=unknown
codec_type=video
codec_time_base=1/30
codec_tag_string=rle
codec_tag=0x20656c72
width=1920
height=1080
coded_width=1920
coded_height=1080
has_b_frames=0
sample_aspect_ratio=1:1
display_aspect_ratio=16:9
pix_fmt=bgra
level=-99
color_range=N/A
color_space=unknown
color_transfer=unknown
color_primaries=unknown
chroma_location=unspecified
field_order=progressive
timecode=N/A
refs=1
id=N/A
r_frame_rate=30/1
avg_frame_rate=30/1
time_base=1/15360
start_pts=0
start_time=0.000000
duration_ts=54789
duration=3.566992
bit_rate=822383192
max_bit_rate=N/A
bits_per_raw_sample=N/A
nb_frames=107
nb_read_frames=N/A
nb_read_packets=N/A
DISPOSITION:default=1
DISPOSITION:dub=0
DISPOSITION:original=0
DISPOSITION:comment=0
DISPOSITION:lyrics=0
DISPOSITION:karaoke=0
DISPOSITION:forced=0
DISPOSITION:hearing_impaired=0
DISPOSITION:visual_impaired=0
DISPOSITION:clean_effects=0
DISPOSITION:attached_pic=0
DISPOSITION:timed_thumbnails=0
TAG:language=eng
TAG:handler_name=DataHandler
TAG:encoder=Lavc57.64.101 qtrle
[/STREAM]
[STREAM]
index=1
codec_name=aac
codec_long_name=AAC (Advanced Audio Coding)
profile=LC
codec_type=audio
codec_time_base=1/44100
codec_tag_string=mp4a
codec_tag=0x6134706d
sample_fmt=fltp
sample_rate=44100
channels=2
channel_layout=stereo
bits_per_sample=0
id=N/A
r_frame_rate=0/0
avg_frame_rate=0/0
time_base=1/44100
start_pts=926
start_time=0.020998
duration_ts=157481
duration=3.570998
bit_rate=132103
max_bit_rate=132103
bits_per_raw_sample=N/A
nb_frames=153
nb_read_frames=N/A
nb_read_packets=N/A
DISPOSITION:default=1
DISPOSITION:dub=0
DISPOSITION:original=0
DISPOSITION:comment=0
DISPOSITION:lyrics=0
DISPOSITION:karaoke=0
DISPOSITION:forced=0
DISPOSITION:hearing_impaired=0
DISPOSITION:visual_impaired=0
DISPOSITION:clean_effects=0
DISPOSITION:attached_pic=0
DISPOSITION:timed_thumbnails=0
TAG:language=eng
TAG:handler_name=DataHandler
[/STREAM]
[FORMAT]
filename=out.mov
nb_streams=2
nb_programs=0
format_name=mov,mp4,m4a,3gp,3g2,mj2
format_long_name=QuickTime / MOV
start_time=0.000000
duration=3.567000
size=366708874
bit_rate=822447712
probe_score=100
TAG:major_brand=qt
TAG:minor_version=512
TAG:compatible_brands=qt
TAG:encoder=Lavf57.56.101
[/FORMAT]

I have a "sample" clip which shows the behaviour I want, with the following stream and information:

[STREAM]
index=0
codec_name=qtrle
codec_long_name=QuickTime Animation (RLE) video
profile=unknown
codec_type=video
codec_time_base=1/24
codec_tag_string=rle
codec_tag=0x20656c72
width=1920
height=1080
coded_width=1920
coded_height=1080
has_b_frames=0
sample_aspect_ratio=0:1
display_aspect_ratio=0:1
pix_fmt=bgra
level=-99
color_range=N/A
color_space=unknown
color_transfer=unknown
color_primaries=unknown
chroma_location=unspecified
field_order=progressive
timecode=N/A
refs=1
id=N/A
r_frame_rate=24/1
avg_frame_rate=24/1
time_base=1/12288
start_pts=0
start_time=0.000000
duration_ts=74760
duration=6.083984
bit_rate=49226848
max_bit_rate=N/A
bits_per_raw_sample=N/A
nb_frames=146
nb_read_frames=N/A
nb_read_packets=N/A
DISPOSITION:default=1
DISPOSITION:dub=0
DISPOSITION:original=0
DISPOSITION:comment=0
DISPOSITION:lyrics=0
DISPOSITION:karaoke=0
DISPOSITION:forced=0
DISPOSITION:hearing_impaired=0
DISPOSITION:visual_impaired=0
DISPOSITION:clean_effects=0
DISPOSITION:attached_pic=0
DISPOSITION:timed_thumbnails=0
TAG:language=eng
TAG:handler_name=DataHandler
TAG:encoder=Lavc57.24.102 qtrle
[/STREAM]
[STREAM]
index=1
codec_name=aac
codec_long_name=AAC (Advanced Audio Coding)
profile=LC
codec_type=audio
codec_time_base=1/48000
codec_tag_string=mp4a
codec_tag=0x6134706d
sample_fmt=fltp
sample_rate=48000
channels=2
channel_layout=stereo
bits_per_sample=0
id=N/A
r_frame_rate=0/0
avg_frame_rate=0/0
time_base=1/48000
start_pts=0
start_time=0.000000
duration_ts=293856
duration=6.122000
bit_rate=53537
max_bit_rate=128000
bits_per_raw_sample=N/A
nb_frames=288
nb_read_frames=N/A
nb_read_packets=N/A
DISPOSITION:default=1
DISPOSITION:dub=0
DISPOSITION:original=0
DISPOSITION:comment=0
DISPOSITION:lyrics=0
DISPOSITION:karaoke=0
DISPOSITION:forced=0
DISPOSITION:hearing_impaired=0
DISPOSITION:visual_impaired=0
DISPOSITION:clean_effects=0
DISPOSITION:attached_pic=0
DISPOSITION:timed_thumbnails=0
TAG:language=eng
TAG:handler_name=DataHandler
[/STREAM]
[FORMAT]
filename=templateOK.mov
nb_streams=2
nb_programs=0
format_name=mov,mp4,m4a,3gp,3g2,mj2
format_long_name=QuickTime / MOV
start_time=0.000000
duration=6.144000
size=37478506
bit_rate=48800138
probe_score=100
TAG:major_brand=qt
TAG:minor_version=512
TAG:compatible_brands=qt
TAG:encoder=Lavf57.25.100
[/FORMAT]

and I simply am not able to spot the relevant difference.

The input, output and the working template can be found here.

(The security issue you might see when clicking the link comes from the server certificate being self-signed. You can accept a temporal exception. Btw: The ridiculous file size of the output file will be the next nut to crack. Probably something about compression.)


Solution

  • A video player will not show transparency, as you've discovered.

    Transparency does not really exist in video. Each pixel has to have some color. Transparency is implemented by adding another component, called alpha. The value of the alpha channel at a position determines how the application handling the file blends the colored pixel at that position with the colored pixels of the layer(s) beneath it (themselves weighted by their alpha). If there are no layers beneath, as is the case in a video player, no blending occurs and the stored color is shown. If alpha for a pixel is 0 then the color is nullified and the layer beneath completely shows through, and hence 'transparency'.

    Long story short: check transparency in a app which can composite, like a video editor or VFX app.