Ffmpeg commands

CLI >

Cutting up an audio track


ffmpeg makes this very easy indeed - and its a program already installed on your system, hopefully.

the command needed looks like this

ffmpeg -ss {sec} -t {sec} -i file -acodec copy output-file

-ss                             option for start position in the track - either hh:mm:ss or just seconds
-t                               the duration time to cut until, also hh:mm:ss or just seconds
  (I tried the option -to which sets the cut-up-to point, and is found in ffmpeg man page, but it doesn't work at all)
-i                                the input file
-acodec or -c:a            sets the audio codec to use - and "copy" keeps the original codec and quality
-aq or -q:a                   will set the audio quality (VBR or variable bitrate), e.g. -aq 200
-ab or -b:a                  will set audio bitrate, e.g. -ab 192k

It would follow that video files can be cut as well, but with compressed video files the results might not be perfect, as with h264 mp4 if you make cuts on frames that are not "key frames" you will very likely end up with smearing of video data (blocky picture at the cut point).  So Avidemux would be a safer option with cutting compressed video, but with raw AVI you could try ffmpeg.

This is how i cut up an audio track...
  1. open track in audacity
  2. select the part of the track to cut
  3. set the selection measure at bottom to "Length" and units of seconds
  4. copy and insert the start/duration on the ffmpeg line, e.g.
ffmpeg -ss 34 -t 572 -i file -acodec copy output-file

I'm using ranger as well, so I have a mapped key with the ffmpeg line, like this
map fvf2 ffmpeg -ss 00 -i %%f -acodec copy %%f_

I noticed a glitch, however, which I couldn't sort out.  For three out of 20 cuts I made, the file produced was only 1K in size -and I checked and rechecked the values on the ffmpeg line. Hmm.. so for those few what I did was export the selection straight from Audacity - change output format to "(external program)" and in Options under that, select ffmpeg.  
It works just as well, although it seems to take a little longer to process (because a file opened in audacity is first conveeted to raw data so that edits and effects can be made - which means exporting a chunk of a track will require that audacity re-encode it to a lossy format, hence the little extra time it takes compared to cutting a lossy format file with ffmpeg).

ffmpeg results!  Most of the cut tracks have up to 10 or 15 sec of the previous track ending on the front of them, sometimes the start of the next track at the end... even when I was precise with the values taken from audacity, so what's going on there???
--> because of that, I found it easier to export the selection from audacity as ogg file, q 8.


Audio Conversions

http://linuxconfig.org/ffmpeg-audio-format-conversions

I have some useful video conversion commands on this page transcoding-mts


flac to ogg
find . -depth -name "*.flac" -exec ffmpeg -i {} -c:a libvorbis -q:a 7 {}.ogg \;

this searches to the lowest levels of directories first, built on the basic form of
ffmpeg -i file.flac -c:a libvorbis -q:a 7 file.ogg

the problem with the recursive command above is that it's not very tidy, results-wise, as find just puts ".ogg" onto the end of the full file name with extension, so song.flac > song.flac.ogg

a script makes the results tidier, without the need to search/replace "flac.ogg" everywhere
#!/bin/bash
for a in *.flac; do
 </dev/null ffmpeg -i "$a" -c:a libvorbis -q:a 7 "${a%.*}.ogg 
done

this searches in the current directory.  to make it recursive you can try double globbing: for a in ./**/*.flac; do

there is also oggenc
find . -name "*flac" -exec oggenc -q 7 {} \;

also dir2ogg
dir2ogg -q 8 -n -r --convert-all /dir


flac to mp3
find . -depth -name "*.flac" -exec ffmpeg -i {} -c:a libmp3lame -q:a 0 {}.mp3 \;

or a script,
for a in *.flac; do
 </dev/null ffmpeg -i "$a" -c:a libmp3lame -q:a 0 "${a%.*}.mp3 
done

Tip: add the option -size to hit files of certain size and above, e.g.
find . -depth -name "*.flac" -size +4M -exec ffmpeg -i {} -c:a libmp3lame -q:a 0 {}.mp3 \;
size can be in k as well, like 4000k = 4M


ogg to mp3
find . -depth -name "*.ogg" -exec ffmpeg -i {} -c:a libmp3lame -q:a 0 {}.mp3 \;

there's a script I found ogg2mp3 https://github.com/monksy/ogg2mp3/... but this might be obsolete having a good ffmpeg install
sudo apt-get install vorbis-tools

copy the script (bottom of page) and save to file ogg2mp3 in your scripts
chmod +x ogg2mp3

make a bash alias
alias ogg2mp3='ogg2mp3 -B 256 --vbr-new -V 0 *.ogg'


Split audio from a video

get audio metadata with
ffmpeg -i video.avi etc

examples
ffmpeg -i input.mpg -c copy audio.m4a     - AAC
ffmpeg -i input.flv -c copy audio.mp3       - MP3
ffmpeg -i input.mpeg -c copy audio.ac3    - AC3

or you can specify quality/codec
ffmpeg -i video.flv -vn -ar 44100 -ac 2 -ab 192k -f mp3 output.mp3