Use a single FFmpeg command with the
scale and
drawtext filters to resize 720p video and overlay text simultaneously. This approach ensures high-quality output by chaining operations efficiently without intermediate files.
Understanding the Filter Chain
The core of this operation relies on FFmpeg's filter graph capability. Instead of running separate commands for scaling and watermarking, you combine them into one
-vf (video filter) argument. The
scale filter handles resolution changes, while
drawtext manages the visual overlay. This method is significantly faster than processing the file twice because it avoids re-encoding steps between operations. You must ensure that the input video dimensions are compatible with the target resolution to maintain aspect ratio integrity during the scaling process.
Scaling Logic
When targeting 720p, the standard resolution is
1280x720. However, if your source video has a different aspect ratio, simply forcing these dimensions might stretch the image. To prevent distortion, use the
scale=-2:720 syntax. The negative width value tells FFmpeg to calculate the width automatically based on the height and the original aspect ratio. This ensures the video fills the vertical space without altering the horizontal proportions unnaturally. The result is a clean, properly proportioned 720p frame ready for further processing.
Watermark Placement
The
drawtext filter allows precise control over where the text appears. By specifying coordinates such as
x=(w-tw)/2 and
y=h-th-10, you can center the text horizontally and place it near the bottom of the frame. Here,
w and
h represent the frame width and height, while
tw and
th represent the text width and height. This dynamic positioning ensures the watermark stays centered regardless of slight variations in video length or specific encoding settings. Adjusting the
fontcolor and
fontsize parameters helps make the text legible against various background scenes.
Constructing the Command
A complete command integrates these filters with the necessary codec settings. Below is a robust example that scales the video to 720p and adds a copyright notice at the bottom center.
ffmpeg -i input.mp4 \
-vf "scale=-2:720,drawtext=text='Copyright 2023':fontcolor=white:fontsize=24:x=(w-tw)/2:y=h-th-10" \
-c:v libx264 -preset fast -crf 23 output.mp4
In this command, the
-i flag specifies the input file. The
-vf argument contains the comma-separated list of filters. First,
scale=-2:720 resizes the video. Second,
drawtext=... applies the text overlay. The
-c:v libx264 option selects the H.264 encoder, which is widely supported. The
-preset fast balances encoding speed and quality, while
-crf 23 sets a constant rate factor for consistent visual fidelity. You can adjust the
CRF value lower for higher quality or higher for smaller file sizes.
Customizing Text Appearance
To improve readability, consider adding a shadow or outline to the text. You can achieve this by modifying the
drawtext parameters. For instance, using
shadowcolor=black:shadowx=2:shadowy=2 adds a subtle drop shadow. This technique prevents white text from blending into bright parts of the video. Experiment with different fonts by specifying the
fontfile path if you need custom typography. Ensure the font file is accessible to the system running FFmpeg, or stick to default system fonts for broader compatibility.
Performance Considerations
Processing video with multiple filters can be CPU-intensive. Using the
libx264 encoder with a
fast preset reduces encoding time significantly compared to
slow or
veryslow. If you are processing large batches of videos, consider using hardware acceleration if available. For NVIDIA GPUs, you can replace
libx264 with
h264_nvenc and add appropriate
-gpu flags. This shift offloads the encoding task from the CPU to the GPU, allowing for much faster throughput. Always monitor your system resources when running intensive multimedia tasks to avoid bottlenecks.
Verifying the Output
After the command completes, verify the output file to ensure both scaling and watermarking were applied correctly. Play the video in a media player and check the resolution properties. Use a tool like ffprobe to inspect the stream details if needed. Look for the presence of the text overlay in every frame, especially in dark scenes where contrast issues might arise. If the text is not visible, adjust the
fontcolor or add a background box using
box=1 within the
drawtext filter.
Batch Video Processing with FFmpeg and Optimize Video Quality vs Size provide additional context for managing larger projects.
Frequently Asked Questions
Why does my video look stretched after scaling?
This happens when you force exact dimensions without preserving the aspect ratio. Always use the
scale=-2:height syntax to let FFmpeg calculate the correct width automatically based on the original proportions.
Can I add an image watermark instead of text?
Yes, use the
overlay filter instead of
drawtext. Specify the position of the image relative to the main video using x and y coordinates within the filter chain.
How do I fix blurry text in the output?
Increase the
fontsize parameter in the
drawtext filter. Also, ensure you are not downscaling the video excessively before applying the text, as low resolution makes small text unreadable.
Is it better to scale first or watermark first?
It is generally better to scale first. Applying filters in order affects the final pixel data; scaling first ensures the watermark is positioned correctly on the final output dimensions rather than the source dimensions.
What if the watermark covers important content?
Adjust the
x and
y coordinates in the
drawtext filter to move the text to a less intrusive area. You can also use dynamic expressions to avoid specific regions if the video has consistent layouts.