Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: Extracting raw audio with FFmpeg (Read 17917 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Extracting raw audio with FFmpeg

I want to extract the original unaltered audio from a video file using FFmpeg.  I'd like to use it in a batch file to process a whole directory of various video files (MP4, FLV, MOV) where the original audio might be in different formats.  If I try the following command without specifying an extension for the audio filename, then I get an error:
Code: [Select]
ffmpeg -i test.mp4 -acodec copy extractedaudio
FFmpeg version UNKNOWN, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: --disable-shared --enable-memalign-hack --enable-gpl --cpu=i686
--enable-libmp3lame --enable-libfaac --enable-libamr-nb --enable-libamr-wb
  libavutil     50. 3. 0 / 50. 3. 0
  libavcodec    52.23. 0 / 52.23. 0
  libavformat   52.32. 0 / 52.32. 0
  libavdevice   52. 2. 0 / 52. 2. 0
  libswscale     0. 7. 1 /  0. 7. 1
  built on Apr  4 2009 11:36:42, gcc: 4.2.1-sjlj (mingw32-2)

Seems stream 1 codec frame rate differs from container frame rate: 59.94 (2997/5
0) -> 29.97 (30000/1001)
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test.mp4':
  Duration: 00:03:05.61, start: 0.000000, bitrate: 2128 kb/s
    Stream #0.0(und): Audio: aac, 44100 Hz, stereo, s16
    Stream #0.1(und): Video: h264, yuv420p, 1280x720 [PAR 1:1 DAR 16:9], 29.97 t
br, 29.97 tbn, 59.94 tbc
Unable to find a suitable output format for 'extractedaudio'


If I specify an extension (like extractedaudio.aac), then it works (although it says it is encoding, so I'm not sure if it is re-encoding it or not), but then since I want to use it in a batch file to process an entire directory of files, I'm not going to know the extension ahead of time.

 

Extracting raw audio with FFmpeg

Reply #1
Code: [Select]
#!/bin/sh
#usage: sh ff.sh <input file>

input="${1}"

# get the first audio stream's codec.
codec=`ffmpeg -i "${input}" 2>&1\
  |sed -n "s/^.*Audio: \([^,]*\),.*/\1/p"\
  |head -n 1`

# assume that the output format's extension is the same as the codec.
# note that this is the case only in rare cases.
ffmpeg -i "${input}" -acodec copy -y "extractedaudio.${codec}"

Extracting raw audio with FFmpeg

Reply #2
# assume that the output format's extension is the same as the codec.
# note that this is the case only in rare cases.

Thanks for the script.  Did you mean to say "note that this is not the case only in rare cases"?

Extracting raw audio with FFmpeg

Reply #3
Did you mean to say "note that this is not the case only in rare cases"?

If I e.g. run the script on a VOB containing LPCM I get the following error:

Code: [Select]
Unable to find a suitable output format for 'extractedaudio.pcm_s16be'

You may further improve the script along the following lines:

Code: [Select]
#!/bin/sh
#usage: sh ff.sh <input file>

input="${1}"

# get the first audio stream's codec.
codec=`ffmpeg -i "${input}" 2>&1\
  |sed -n "s/^.*Audio: \([^,]*\),.*/\1/p"\
  |head -n 1`

case ${codec} in
  pcm_dvd)
    ffmpeg -i "${input}" -acodec pcm_s24le -y "extractedaudio.wav"
   ;;
  pcm_s16be)
    ffmpeg -i "${input}" -acodec pcm_s16le -y "extractedaudio.wav"
   ;;
  pcm_*)
    ffmpeg -i "${input}" -acodec copy -y "extractedaudio.wav"
   ;;
  *)
    ffmpeg -i "${input}" -acodec copy -y "extractedaudio.${codec}"
esac

Extracting raw audio with FFmpeg

Reply #4
You might try SUPER[/color]. 

SUPER is a (FREE!!!) Windows/GUI A/V conversion program (that uses FFMPEG and MENCODER).  It has options for "audio only" and "stream copy", and if you check both of those boxes you can generally extract the audio without altering it.

Extracting raw audio with FFmpeg

Reply #5
case ${codec} in
  pcm_dvd)
   ffmpeg -i "${input}" -acodec pcm_s24le -y "extractedaudio.wav"
  ;;
  pcm_s16be)
   ffmpeg -i "${input}" -acodec pcm_s16le -y "extractedaudio.wav"
  ;;


Just curious, since you're not using the "-acodec copy" for those two, is it still a lossless extraction?

Quote
pcm_*)
   ffmpeg -i "${input}" -acodec copy -y "extractedaudio.wav"
  ;;


This is useful, thanks for including it!  I have a video with pcm_u8 audio, and now I know I can just use a .wav extension.

Quote
*)
   ffmpeg -i "${input}" -acodec copy -y "extractedaudio.${codec}"
esac


I have some videos with "adpcm_ima_qt" audio and other videos with "qdm2" audio.  Do you or another else have any idea what ffmpeg command would be needed to extract those?  I tried various commands and nothing worked.