如何在python函数中使用ffmpeg [英] How to use ffmpeg in a python function

查看:145
本文介绍了如何在python函数中使用ffmpeg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用ffmpeg从视频文件中提取音频,这是我的代码

I have tried to use a ffmpeg to extract an audio from a video file and this is my code

import io
import os
import subprocess

def extract_audio(video,output):
    command = "ffmpeg -i '{video}' -ac 1  -f flac -vn '{output}'"
    subprocess.call(command,shell=True)

extract_audio('dm.MOV','dm-new.flac')

编译后我没有任何错误.通过这样做,我应该获得一个新文件'dm-new.flac'.但是在编译脚本后没有创建任何flac文件.我认为语法有问题或变量'command'中有问题,我不知道要解决此问题.我的问题在这里是如何基于此代码在python函数中使用ffmpeg?

And I got no error after compiled. By doing this I should get a new file which is 'dm-new.flac'. But there is no such a flac file created after I compile the script. I think there are something wrong with the syntax or something in the variable 'command' which I have no idea to fix this. My question here is how can I use ffmpeg in a python function base on this code?

顺便说一句,我知道我可以不用编写函数就可以使用ffmpeg.但是我真的需要写一个函数.谢谢

By the way, I knew that I could just use ffmpeg without writing a function. But I really need to write in in a function. Thank you

推荐答案

尝试一下:

import io
import os
import subprocess

def extract_audio(video,output):
    command = "ffmpeg -i {video} -ac 1  -f flac -vn {output}".format(video=video, output=output)
    subprocess.call(command,shell=True)

extract_audio('dm.MOV','dm-new.flac')

我认为您正在尝试在字符串中引用两个变量,但没有告诉Python您应将视频"和输出"替换为其对应的变量. .format()允许您引用字符串中引用的变量.

I think you were trying to reference two variables inside a string but did not tell Python that you should replace 'video' and 'output' with their corresponding variables. .format() allows you to reference the variables that you refer to in a string.

有关更多信息,请参见此处.

See here for more info.

这篇关于如何在python函数中使用ffmpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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