Python extract wav from video file

烂漫一生 提交于 2019-11-28 05:27:35

It is a very easy Task using ffmpeg with python subprocess and there is a reason why people are pointing to this solution as a good solution.

This is the basic command extracting audio from a given video File:

ffmpeg -i test.mp4 -ab 160k -ac 2 -ar 44100 -vn audio.wav

The Python Code is just wrapping this command:

import subprocess

command = "ffmpeg -i C:/test.mp4 -ab 160k -ac 2 -ar 44100 -vn audio.wav"

subprocess.call(command, shell=True)

You have to make sure that ffmpeg is a known task, so in your system environment variables, under path, the path to ffmpeg.exe should be listed, or you can just use the full path to the exe in your python code.

Audio clips can be created from an audio file or from the soundtrack of a video file

from moviepy.editor import *
audioclip = AudioFileClip("some_audiofile.mp3")
audioclip = AudioFileClip("some_video.avi")

https://zulko.github.io/moviepy/getting_started/audioclips.html

this could be better and easier to use than ffmpeg, it's called python-video converter, and can be used to extract the audio from video, https://github.com/senko/python-video-converter , it could be used in conjunction with mpg123, as follows

    from converter import Converter
    import os
    c = Converter()
    clip = 'clip.avi'
    conv = c.convert(clip, 'audio.mp3', {'format':'mp3','audio':{'codec': 'mp3','bitrate':'22050','channels':1}})
    for timecode in conv:
        pass    
    os.system("mpg123 -w audio.wav audio.mp3")

the converter module extracts the audio from the video and saves it as an mp3 file, while mpg123 converts the mp3 file to mp4,

a different solution is as follows: using moviepy module in python https://github.com/Zulko/moviepy

    import moviepy.editor as mp
    clip = mp.VideoFileClip("video.avi").subclip(0,20)
    clip.audio.write_audiofile("theaudio.mp3")

the numbers within the subclip function specify start and end of audio, in seconds. you can then use mpg123 to change the audio to any other format

or example extract mp3 from

import os

VIDEOS_PATH = '/Users/****/videos'
VIDEOS_EXTENSION = '.webm'  # for example
AUDIO_EXT = 'wav'

EXTRACT_VIDEO_COMMAND = ('ffmpeg -i "{from_video_path}" '
                         '-f {audio_ext} -ab 192000 '
                         '-vn "{to_audio_path}"')

os.chdir(VIDEOS_PATH)
files = os.listdir(VIDEOS_PATH)
for f in files:
    if not f.endswith(VIDEOS_EXTENSION):
        continue

    audio_file_name = '{}.{}'.format(f, AUDIO_EXT)
    command = EXTRACT_VIDEO_COMMAND.format(
        from_video_path=f, audio_ext=AUDIO_EXT, to_audio_path=audio_file_name,
    )
    os.system(command)

FFmpeg is one of the most famous multimedia frameworks wich is widely used for processing videos. In order to encode the video, certainly a video encoder must be used. for more information use this: http://machinelearninguru.com/computer_vision/video_processing/ffmpeg_extract_audio/ffmpeg_audio_extract.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!