Search code examples
ffmpegterminalvideo-processing

Why is ffmpeg creating extra files?


I'm trying to convert files from url with ffmpeg, and most of the time, I get exactly what I'm looking for:

$video = "tmpVideos/".$img->image_id.$randNum.".mov";
$largeURL = uniqid();
$command = "ffmpeg -i $video -filter:v scale=720:-2:force_original_aspect_ratio=decrease -c:v libx264 -x264-params crf=23 -an large/$largeURL.mp4";
shell_exec($command);

But when the input file is larger than about 2GB, weird things start to happen. Most of the time, it just makes extra files. The file I asked for will be created, and then several more (sometimes 1, sometimes 3) will be made with the same resolution, but with slightly (about 20%) larger filesizes.

Why does this happen? Is there any way to kill ffmpeg when the file I want is created?


Solution

  • After a lot of trial and error, I did solve this.

    I had two problems. One was listed above, and the other was that the 2-4GB files I was downloading would download 1-4 times, and then each would process with ffmpeg 1-4 more times, so I was eating up a lot of processing power and bandwidth.

    Turns out, the entire script was simply repeating itself before it could get to die() or exit(); When it timed out, the php would stop executing (or start from the beginning?), but the download or process would keep going.

    I still don't know why it repeated itself after timing out, but adding ini_set('max_execution_time', '1000'); (1000 or some other large number) did the trick. The script was given sufficient time to execute and die() naturally.