Posts Tagged ‘ffmpeg’

Split a long mp3 audio file into 3 min files

Wednesday, July 29th, 2015

After some frustration trying to get mp3splt to work, I caved in and wrote a script to split apart a large audio file into many 3min chunks. Save this in

duration_stamp=$(ffmpeg -i "$big" 2>&1 | grep Duration | sed 's/^.*Duration: *\([^ ,]*\),.*/\1/g')
title=$(ffmpeg -i "$big" 2>&1  | grep "title *:" | sed 's/^.*title *: *\(.*\)/\1/g')
# get minutes as a raw integer number (rounded up)
prefix=$(basename "$big" .mp3)
echo $duration_stamp
mins=$(echo "$duration_stamp" | sed 's/\([0-9]*\):\([0-9]*\):\([0-9]*\)\.\([0-9]*\)/\1*60+\2+\3\/60+\4\/60\/100/g' | bc -l | python -c "import math; print int(math.ceil(float(raw_input())))")
total_count=$(echo "$mins/3+1" | bc)
while [ "$ss" -lt "$mins" ]
  zcount=$(printf "%05d" $count)
  ss_hours=$(echo "$ss/60" | bc)
  ss_mins=$(echo "$ss%60" | bc)
  ss_stamp=$(printf "%02d:%02d:00" $ss_hours $ss_mins)
  ffmpeg -i "$big" -acodec copy -t 00:03:00 -ss $ss_stamp -metadata track="$count/$total_count" -metadata title="$title $zcount" "$prefix-$zcount.mp3" 

The execute my-long-file.mp3. This will output a sequence of files:


Each will retain the meta data from the original file except the file number will be appended to the track name and the track number will be set accordingly (i.e. this will work well for splitting enormous audiobook files into file lists that play in the correct sequence on an iphone).

Note: mp3splt really seems like the right tool for this. It supposedly has fancy features like silence detection and presumably won’t reload the file for each new split.

Convert all .mkv files in a directory to .mp4 for ipad

Sunday, March 30th, 2014

Here’s a bash “one-liner” to convert all movie files in the current directory to a format that works with the ipad. It renames the files from .mkv to .mp4.

for f in *.mkv; do ffmpeg -i $f -acodec aac -ac 2 -strict experimental -ab 160k -s 1920x1080 -vcodec copy -f mp4 -threads 0 ${f%.*}.mp4; done

Convert mkv into high quality mp4 for iPad

Friday, November 1st, 2013

Here’s what I’m using to convert 1080p BluRay rips from .mkv to .mp4s for playing back on the iPad.

ffmpeg -i input.mkv -acodec aac -ac 2 -strict experimental -ab 160k -s 1920x1080 -vcodec copy -f mp4 -threads 0 output.mp4

Update: This only works if the input .mkv file is already using h264 as the video codec (see, it’s copying the video rather than transcoding it). Otherwise, you’ll need this (takes much longer):

ffmpeg -i input.mkv -acodec aac -ac 2 -strict experimental -ab 160k -vcodec libx264 -preset slow -profile:v baseline -level 30 -maxrate 10000000 -bufsize 10000000 -b 1200k -f mp4 -threads 0 output.mp4

Convert a bunch of images into a video, batch job

Friday, October 26th, 2012

I was sure I posted this some time before, but just in case, here it is again. My programs can easily dump jpgs, pngs, or tga image files at every frame. Sometimes it suffices to convert these to an animated GIF using imagemagick. But if there are many images and they’re all hi-res then really you need a proper video codec to get a reasonably small file. I use this ffmpeg command:

ffmpeg -f image2 -r 30 -i myimages-%04d.tga -r 30 -vcodec libx264 -pix_fmt yuv420p -q:vscale 0 output.mp4

or using GLOB for a lexicographical, alphabetical file list:

ffmpeg -f image2 -r 30 -pattern_type glob -i 'my-images-*.png' -r 30 -vcodec libx264 -pix_fmt yuv420p -q:vscale 0 output.mp4

