What do you do when you face massive files you need to work with? Go back to the roots, in this case, the command line.

The main stage at the WeAreDevelopers world congress has a lot of cameras that all record in 4K which results in footage stored in huge files:

A folder full of video files ranging from 40 GB to 5 GB

To create social media posts and shorts from the talks we needed to cut the recording of the whole stage into smaller videos of each talk without changing the resolution. So we needed a way to cut several huge files from one time stamp to another. Opening files that huge in an editor would be slow going, even on beefy machines and with streaming proxies. I neither had fancy editors nor more than a M2 Mac Mini. But I had the power of the command line and scripting skills. So here’s how you cut chunks out of several huge video files using PHP and FFMPEG.

First, here is the command to FFMPEG to cut a part out of a video from timestamp to timestamp, for example from 1:22:00 to 1:56:00:

ffmpeg -ss 1:22:00 -to 1:56:00 -i hugevideo.mp4 -c copy partvideo.mp4

Notice this is a copy command, there is no re-encoding or change to the video, which means it is pretty fast.

Next I needed to loop over all the videos I wanted to edit. For this, I’m using PHP but you can also use Bash, Python or anything else you fancy. The script I ended up with was this:


// Define FFMPEG as the tool and set it to only report errors, not log the whole process
$tool = 'ffmpeg -hide_banner -loglevel error ';

// The videos to batch edit - same recording from different angles
$mp4s = array(
    "WaD25 Cam1_ch1_0106_0001.mp4", 
    "WaD CamClean_ch4_0106_0001.mp4", 
    "WaD25 Cam2_ch2_0106_0001.mp4", 
    "WaD25 CamPGM_ch3_0106_0001.mp4",
);

// run the FFMPEG command on each of them
foreach ($mp4s as $mp4) {
    echo "Processing $mp4 \n\n";
    $start = '1:22:00';
    $end = '1:56:00';
    $chunkname = str_replace('.mp4', '-chunked.mp4', $mp4);
    $cmd = $tool. "-ss $start -to $end -i '$mp4' -c copy '$chunkname'";
    exec($cmd);
}

Here’s the script in action, cutting 6GB files from the 43GB ones in under a minute (sped up of course):

the script in action showing how it creates shorter videos from the huge ones

If you ever find yourself having to edit massive files like that, this is one way of doing it.

Other interesting articles:
PHP
See all articles