Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have several 100 videos that I need to remove the first 20 seconds and the last 30 seconds of.

Obviously not practical to do this one at a time. Can I automate this somehow, maybe in an OS X Automator script?

share|improve this question
up vote 1 down vote accepted

Assuming you run *nix and Bash, download an ffmpeg static build for your operating system. Then, take the ffmpeg file and copy it to /usr/bin/ffmpeg.

sudo cp ~/Downloads/ffmpeg/ffmpeg /usr/bin/ffmpeg
sudo chmod +x /usr/bin/ffmpeg

Now you can run this shell script in Bash — should be enough to copy-paste it in the folder where the videos are.

for f in *.mp4; do
    duration=$(ffmpeg -i "$f" 2>&1 | grep "Duration"| cut -d ' ' -f 4 | sed s/,//)
    length=$(echo "$duration" | awk '{ split($1, A, ":"); print 3600*A[1] + 60*A[2] + A[3] }' )
    trim_end=$(echo "$length" - 30 - "$trim_start" | bc)
    echo ffmpeg -ss "$trim_start" -i "$f" -c copy -map 0 -t "$trim_end" "${f%.mp4}-trimmed.mp4"

Note: Remove the echo statement to actually perform the commands. It would be good to test the generated commands first for accuracy. This would generate something like:

ffmpeg -ss 20 -i input.mp4 -c copy -map 0 -t 161.58 input-trimmed.mp4

What the script will do is calculate the total running time of the input, subtract the start time and the time you want to remove from the end, and then tell ffmpeg to do a stream-copy from your specified start time to the newly calculated end time. This will not harm the video and audio quality in any way. It should run within less then a few seconds even for large files.

To clarify, -ss sets the start time and -c copy -map 0 will copy all video, audio and subtitle streams from the original to the cut version. -t sets the end time. See this article on the FFmpeg Wiki for more info about seeking.

share|improve this answer

If you're comfortable with command line, I would recommend FFmpeg

ffmpeg -i file.mp4 -vcodec copy -acodec copy -ss 00:00:20.000 -t 00:00:30.000 output.mp4

Another option is a small shell script which loops through your files and runs a single-file FFmpeg job for each of them. Then again, I never used a Mac shell :)

for f in *.mp4; do ffmpeg -i $f -vcodec copy -acodec copy -ss 00:00:20.000 -t 00:00:30.000 output/$f; done

Hope this helps a little bit.

share|improve this answer
The OP needs to remove the last 30 seconds, not encode for 30 seconds, so your approach does not work. Also, the input syntax with %d is only supported for images (more specifically the image2 demuxer). – slhck Nov 24 '13 at 17:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .