如何简化FFmpeg脚本并添加带定时淡入淡出效果的文字
Simplified FFmpeg Script with Text Fade In/Out
Got it, let’s streamline your FFmpeg script while adding smooth text transitions with proper timing. The original split filter was unnecessary here—we can directly generate a transparent text layer and overlay it onto your video, making the code cleaner and easier to tweak.
Streamlined Script
ffmpeg -y -i video.mp4 -filter_complex " # Create text layer with initial transparency, then add fade effects drawtext=fontfile=gvr.otf:text='Which of these is not an event listener adapter defined in the java.awt.event package?':fontcolor=white:fontsize=40:x=100:y=200:alpha=0, fade=t=in:st=1:d=1:alpha=1, # Fade in starting at 1s, taking 1s to complete fade=t=out:st=8:d=1:alpha=1[text_overlay]; # Overlay the text layer onto the original video [0][text_overlay]overlay" output.mp4
Key Improvements & Customization Tips
- Removed redundant split filter: We don’t need to duplicate the video stream—generating the text layer separately and overlaying it is more efficient and easier to follow.
- Controlled timing:
- Text starts fading in at 1 second (
st=1) and finishes fading in after 1 second (d=1), so it’s fully visible from 2s to 8s. - Fade-out starts at 8 seconds (
st=8) and takes 1 second (d=1), so the text disappears completely by 9s.
- Text starts fading in at 1 second (
- Easy tweaks for your needs:
- To center the text automatically (works for any video resolution), replace
x=100:y=200withx=(w-text_w)/2:y=(h-text_h)/2. - Adjust fade speed: Change
d=1tod=0.5for a faster 0.5s transition, ord=2for a slower, more subtle fade. - Extend text display time: If you want the text to stay visible longer, set the fade-out start time (
st=8) to a later value (e.g.,st=12for 10s of total visible time including fades). - Boost text readability: Add a soft shadow with
shadowcolor=black:shadowx=2:shadowy=2inside thedrawtextfilter.
- To center the text automatically (works for any video resolution), replace
Quick Note
Ensure your source video is at least as long as the total text timeline (in the example, the video needs to be 9s or longer to avoid errors). If your video is shorter, adjust the fade-out start time (st=8) to match the video’s duration minus the fade-out time (e.g., st=5 for a 6s video with 1s fade-out).
内容的提问来源于stack exchange,提问作者Pravesh Kumar
相关产品推荐
相关产品推荐

