Search code examples
pythonaudio

Extract wav from mp4


I have this code and I want it to produce .wav files from all the .mp4 I have in my folder

import os
import ffmpy

inputdir = '/Users/x/y/videos'
outputdir = '/Users/x/y/video_audio'
for filename in os.listdir(inputdir):
    actual_filename = filename[:-4]
if(filename.endswith(".mp4")):
    os.system('ffmpeg -i {} -acodec pcm_s16le -ar 16000 {}/{}.wav'.format(filename, outdir, actual_filename))
else:
    continue

But I get this error:

ffmpeg version 4.1.4 Copyright (c) 2000-2019 the FFmpeg developers
built with Apple LLVM version 10.0.1 (clang-1001.0.46.4)
configuration: --prefix=/usr/local/Cellar/ffmpeg/4.1.4_2 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/adoptopenjdk-12.0.1.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/adoptopenjdk-12.0.1.jdk/Contents/Home/include/darwin' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-videotoolbox --disable-libjack --disable-indev=jack --enable-libaom --enable-libsoxr
libavutil      56. 22.100 / 56. 22.100
libavcodec     58. 35.100 / 58. 35.100
libavformat    58. 20.100 / 58. 20.100
libavdevice    58.  5.100 / 58.  5.100
libavfilter     7. 40.101 /  7. 40.101
libavresample   4.  0.  0 /  4.  0.  0
libswscale      5.  3.100 /  5.  3.100
libswresample   3.  3.100 /  3.  3.100
libpostproc    55.  3.100 / 55.  3.100
Avengers_Endgame.mp4: No such file or directory

I'm not sure how to fix this because the folder and file exists.


Solution

  • when you are giving an input file to the ffmpeg, you should specify the path to the file in addition to the name you specified as you have done it while writing your output to a directory(outputdir). So your code should be like

    import os
    import ffmpy
    inputdir = '/Users/x/y/videos'
    outputdir = '/Users/x/y/video_audio'
    for filename in os.listdir(inputdir):
        actual_filename = filename[:-4]
        if(filename.endswith(".mp4")):
            os.system('ffmpeg -i {}/{} -acodec pcm_s16le -ar 16000 {}/{}.wav'.format(inputdir, filename, outputdir, actual_filename))
        else:
            continue