简体   繁体   English

ffmpeg 使用 python 子进程设置文件名模式

[英]ffmpeg set filename pattern using python subprocess

When running on command line, i use this code:在命令行上运行时,我使用以下代码:

ffmpeg -i video-frame-%06d.png -c:v libx264 -vf "fps=25,format=yuv420p" out.mp4

But im trying to run it inside python script,and Im getting no such file or directory error and I think because of the formatting type for the image filename numerals -%06d但是我试图在 python 脚本中运行它,我没有得到这样的文件或目录错误,我认为是因为图像文件名数字的格式类型-%06d

How can I concatenate or use the same file pattern I run in command line to use it in my python script如何连接或使用我在命令行中运行的相同文件模式以在我的 python 脚本中使用它

subprocess.Popen(['ffmpeg -i dir/video-frame-%06d.png -c:v libx264 -vf "fps=25,format=yuv420p" out.mp4', shell=True])

the sample image filename inside the folder are:文件夹内的示例图像文件名是:

  1. video-frame-00001.png视频帧-00001.png
  2. video-frame-00002.png视频帧-00002.png
  3. video-frame-00003.png视频帧-00003.png

I believe if you want to run subprocess you have 2 options:我相信如果您想运行子流程,您有两个选择:

  • one line command and use shell=True which is not recommended.一行命令并使用不推荐的 shell=True 。
  • or use a proper list.或使用适当的列表。

try this:尝试这个:

cmd = 'ffmpeg -i dir/video-frame-%06d.png -c:v libx264 -vf "fps=25,format=yuv420p" out.mp4'
subprocess.run(cmd.split())

You should use the full path instead of relative path.您应该使用完整路径而不是相对路径。 Eg.: based on path of your Python file:例如:基于你的 Python 文件的路径:

dir_of_imgs = os.path.join(os.path.realpath(os.path.dirname(__file__)), "dir")

And use this variable to define the path of image:并使用此变量来定义图像的路径:

img_path = os.path.join(dir_of_imgs, "video-frame-%06d.png")

It means your command should be something similar like that:这意味着你的命令应该是类似的:

subprocess.Popen(['ffmpeg -i {} -c:v libx264 -vf "fps=25,format=yuv420p" out.mp4'.format(img_path), shell=True])

NOTE:笔记:

Of course, probably your image files are in different folder but you can use the path of your Python file as a reference (as I show above).当然,您的图像文件可能位于不同的文件夹中,但您可以使用 Python 文件的路径作为参考(如我上面所示)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM