kklmarvelmedia
kklmarvelmedia

Reputation: 61

FFmpeg transparent PNG black outline issue

I'm encoding a video with a transparent PNG using ffmpeg. I noticed there's a slight black outline surrounding the image. Is there any way to remove it?

Output image: image

Transparent PNG sample: image

My ffmpeg command

ffmpeg -hide_banner -y -ss 0.0 -t 8.5 -i C:\Users\Admin\Desktop\test_movies\6.mp4 -i C:\Users\Admin\Desktop\test_movies\text_and_emoji.png -filter_complex [0:v]setpts=PTS-STARTPTS,scale=640:640:force_original_aspect_ratio=decrease,pad=640:640:(ow-iw)/2:(oh-ih)/2:color=#18ffff[0v];[1:v]scale=556.24744:141.41884[1v];[0v][1v]overlay=(W-w)/2-(W/2-325.33328):(H-h)/2-(H/2-567.7075):enable='between(t,0.0,8.5)' -ac 2 -ar 44100 -vcodec libx264 -g 75 -r 20 -preset ultrafast -strict experimental C:\Users\Admin\Desktop\test_movies\test.mp4

Last edit 1:

I tried using without [1:v]scale=556.24744:141.41884[1v], the output still have the slight outline

Sample output: image

Sample code:

ffmpeg -hide_banner -y -ss 0.0 -t 8.5 -i C:\Users\Admin\Desktop\test_movies\white.mp4 -i C:\Users\Admin\Desktop\test_movies\text_and_emoji.png -filter_complex [0:v]scale=640:640:force_original_aspect_ratio=decrease,pad=640:640:(ow-iw)/2:(oh-ih)/2:color=#18ffff[0v];[0v][1:v]overlay=(W-w)/2-(W/2-325.33328):(H-h)/2-(H/2-567.7075):enable='between(t,0.0,8.5)' -ac 2 -ar 44100 -vcodec libx264 -preset ultrafast -strict experimental C:\Users\Admin\Desktop\test_movies\test.mp4

Last edit 2:

I tried another one with added alpha=premultiplied with latest ffmpeg version. It somehow removed the outline, but the quality of the picture reduced alot till it seems like it's pixelated. Plus. there's another unknown white layer at the back of the image.

Output video output video

Sample code:

C:\Users\Admin\Downloads\ffmpeg-20180102-57d0c24-win64-static\bin\ffmpeg -y -ss 0.0 -t 8.5 -i C:\Users\Admin\Desktop\test_movies\white.mp4 -i C:\Users\Admin\Desktop\test_movies\text_and_emoji.png -filter_complex [0:v]scale=640:640:force_original_aspect_ratio=decrease,pad=640:640:(ow-iw)/2:(oh-ih)/2:color=#00ffff[0v];[1:v]scale=480:120[1v];[0v][1v]overlay=(W-w)/2-(W/2-325.33328):(H-h)/2-(H/2-567.7075):alpha=premultiplied:enable='between(t,0.0,8.5)' -ac 2 -ar 44100 -vcodec libx264 C:\Users\Admin\Desktop\test_movies\test.mp4

Latest edit 3:

As suggested by @Mulvya, I combined his code with alpha=premultiplied and it seems alot better now, with very slight black outline (almost not visible)

Output video: Output video

Sample code:

C:\Users\Admin\Downloads\ffmpeg-20180102-57d0c24-win64-static\bin\ffmpeg -y -ss 0.0 -t 8.5 -i C:\Users\Admin\Desktop\test_movies\white.mp4 -i C:\Users\Admin\Desktop\test_movies\text_and_emoji.png -filter_complex [0:v]setpts=PTS-STARTPTS,scale=640:640:force_original_aspect_ratio=decrease,pad=640:640:(ow-iw)/2:(oh-ih)/2:color=#18ffff[0v];[1:v]premultiply=inplace=1,scale=480:120[1v];[0v][1v]overlay=(W-w)/2-(W/2-325.33328):(H-h)/2-(H/2-567.7075):alpha=premultiplied:enable='between(t,0.0,8.5)':format=rgb,format=yuv420p -ac 2 -ar 44100 -vcodec libx264 C:\Users\Admin\Desktop\test_movies\test.mp4

Upvotes: 4

Views: 2747

Answers (2)

Tucker
Tucker

Reputation: 659

This occured to me due to loss of color definition while using the overlay filter.

The default format of the overlay filter is yuv420.

The yuv420 format will reduce your color definition for increased transmission time. Changing the format to rgb or yuv444 will use full chroma fixing the issue of black outline issue occuring.

YUV is a color space typically used as part of a color image pipeline. It encodes a color image or video taking human perception into account, allowing reduced bandwidth for chrominance components, thereby typically enabling transmission errors or compression artifacts to be more efficiently masked by the human perception than using a "direct" RGB-representation. However, YUV 4:2:2 and YUV 4:2:0 subsampling implements less resolution for chroma information than for luma information, resulting in loss of color definition.

Upvotes: 0

Gyan
Gyan

Reputation: 93369

This is due to a bug in the overlay filter, since fixed. Alter your filtergraph to to this,

"[0:v]setpts=PTS-STARTPTS,scale=640:640:force_original_aspect_ratio=decrease,pad=640:640:(ow-iw)/2:(oh-ih)/2:color=#18ffff[0v];[1:v]premultiply=inplace=1,scale=556.24744:141.41884[1v];[0v][1v]overlay=(W-w)/2-(W/2-325.33328):(H-h)/2-(H/2-567.7075):enable='between(t,0.0,8.5)':format=rgb,format=yuv420p"

You'll need a FFmpeg version from after Dec 16 2017 for this.

Upvotes: 1

Related Questions