如何使用python从视频文件中提取音频? [英] How to extract audio from a video file using python?

查看:749
本文介绍了如何使用python从视频文件中提取音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个可以从视频文件中提取音频的python程序(例如video.avi). 有什么好的图书馆吗?我应该从哪里开始呢? 我尝试使用PyMedia,但无法将其安装在MacOSX(Mountain Lion)上.

I want to write a python program that could extract audio from a video file (e.g. video.avi). Is there any good library for it? And where should I start from? I tried to use PyMedia, but I couldn't install it on my MacOSX(Mountain Lion).

问题是video.avi不能完全可用.有人在写它,并每秒添加一些帧.因此,我想用python编写代码以获取视频,并从中提取音频并将其写入文件(例如audio.mp3audio.wav). 我不知道ffmpeg是否可以等待视频复制到video.avi.

The problem is video.avi is not completely available. Someone is writing on it and adding some frames to it every second. So I wanted to write a code in python to get the video as it comes and extract the audio from it and write it to a file (e.g. audio.mp3, audio.wav). I don't know if ffmpeg can wait for the video to be copied to video.avi.

我不能等待视频被完全复制然后再进行音频提取.我必须做到这一点.

And I cannot wait for the video to be copied completely and then do the audio extraction. I have to do it as it comes.

推荐答案

我没有完整的解决方案,但是来自 ffmpeg文档,看起来ffmpeg可以通过管道传输到stdin来读取不完整的文件.文档中的示例:

I don't have a complete solution, but from the ffmpeg docs, it looks like ffmpeg can read an incomplete file by piping to stdin. Example from the docs:

cat test.wav | ffmpeg -i pipe:0

如果必须使用python,则始终可以使用子进程调用ffmpeg .

If you must use python, you can always call ffmpeg using subprocess.

这篇关于如何使用python从视频文件中提取音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