在WordPress中用FFmpeg实现多图自定义时长转视频遇问题
Fix: FFmpeg Only Shows One Image in Your WordPress Form-Generated Video
Hey there! Let's break down why your current FFmpeg command is only displaying one image, and fix it to show each image for your specified durations.
The Problem with Your Current Command
Right now, you're adding -t to each image input, but FFmpeg doesn't automatically sequence images with those timestamps. It defaults to using the first input's duration for the whole output, which is why only the first image shows up.
The Solution: Sequence Images with FFmpeg Filters
We need to create individual video streams for each image (with their desired durations) then concatenate them together, plus add your audio. Here's the revised code with explanations:
<?php if (isset($_POST['button'])) { // Sanitize input values to avoid command injection risks! $cap_1 = (float)$_POST['cap_1']; $cap_2 = (float)$_POST['cap_2']; $total_duration = $cap_1 + $cap_2; $ffmpeg_command = 'ffmpeg ' . // Create a looping video stream for the first image (duration = cap_1) '-loop 1 -t ' . $cap_1 . ' -i /myurl/beach-1866431.jpg ' . // Create a looping video stream for the second image (duration = cap_2) '-loop 1 -t ' . $cap_2 . ' -i /myurl/orlando-1104481-1.jpg ' . // Add your audio input '-i /myurl/audio.mp3 ' . // Filter complex: scale images to 1280px width, then concatenate the video streams '-filter_complex "'. '[0:v]scale=1280:-2[v0]; '. // Scale first image '[1:v]scale=1280:-2[v1]; '. // Scale second image '[v0][v1]concat=n=2:v=1:a=0[v]" ' . // Concatenate 2 video streams // Map the concatenated video and audio, trim audio to match total video duration '-map "[v]" -map 2:a -t ' . $total_duration . ' ' . // Define your output file path (adjust this to your needs!) 'your-output-video.mp4'; // Execute the command and capture output (optional: add error handling) $output = shell_exec($ffmpeg_command . ' 2>&1'); echo '<pre>' . htmlspecialchars($output) . '</pre>'; } ?>
Key Changes Explained
-loop 1: Makes each image loop continuously until the-tduration is reached (critical for turning a single image into a video clip)concatfilter: Combines the two image video streams into one sequential video- Input sanitization: Casting
$cap_1and$cap_2to floats prevents malicious command injection (always sanitize user input!) - Audio mapping:
-map 2:atargets the third input (your audio file) and trims it to match the total video duration with-t $total_duration
Additional Tips
- Add error handling: Check if FFmpeg can access your input files and write to the output path
- If you want smooth transitions between images, add a
fadefilter in thefilter_complexchain (e.g.,[v0]fade=t=out:st='.($cap_1-1).':d=1[v0f]; [v1]fade=t=in:st=0:d=1[v1f]; [v0f][v1f]concat...) - Use
-shortestinstead of-t $total_durationif you want the output to end when either the video or audio finishes
内容的提问来源于stack exchange,提问作者730wavy
相关产品推荐
相关产品推荐

