如何用ffmpeg-python实现渐变填充文字并完成音视频混音?现有抠像方案优化咨询
Got it, let's tackle your two main issues step by step—first replacing that clunky keying workflow with a clean alpha mask-based text overlay (no brightness loss!), then adding audio mixing to your final video.
1. Gradient-Filled Text with Alpha Mask (ffmpeg-python)
Your original keying approach caused brightness loss because you were double-processing the gradient through color filters. Instead, we'll generate a text alpha mask directly, then use it to "cut out" your gradient image—this preserves the gradient's original brightness perfectly and simplifies the workflow.
Here's the complete code with comments:
import ffmpeg # Define your file paths font_path = 'fonts/my_font.ttf' input_video_path = 'vid01.mp4' gradient_image_path = 'fill_gradient_gold.png' # Your pre-made gradient final_output_path = 'vid01_final.mp4' music_path = 'background_music.mp3' # 1. Set up input sources input_video = ffmpeg.input(input_video_path) gradient = ffmpeg.input(gradient_image_path) background_music = ffmpeg.input(music_path).audio # 2. Generate a text alpha mask # First, create a black background matching the video's resolution black_bg = ffmpeg.input(f'color=c=black:s={input_video["w"]}x{input_video["h"]}', format='lavfi') # Draw white text on the black background text_mask = black_bg.filter( 'drawtext', text='some text', fontcolor='white', fontsize=85, x='(w-text_w)/2', # Center horizontally y='68-(text_h/2)', # Your desired vertical position fontfile=font_path ) # Convert the text layer to a grayscale mask (used for alpha transparency) text_mask = text_mask.filter('format', pix_fmt='gray') # 3. Apply the mask to your gradient image (creates gradient-filled text) gradient_text = ffmpeg.filter( [gradient, text_mask], 'overlay', x='(W-w)/2', y='(H-h)/2', # Center gradient on mask alpha_mask=1 # Use the text mask as alpha channel ) # 4. Overlay the gradient text onto your original video video_with_text = ffmpeg.filter( [input_video, gradient_text], 'overlay', x='(W-w)/2', y='(H-h)/2' # Position text on video ) # 5. Mix audio: Combine original video audio with background music mixed_audio = ffmpeg.filter( [input_video.audio, background_music], 'amix', inputs=2, duration='longest', # Match longer of the two audio sources weights='1 0.5' # Optional: Adjust volume balance (1 = original audio, 0.5 = music) ) # 6. Output the final video with text and mixed audio ffmpeg.output(video_with_text, mixed_audio, final_output_path).overwrite_output().run()
Key Improvements:
- No brightness loss: We're using your original gradient image directly, only masking out the text shape instead of altering colors via keying.
- Simplified workflow: No multiple splits or colorkey filters—just a single mask generation and overlay step.
2. Audio Mixing Notes
- If your original video has no audio, replace the
mixed_audioline withmixed_audio = background_music. - Adjust the
weightsparameter inamixto tweak volume balance (e.g.,weights='1 0.3'makes the music quieter). - If you want the music to fade out at the end, add an
afadefilter to the music input:
(This fades out the music starting at 30 seconds, over 5 seconds.)background_music = ffmpeg.input(music_path).audio.filter('afade', t='out', st='30', d='5')
内容的提问来源于stack exchange,提问作者Roman March
相关产品推荐
相关产品推荐

