如何用ffmpeg为图片拼接的幻灯片添加对应创建日期的文本
Adding Creation Date Overlays to Concatenated Image Video with FFmpeg
To add a unique creation date overlay to each image in your concatenated video, you’ll need to process each image individually with the drawtext filter before combining them. Here’s a practical, automated solution using a Bash script tailored to your setup:
Prerequisites
First, install exiftool (to reliably pull EXIF creation dates from images) if you don’t already have it:
sudo apt install libimage-exiftool-perl # Debian/Ubuntu # For RHEL/CentOS: sudo dnf install perl-Image-ExifTool
Bash Script to Generate and Run the FFmpeg Command
This script reads your existing files.txt (with absolute image paths), extracts each image’s creation date, and builds an FFmpeg command that applies the overlay to every image before concatenation:
#!/bin/bash # Read the list of images from your existing files.txt mapfile -t images < files.txt # Initialize variables for FFmpeg inputs and filter complex ffmpeg_inputs="" filter_complex="" concat_streams="" # Configure drawtext settings (customize these as needed) FONT_PATH="/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" FONT_SIZE=24 FONT_COLOR="white" TEXT_POSITION="x=10:y=10" # Top-left corner; use x=(w-text_w)/2:y=(h-text_h)/2 for center SHADOW_SETTINGS="shadowcolor=black:shadowx=2:shadowy=2" IMAGE_DURATION="3" # Seconds each image is displayed # Loop through each image to build the filter chain for idx in "${!images[@]}"; do img_path="${images[$idx]}" # Extract creation date (fallback to file modification time if EXIF data is missing) creation_date=$(exiftool -d "%Y-%m-%d %H:%M:%S" -CreateDate -s3 "$img_path") if [[ -z "$creation_date" ]]; then creation_date=$(stat -c "%y" "$img_path" | cut -d' ' -f1,2) fi # Add image input with duration ffmpeg_inputs+=" -i \"$img_path\" -t $IMAGE_DURATION" # Add drawtext filter for this image filter_complex+="[${idx}:v]drawtext=fontfile=$FONT_PATH:text='${creation_date}':fontsize=$FONT_SIZE:fontcolor=$FONT_COLOR:$TEXT_POSITION:$SHADOW_SETTINGS[v${idx}];" # Track the filtered stream for concatenation concat_streams+="[v${idx}]" done # Final concat filter to combine all processed streams filter_complex+="${concat_streams}concat=n=${#images[@]}:v=1:a=0[v]" # Build and execute the FFmpeg command ffmpeg_cmd="ffmpeg $ffmpeg_inputs -filter_complex \"$filter_complex\" -map \"[v]\" -c:v libx264 -pix_fmt yuv420p out.mp4" eval $ffmpeg_cmd
Customization Tips
- Font Settings: Adjust
FONT_PATHto a font installed on your system (common options include/usr/share/fonts/truetype/ubuntu/Ubuntu-B.ttfor/usr/share/fonts/truetype/freefont/FreeSansBold.ttf). - Text Position: Change
TEXT_POSITIONto place the date where you want it:- Bottom-right:
x=w-text_w-10:y=h-text_h-10 - Centered:
x=(w-text_w)/2:y=(h-text_h)/2
- Bottom-right:
- Duration: Modify
IMAGE_DURATIONto change how long each image is shown (in seconds). - Resolution Consistency: If your images have different sizes, add a
scalefilter beforedrawtextto standardize them. For example:
Replacefilter_complex+="[${idx}:v]scale=1920:1080,drawtext=...[v${idx}];"1920:1080with your desired resolution.
How It Works
- The script reads your
files.txtto get all image paths. - For each image, it extracts the creation date using
exiftool(or falls back to file modification time if EXIF data is missing). - It builds an FFmpeg filter complex where each image is processed with its own
drawtextoverlay. - Finally, all processed image streams are concatenated into a single video stream, which is encoded to MP4 using H.264.
内容的提问来源于stack exchange,提问作者Hauke
相关产品推荐
相关产品推荐

