求助:如何开发将视频转换为MP4并烧录元数据日期时间戳的应用
Hey Jacob, great job getting the timestamp extraction part down—let’s dive into how to burn that timestamp into your video and convert it to MP4. The industry standard tool for this is FFmpeg; it’s free, powerful, and handles both video conversion and text overlay perfectly.
Step 1: Install FFmpeg (if you haven’t already)
- On macOS:
brew install ffmpeg - On Linux (Debian/Ubuntu):
sudo apt update && sudo apt install ffmpeg - On Windows: Download the official build and add it to your system PATH.
Step 2: Basic Timestamp Overlay + MP4 Conversion
Since you already know how to extract the timestamp from metadata, let’s assume you’ve stored it in a variable (e.g., TIMESTAMP="2024-05-20 14:30:00"). Use this FFmpeg command to burn it in and convert to MP4:
ffmpeg -i input_video.mp4 -vf "drawtext=text='${TIMESTAMP}':fontsize=24:fontcolor='yellow':x=10:y=10" -c:v libx264 -c:a aac output_video.mp4
Breakdown of the command:
-i input_video.mp4: Points to your source video file.-vf "drawtext=...": Applies the video filter to overlay text. Here’s what each part means:text='${TIMESTAMP}': Inserts your pre-extracted timestamp (replace the variable with your actual text if needed).fontsize=24: Adjust this to make the text larger/smaller.fontcolor='yellow': Pick any color name or hex code (e.g.,#FFFFFFfor white).x=10:y=10: Positions the text 10 pixels from the top-left corner.
-c:v libx264: Encodes the video using H.264 (the standard for MP4 files).-c:a aac: Encodes the audio using AAC (also standard for MP4).output_video.mp4: Your final converted, timestamped MP4.
Step 3: Advanced Tweaks for Better Results
1. Pull Timestamp Directly from Metadata (Skip Manual Extraction)
FFmpeg can read video metadata directly, so you don’t even need to extract the timestamp first if your video’s metadata has a creation_time field (most do):
ffmpeg -i input_video.mp4 -vf "drawtext=text='%{metadata:creation_time}':fontsize=24:fontcolor='yellow':x=10:y=10" -c:v libx264 -c:a aac output_video.mp4
2. Add a Semi-Transparent Background
Make the timestamp easier to read over busy video by adding a black semi-transparent box:
ffmpeg -i input_video.mp4 -vf "drawtext=text='${TIMESTAMP}':fontsize=24:fontcolor='yellow':x=10:y=10:box=1:boxcolor=black@0.5:boxborderw=5" -c:v libx264 -c:a aac output_video.mp4
@0.5sets the box to 50% transparency (adjust from 0 to 1 for more/less opacity).boxborderw=5adds padding around the text.
3. Position Timestamp in the Bottom-Right Corner
Instead of hardcoding pixel positions, use dynamic values to align the text to the bottom-right, regardless of video resolution:
ffmpeg -i input_video.mp4 -vf "drawtext=text='${TIMESTAMP}':fontsize=24:fontcolor='yellow':x=w-tw-10:y=h-th-10" -c:v libx264 -c:a aac output_video.mp4
w= video width,tw= text width;h= video height,th= text height. This keeps the text 10 pixels away from the bottom-right edge.
4. Customize Timestamp Format
If the raw metadata timestamp is in an ugly format (e.g., 2024-05-20T14:30:00Z), format it first using your shell’s date command:
# Extract raw timestamp from metadata RAW_TIMESTAMP=$(ffmpeg -i input_video.mp4 2>&1 | grep -oP '(?<=creation_time : ).*') # Format into a friendlier string (e.g., "May 20, 2024 2:30 PM") FORMATTED_TIMESTAMP=$(date -d "$RAW_TIMESTAMP" +"%b %d, %Y %l:%M %p") # Burn into video ffmpeg -i input_video.mp4 -vf "drawtext=text='${FORMATTED_TIMESTAMP}':fontsize=24:fontcolor='yellow':x=10:y=10" -c:v libx264 -c:a aac output_video.mp4
Troubleshooting Tips
- If you get a "font not found" error, specify a custom font file with
fontfile=/path/to/your/font.ttfinside the drawtext filter. - For large videos, add
-preset medium(orfastfor quicker processing) to balance speed and quality:-c:v libx264 -preset medium -c:a aac.
内容的提问来源于stack exchange,提问作者Jacob

