Python从视频文件中提取wav

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26741116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:57:25  来源:igfitidea点击:

Python extract wav from video file

pythonaudiovideoffmpeggstreamer

提问by xolodec

Related:

有关的:

How to extract audio from a video file using python?

如何使用python从视频文件中提取音频?

Extract audio from video as wav

从视频中提取音频为 wav

How to rip the audio from a video?

如何从视频中翻录音频?

My question is how could I extract wav audio track from video file, say video.avi? I read many articles and everywhere people suggest to use (from Python) ffmpegas a subprocess (because there are no reliable python bindings to ffmpeg - the only hope was PyFFmpegbut i found it is unmaintaned now). I don't know if it is right solution and i am looking for good one.
I looked to gstreamer and found it nice but unable to satisfy my needs -- the only way I found to accomplish this from command line looks like

我的问题是如何从视频文件中提取 wav 音轨,比如说video.avi?我读了很多文章,到处都有人建议使用(来自 Python)ffmpeg作为子进程(因为没有可靠的 python 绑定到 ffmpeg - 唯一的希望是,PyFFmpeg但我发现它现在没有维护)。我不知道这是否是正确的解决方案,我正在寻找好的解决方案。
我查看了 gstreamer,发现它很好,但无法满足我的需求——我发现从命令行完成此任务的唯一方法看起来像

 gst-launch-0.10 playbin2 uri=file://`pwd`/ex.mp4  audio-sink='identity single-segment=true ! audioconvert ! audio/x-raw-int, endianness=(int)1234, signed=(boolean)true, width=(int)16, depth=(int)16, rate=(int)16000, channels=(int)1 ! wavenc !  filesink location=foo.wav' 

But it is not efficient because i need to wait ages while playing video and simultaneously writing to wav file.

但这效率不高,因为我需要在播放视频并同时写入 wav 文件时等待很长时间。

ffmpegis much better:

ffmpeg好多了:

avconv  -i foo.mp4  -ab 160k -ac 1 -ar 16000 -vn ffaudio.wav

But i am unable to launch it from python (not as a command line subprocess). Could you please point me out pros and cons of launching ffmpeg from python as a command line utility ? (I mean using python multiprocessingmodule or something similar).

但是我无法从 python 启动它(不是作为命令行子进程)。您能否指出从 python 启动 ffmpeg 作为命令行实用程序的利弊?(我的意思是使用 pythonmultiprocessing模块或类似的东西)。

And second question.

还有第二个问题。

What is simple way to cut long wav file into pieces so that i don't break any words ? i mean pieces of 10-20 sec length with start and end during the pause in sentences/words ?

有什么简单的方法可以将长 wav 文件切成碎片,这样我就不会打断任何单词?我的意思是在句子/单词的暂停期间开始和结束 10-20 秒的长度?

i know how to break them on arbitrary pieces:

我知道如何在任意片段上打破它们:

import wave


win= wave.open('ffaudio.wav', 'rb')
wout= wave.open('ffsegment.wav', 'wb')

t0, t1= 2418, 2421 # cut audio between 2413, 2422 seconds
s0, s1= int(t0*win.getframerate()), int(t1*win.getframerate())
win.readframes(s0) # discard
frames= win.readframes(s1-s0)

wout.setparams(win.getparams())
wout.writeframes(frames)

win.close()
wout.close()

回答by user1767754

It is a very easy Task using ffmpegwith pythonsubprocess and there is a reason why people are pointing to this solution as a good solution.

ffmpegpython进程一起使用是一项非常简单的任务,并且人们指出此解决方案是一个很好的解决方案是有原因的。

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

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

The Python Code is just wrapping this command:

Python 代码只是包装了这个命令:

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.

你必须确保 ffmpeg 是一个已知任务,所以在你的系统环境变量中,在路径下,应该列出 ffmpeg.exe 的路径,或者你可以在你的 python 代码中使用 exe 的完整路径。

回答by programmer44

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

这可能比 ffmpeg 更好更容易使用,它被称为 python-video 转换器,可用于从视频中提取音频,https://github.com/senko/python-video-converter,它可以用于结合mpg123,如下

    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,

转换器模块从视频中提取音频并将其保存为 mp3 文件,而 mpg123 将 mp3 文件转换为 mp4,

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

一个不同的解决方案如下:在 python https://github.com/Zulko/moviepy 中使用 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

subclip 函数中的数字指定音频的开始和结束,以秒为单位。然后您可以使用 mpg123 将音频更改为任何其他格式

回答by Kouassi Konan Jean-Claude

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

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

回答by user2254105

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

FFmpeg 是最著名的多媒体框架之一,广泛用于处理视频。为了对视频进行编码,当然必须使用视频编码器。有关更多信息,请使用:http: //machinelearninguru.com/computer_vision/video_processing/ffmpeg_extract_audio/ffmpeg_audio_extract.html

回答by Nick Korolkov

or example extract mp3 from

或例如从中提取mp3

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)