Robert Melton's Writings

Bash Wizardry: The 30-Second Video Editor for Impatient Developers

Listen up, code jockeys. If you’ve ever watched your own demo video and thought, “Dear god, do I really talk this slowly?”, then this post is for you. I’m about to introduce you to a bash script that’ll make your videos tighter than your project deadlines.

First, feast your eyes on this disgusting little bit of bash:

#!/bin/bash

# Ensure the correct number of arguments is given
if [ "$#" -ne 3 ]; then
    echo "Usage: $0 <video file> <start time in seconds> <end time in seconds>"
    exit 1
fi

# Assign command line arguments to variables
VIDEO_FILE="$1"
START_TIME="$2"
END_TIME="$3"

# Create a backup of the original file with a timestamp
BACKUP_FILE="${VIDEO_FILE%.*}_backup_$(date +%Y%m%d%H%M%S).${VIDEO_FILE##*.}"
cp "$VIDEO_FILE" "$BACKUP_FILE"
echo "Backup of original file created at: $BACKUP_FILE"

# Perform the jump cut including video and audio streams
ffmpeg -i "$VIDEO_FILE" -filter_complex \
"[0:v]trim=start=0:end=$START_TIME,setpts=PTS-STARTPTS[v0]; \
 [0:a]atrim=start=0:end=$START_TIME,asetpts=PTS-STARTPTS[a0]; \
 [0:v]trim=start=$END_TIME,setpts=PTS-STARTPTS[v1]; \
 [0:a]atrim=start=$END_TIME,asetpts=PTS-STARTPTS[a1]; \
 [v0][v1]concat=n=2:v=1[v]; \
 [a0][a1]concat=n=2:v=0:a=1[a]" -map "[v]" -map "[a]" temp_output.mp4

# Check if ffmpeg succeeded
if [ $? -eq 0 ]; then
    mv temp_output.mp4 "$VIDEO_FILE"
    echo "Jump cut successful. Original video file updated."
else
    echo "Error during video editing. Check ffmpeg output for details."
    exit 2
fi

Now, I know what you’re thinking. “But I’m a developer, not a video editor!” Exactly. That’s why we’re doing this the programmer way: efficient, iterative, and fueled by caffeine and impatience.

Here’s the workflow that’s saved my bacon more times than I can count:

  1. Fire up VLC and start watching your video. Try not to cringe too hard.
  2. Hit a boring bit? Pause. Note the timestamp.
  3. Watch until the boring bit ends. Note that timestamp too.
  4. Alt-tab to your terminal faster than you abandon side projects.
  5. Run the script:
./jumpcut.sh my_boring_demo.mp4 120 145
  1. Jump back in VLC to about 5 seconds before your cut.
  2. Rinse and repeat until your video is tighter than your unit tests.

Some call this “trim and join,” but those are video people. We’re programmers, dammit. We call it a jump cut because it sounds cooler and makes us feel like we’re directing “The Matrix” from our terminals.

The beauty of this setup? It’s incremental editing for the impatient developer. You’re not trying to create a cinematic masterpiece here. You’re trying to make your demo video watchable without putting your audience into a coma.

And here’s the kicker: with the backups this script creates, you can always go back to an older version. It’s like Git for your videos, minus the merge conflicts and existential dread.

Now, I’ll be honest. At some point, I’ll probably add multiple parameters to this script. Because let’s face it, once you start optimizing, it’s hard to stop. But for now, this works. It’s the MVP of video editing scripts.

Is it going to replace your professional video editing suite? Not unless your idea of color grading is changing your terminal theme. But it will save your bacon when you need to tighten up that demo video five minutes before the client call.

And let’s be real: how many useful wrappers could we build around ffmpeg? That thing has more options than a Vim enthusiast has plugins. We’ve barely scratched the surface here, folks. The possibilities are endless, limited only by our imagination and how much we can abuse our poor CPUs.

So there you have it. A simple bash script and workflow that turns you into a command-line video editing ninja. It’s not going to win you an Oscar, but it might just save you from the embarrassment of watching your audience’s eyes glaze over during your next demo.

Now if you’ll excuse me, I’ve got some videos to cut. These awkward pauses aren’t going to remove themselves, and I’ve got a demo in… oh crap, 15 minutes. Time to make the donuts, folks!

Remember: tighten up those demo videos, developers. Your audience’s attention span will thank you.