You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在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 -t duration is reached (critical for turning a single image into a video clip)
  • concat filter: Combines the two image video streams into one sequential video
  • Input sanitization: Casting $cap_1 and $cap_2 to floats prevents malicious command injection (always sanitize user input!)
  • Audio mapping: -map 2:a targets 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 fade filter in the filter_complex chain (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 -shortest instead of -t $total_duration if you want the output to end when either the video or audio finishes

内容的提问来源于stack exchange,提问作者730wavy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:50:01