Search code examples
pythonffmpegwavmp4pyffmpeg

conversing mp4 to wav with the same file name in python


Below are my code and I am trying to convert mp4 file to wav with some parameter change, like I am changing its frequency and so on. Now my problem is I have 100 mp4 files in the "inputdir" folder and I read the file one by 1 and convert it, then I want to save the file with the same name as mp4 file names but with different extension(in my case .wav). In my above code, every time I am converting, it's getting saved as out.wav. Can some one suggest me some options please?

I also checked How do I use FFMPEG to create a WAV file with the same name as the input (m4a) file?

but I don't find any answer.

Thanks :)

import os
import ffmpy

inputdir = '/home/------my folder path------------'


for filename in os.listdir(inputdir):
    actual_filename = filename[:-4]
    if(filename.endswith(".mp4")):
        os.system("ffmpeg -i {0} -acodec pcm_s16le -ar 16000 out.wav".format(filename))
    else:
        continue

I want to make it as,

os.system("ffmpeg -i {0} -acodec pcm_s16le -ar 16000 actual_filename.wav".format(filename))

I also tried like below.

os.system("ffmpeg -i "$filename" -acodec pcm_s16le -ar 16000 "${actual_filename}".wav".format(filename))

But it does not work. I am getting

os.system("ffmpeg -i "$filename" -acodec pcm_s16le -ar 16000 "${actual_filename}".wav".format(filename))
                          ^
SyntaxError: invalid syntax

Solution

  • You are closing your quotes in the os.system call.

    os.system('ffmpeg -i {} -acodec pcm_s16le -ar 16000 {}.wav'.format(filename, actual_filename))