Update : The -pix_fmt yuv420p` is important for getting the output video to play in quicktime.

Ffmpeg to wmv bitrate parameter order gotcha

Tuesday, July 3rd, 2012

I had been using:

ffmpeg -pass 1 -passlogfile ffmpeg2pass -g 1 -b 16000k -i output.wmv
ffmpeg -pass 2 -passlogfile ffmpeg2pass -g 1 -b 16000k -i output.wmv

which at one point gave me an error on the second pass:

[msmpeg4 @ 0x7fde41844e00] requested bitrate is too low

I tried increasing 16000k to 32000k, but this did not make the error go away. Turns out I just needed to switch the order of the parameters:

ffmpeg -pass 1 -passlogfile ffmpeg2pass -g 1 -i  -b 16000k output.wmv
ffmpeg -pass 2 -passlogfile ffmpeg2pass -g 1 -i  -b 16000k output.wmv

Convert video (.mp4 or other) to high quality animated gif

Monday, September 12th, 2011

You can do this directly with ffmpeg, but I had trouble with it and seem to remember the quality not being so good. Instead I convert my .mp4 video into an animated gif by first grabbing 10 frames every second and saving them to files using the following:

ffmpeg -i input.mp4 -r 10 output%05d.png

Then using imagemagick’s convert tool insert each frame into an animated gif:

convert output*.png output.gif

This makes a rather large .gif file for even modest videos so probably you’ll want to post process it (e.g. with Photoshop) to reduce the file size by compressing the .gif file or reducing the dimensions.

Clean up the png files using:

rm output*.png

Convert mov/mp4/etc to wmv high quality with ffmpeg

Wednesday, July 6th, 2011

The following two ffmpeg commands worked well converting my animated movie (with OpenGL screen captures) from a quicktime .mov file to a high quality .wmv file.

ffmpeg -pass 1 -passlogfile ffmpeg2pass -g 1 -i  -b 16000k output.wmv
ffmpeg -pass 2 -passlogfile ffmpeg2pass -g 1 -i  -b 16000k output.wmv

The resulting wmv file is rather large but it doesn’t look like total garbage, so I’m happy.

Extract clip from video and convert to a bunch of formats

Sunday, May 23rd, 2010

I’ve been interested in using video for everybody for which you need a .mp4, .ogv and .flv version of your video. To suck out this clip of Cary Grant from Notorious I used mencoder with the following command:

mencoder -endpos 00:00:01.38 -ss 00:05:35 -of lavf -oac mp3lame -lameopts abr:br=56 -srate 22050 -ovc lavc -lavcopts vcodec=flv:vbitrate=500:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 notorious.1946.x264.mkv -o cary-grant-eyes.flv

That makes a .flv file of a clip from this movie starting at 5m35 seconds and lasting 1.38 seconds. To convert this to a quicktime compatible .mp4 file I use the following ffmpeg command:

ffmpeg -i cary-grant-eyes.flv -vcodec libx264 -vpre libx264-lossless_max cary-grant-eyes.mp4

Finally to convert that .mp4 file to a firefox compatibly .ogv file I use the following ffmpeg2theora command:

ffmpeg2theora cary-grant-eyes.mp4 -o cary-grant-eyes.ogv

Note: It really seems like I should be able to do each of these straight from mencoder but I couldn’t figure out how.

Update: I think is the right way to do it (losslessly) with ffmpeg

ffmpeg -ss 1:42:11 -i JFK.DirCut.1991.1080p.BluRay.DTS.x264-ESiR.mkv -acodec copy -vcodec copy -t 00:00:02.4 joe-pesci-jfk-honk.mkv

This makes a 2.4 second clip starting from 1:42:11 in the movie JFK:

Extract mp3 audio from flv flash video

Thursday, May 20th, 2010

Found this resource about using mplayer or ffmpeg to extract the audio of an flv flash movie (like those from youtube etc.) and save it as an mp3 file.

I adapted the ffmpeg version with the -ab flag that allows me to specify audio bitrate:

ffmpeg -i input.flv -f mp3 -ab 192 -vn ouputfile.mp3

Change the 192 to whatever bitrate you want.