Gregory Hildstrom Projects Publications Resume Links Contact About Google+ Facebook Youtube Donate




cut-cat minimal video editing bash script

Introduction
Usage Example
Testing
Discussion
Real Example

Introduction

Here is the quick description from the
cut-cat.sh script, which relies on ffmpeg. On a related note here is a file full of quick ffmpeg one-liners.
# cut-cat is a minimal video editing bash script that relies on ffmpeg.
# It was developed to edit videos quickly without transcoding, but the ffmpeg
# options could easily be changed to support transcoding. The primary goals
# are speed, ease of use, and stability. Most Linux GUI video editing software
# crashes far too often and point/click/drag/repeat inefficiency seems to slow
# down many simple editing tasks. Input files are not modified. Start and end
# times may not be exact without transcoding in ffmpeg. Things may not work
# properly with different video formats from different sources without
# transcoding. It works great with the H.264/AAC/.mp4 video from my HTC One
# and my GoPro.
#
# cut-cat was inspired by
# http://superuser.com/questions/133413/joining-h264-without-re-encoding
# and developed with ffmpeg version 2.1.4 on Fedora 20. It was developed
# on Linux, but it should work on any platform with bash and ffmpeg like
# MacOS X, Solaris, BSD, etc.
#
# usage information
# cut input_video [optional start time] [optional end time]
# cat output_video

Usage Example

Here is an example
project.sh script that uses cut-cat. Just run the project.sh script in the same directory as the cut-cat.sh script to assemble the output video.
#!/bin/bash
. cut-cat.sh                    # source the cut-cat script
cut video1.mp4                  # use entire video
cut video2.mp4 13 22            # second syntax
cut video2.mp4 1:33 01:59       # minute syntax
cut video3.mp4 0:0:58 00:01:56  # hour syntax
cut video3.mp4 2:38             # use last part of video
cat project.mp4                 # concatenate all of the cuts

Testing

When I started testing, each project used video files from the same source with the same format and settings. As long as the video codec was H.264 and the audio codec was AAC, everything worked great. Here was my test project sim.sh script for editing some similar Go Pro videos together.


The output video played fine using various local media players and YouTube didn't have any trouble with it either.



I thought a much more robust test would be to edit videos together using different sources with slightly different formats. Here's what I used:
cameravideo codecresolutionframes per secondvideo bit rateaudio codecchannelssample rateaudio bit rate
GoPro HD Hero 2H.2641080p3015MAAC248000128K
HTC One (M7)H.264720p3012MAAC24800096K
Casio Exilim EX-S880H.264480p306MIMA ADPCM144100N/A

The Casio didn't use AAC for the audio, so that portion of the first non-transcoded output video didn't have any sound. The quick
fix was to transcode just the audio of the Casio file, to match the audio codec, channels, and sample rate of the other videos, before editing with cut-cat.
[ghildstrom@hplt cut-cat]$ ffmpeg -i casio.mov -c:v copy -c:a aac -b:a 96k -strict -2 -ar 48000 casio.mp4

Here was my test project dis.sh script for editing the dissimilar videos together.
#!/bin/bash
. cut-cat.sh
cut gopro2.mp4 20 29
cut htc.mp4 14 23
cut casio.mp4 14 20
cat dis.mp4

The output video played fine using various local media players. Amazingly, YouTube uploaded the video, but processing definitely choked on the non-transcoded dis.mp4 output video. If the embedded play doesn't work, reload and try "Watch on YouTube.com".


Discussion

This approach allows you to use any video player to determine cut times in the source video files since preview isn't integrated with editing. I have used GNOME Totem, VLC, Windows Media Player, Quicktime, Android, etc. In my testing, I discovered that the source videos don't even need to be same resolution. I created a single non-transcoded video using three different cameras. It played fine in Totem, VLC, and Windows Media Player and it will probably play fine in many other players as well. The Totem player properties shows the resolution changing on the fly during playback. The non-transcoded output file frame rate is the average of the input file frame rates. So, for the best results without transcoding, it is a good idea to keep things as similar as possible on the input side. The output video is accepted by YouTube and probably many other video hosting services.

Real Example

Here's the first real video I edited with cut-cat. I shot the video with my HTC One in 720p. It's a quick video to help fellow Buell motorcycle owners with electrical problems. Using cut-cat was quick and easy. Here's the project script:
#!/bin/bash
. cut-cat.sh
cut sc1.mp4 1 55
cut sc2.mp4 3 20
cut sc3.mp4 1 46
cut sc4.mp4 2 42
cut sc4.mp4 58 1:20
cut sc5.mp4 22 38
cut sc5.mp4 45 58
cut sc5.mp4 1:10 1:32
cat statorcheck.mp4

Here's the resulting output video: