php - Making Video in Laravel 11 - Stack Overflow

In my program, we can build video using images and audio, collect the images with length and audio file

In my program, we can build video using images and audio, collect the images with length and audio file to create the video file. I'm having a problem like Failed to create video: The command "C:\ffmpeg\bin\ffmpeg.EXE -y -f concat -safe 0 -i C:\xampp\htdocs\project\storage\app/private\temp/8eddf84c-690c-4244-ba14-b2070459e32e/filelist.txt -vf scale=1280:720 -r 25 -i C:/xampp/htdocs/project/public/storage/videomakingaudio/1742648144_I2DwaAZKky.mp3 -map 0:v:0 -map 1:a:0 -c:v libx264 -pix_fmt yuv420p -c:a aac -shortest C:\xampp\htdocs\project\storage\app/private\public/videos/video_8eddf84c-690c-4244-ba14-b2070459e32e.mp4" failed.

Please check my code

`// Create unique folder for this process
    $processId = Str::uuid();
    $tempPath = "temp/{$processId}";
    $outputPath = "public/videos";
    
    Storage::makeDirectory($tempPath);
    Storage::makeDirectory($outputPath);
    
    try {
        // Save images to disk
        $imageFiles = [];
        $imageCount = 0;
        
        foreach ($request->file('images') as $imageFile) {
            $fileName = "{$imageCount}.jpg";
            $path = $imageFile->storeAs($tempPath, $fileName);
            $imageFiles[] = Storage::path($path);
            $imageCount++;
        }
        
        // Save audio file if provided
        $audioPath = null;
        if ($request->hasFile('audio')) {
            $audioFile = $request->file('audio');
            $audioPath = $audioFile->storeAs($tempPath, 'audio.' . $audioFile->getClientOriginalExtension());
            $audioPath = Storage::path($audioPath);
        }
        
        // Create a text file with file paths for ffmpeg
        $fileListPath = Storage::path("{$tempPath}/filelist.txt");
        $fileContents = '';
        
        $duration = $request->input('duration', 2); // Default 2 seconds per image
        
        foreach ($imageFiles as $img) {
            $fileContents .= "file '{$img}'\n";
            $fileContents .= "duration {$duration}\n";
        }
        
        // Add the last image with a very small duration to avoid issues with certain players
        if (count($imageFiles) > 0) {
            $fileContents .= "file '{$imageFiles[count($imageFiles) - 1]}'\n";
            $fileContents .= "duration 0.1\n";
        }
        
        file_put_contents($fileListPath, $fileContents);
        
        // Output video name
        $videoFileName = "video_{$processId}.mp4";
        $outputVideoPath = Storage::path("{$outputPath}/{$videoFileName}");
        
        // Command for creating video from images
        $ffmpegCommand = [
            'ffmpeg',
            '-y',  // Overwrite output files
            '-f', 'concat',
            '-safe', '0',
            '-i', $fileListPath,
            '-vsync', 'vfr',
            '-pix_fmt', 'yuv420p'
        ];
        
        // Add audio if provided
        if ($audioPath) {
            $ffmpegCommand = array_merge($ffmpegCommand, [
                '-i', $audioPath,
                '-c:v', 'libx264',
                '-c:a', 'aac',
                '-shortest'  // End when the shortest input stream ends
            ]);
        } else {
            $ffmpegCommand = array_merge($ffmpegCommand, [
                '-c:v', 'libx264',
            ]);
        }
        
        // Complete the command with output file
        $ffmpegCommand[] = $outputVideoPath;
        
        // Execute ffmpeg command
        $process = new Process($ffmpegCommand);
        $process->setTimeout(300); // 5 minutes timeout
        $process->run();` 

Uploaded the photographs, created a file, and included the image path in the file content. I verified the file, the path is correct, and I also checked the audio path, which is correct, but I am still facing that issue.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744313917a4568086.html

相关推荐

  • php - Making Video in Laravel 11 - Stack Overflow

    In my program, we can build video using images and audio, collect the images with length and audio file

    7天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信