Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out the file extension for extracting audio tracks with ffmpeg and python?

I want my python program to extract the audio tracks from various video files without changing the audio codec. For this I call the following command:

ffmpeg -i "input" -vn -acodec copy "output.???"

However this only works, if the file extension of the output file is known. Is there a way to find out the corresponding file extension?

like image 595
seenorth Avatar asked May 22 '17 23:05

seenorth


People also ask

How do I extract audio from ffmpeg?

The -i option in the above command is simple: it is the path to the input file. The second option -f mp3 tells ffmpeg that the ouput is in mp3 format. The third option i.e -ab 192000 tells ffmpeg that we want the output to be encoded at 192Kbps and -vn tells ffmpeg that we dont want video.

How to extract audio from a video file using Python?

We need to import the moviepy package or the editor class alone specifically. Then we need to create a VideoFileClip object by referencing the video file through the parameter. Once this is done, extracting the audio is as simple as accessing the audio member of the VideoFileClip object that we created.


1 Answers

You can use ffprobe to return the audio information:

ffprobe videofilexample.mp4 would return something like:

ffprobe version 4.0 Copyright (c) 2007-2018 the FFmpeg developers
  built with gcc 8.1.0 (GCC)
  configuration: --prefix=/usr --disable-debug --disable-static --disable-stripping --enable-avresample --enable-fontconfig --enable-gmp --enable-gnutls --enable-gpl --enable-ladspa --enable-libass --enable-libbluray --enable-libdrm --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libiec61883 --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-nvenc --enable-omx --enable-shared --enable-version3
  libavutil      56. 14.100 / 56. 14.100
  libavcodec     58. 18.100 / 58. 18.100
  libavformat    58. 12.100 / 58. 12.100
  libavdevice    58.  3.100 / 58.  3.100
  libavfilter     7. 16.100 /  7. 16.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  1.100 /  5.  1.100
  libswresample   3.  1.100 /  3.  1.100
  libpostproc    55.  1.100 / 55.  1.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'tidalsunday3-2018-05-20_19.43.45':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.83.100
  Duration: 00:44:13.40, start: 0.000000, bitrate: 1035 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 3840x2160 [SAR 1:1 DAR 16:9], 782 kb/s, 24 fps, 24 tbr, 12288 tbn, 48 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 247 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
like image 178
Chris Stryczynski Avatar answered Oct 18 '22 21:10

Chris Stryczynski