2
votes

Problem

I am trying to find a way to extract an audio wav file from a mp4 video file that is uploaded by a web user using ffmpeg using Django.

If I will find to extract audio, then where should I save it in my project?

  1. I tried it with "Django-ffmpeg", but didn't convert and was stuck in 'pending conversion' message.
  2. Then I tried with:

    import subprocess
    
    subprocess.call('ffmpeg -i filename.mp4 filename.wav')
    

Error

Error

Script

def validate_file_extension(value): import os from django.core.exceptions import ValidationError ext = os.path.splitext(value.name)1 # [0] returns path+filename filename = os.path.splitext(value.name)1 # [0] returns path+filename valid_extensions = ['.mp4'] if not ext.lower() in valid_extensions: raise ValidationError(u'Unsupported file extension.') else: import subprocess subprocess.call('ffmpeg -i filename.mp4 filename.wav')

1
use the full path to the executableszatmary
@szatmary please if you guide step by step how to use ffmpeg command for variable filename that is uploaded by web user in mp4 format and we want to extract wav audio file from that.maryam mehboob
@szatmary using "inFile = videofile outFile = videofile[:-3] + "wav" cmd = "ffmpeg -i {} {}".format(inFile, outFile) os.popen(cmd)" it give an error "'FieldFile' object is not subscriptable".Have you any idea?maryam mehboob
The error related to this line of code "outFile = videofile[:-3] + "wav" ".maryam mehboob
I just replace "outFile = videofile[:-3] + "wav" " with this "outFile = os.path.splitext(value.name)[0] + '.wav' " error is solved.maryam mehboob

1 Answers

0
votes

Solution: - you can extract wav audio file in this way in Django:

def extract_audio(videofile,channels=1, rate=16000):

    your_media_root = settings.MEDIA_ROOT
    path_to_user_folder = your_media_root + videofile.name

    inFile = path_to_user_folder

    temp = tempfile.NamedTemporaryFile(suffix='.wav', delete=False)
    command = ["ffmpeg", "-y", "-i", inFile,
           "-ac", str(channels), "-ar", str(rate),
           "-loglevel", "error", temp.name]
    subprocess.check_output(command)
    print(temp.name)
    return temp.name
  • secondly use "import tempfile" library to store extracted file temporary.