简体   繁体   English

ffmpeg无法检测文件,而os可以

[英]ffmpeg cannot detect file while os can

Issue 问题

I'm trying to load images in a sub-directory with files named: 我正在尝试将图像加载到名为以下文件的子目录中:

  • .\\images\\image0.png \\影像\\ image0.png
  • .\\images\\image1.png \\影像\\ image1.png
  • .\\images\\image2.png \\影像\\ image2.png
  • .\\images\\image3.png \\影像\\ image3.png
  • ... etc ... ...等等...
  • .\\images\\image8.png \\影像\\ image8.png
  • .\\images\\image9.png \\影像\\ image9.png

When I run code: 当我运行代码时:

 print os.path.exists('.\\images\\image0.png')
 cmd = "ffmpeg -f image2 -r 20 -i .\\images\\image%01d.png -c:v libx264 -r 20 .\\images\\output.mp4"
 os.system(cmd)

it results in: 结果是:

 True
 //blah blah blah ffmpeg start up stuff blah blah blah 

 [image2 @ 00000000026ceda0] Could find no file with with path '.\images\image%01
 d.png' and index in the range 0-4
 .\images\image%01d.png: No such file or directory

I even checked: 我什至检查:

 os.getcwd()

Was the right path. 是正确的道路。

Could someone shed some light on my issue? 有人可以阐明我的问题吗? I'm pretty sure my ffmpeg command is correct (it has worked for me in the past). 我很确定我的ffmpeg命令是正确的(过去对我有用)。 I'm wondering if there might be a reason that ffmpeg won't recognize my current working directory? 我想知道ffmpeg是否可能无法识别我当前的工作目录? (And won't take that relative path as a result?) (并且不会采用相对路径吗?)

Thanks! 谢谢! Advice/suggestions will be greatly appreciated. 建议/建议将不胜感激。

The problem is that you've set up your system so every new cmd.exe shell immediately cd's to your Documents directory, so ffmpeg is trying to run there. 问题在于您已经设置了系统,因此每个新的cmd.exe Shell都会立即将cd ffmpeg您的Documents目录,因此ffmpeg试图在此运行。

The obvious way around that is to just give ffmpeg absolute paths instead of relative: 解决这个问题的明显方法是只给ffmpeg绝对路径,而不是相对路径:

images = os.path.abspath('.\\images')
cmd = "ffmpeg -f image2 -r 20 -i {}\\image%01d.png -c:v libx264 -r 20 {}\\output.mp4".format(images, images)

Alternatively, you can always stick a cd command into what you send to the system function: 或者,您始终可以在发送给system功能的内容中粘贴cd命令:

curpath = os.path.abspath(os.getcwd())
cmd = "cd {} && ffmpeg -f image2 -r 20 -i .\\images\\image%01d.png -c:v libx264 -r 20 .\\images\\output.mp4".format(curpath)

However, the best solution is to stop using system entirely, as the documentation for system suggests, and leave the shell out of it: 但是,最好的解决方案是完全停止使用system ,如system的文档所建议的那样,将shell排除在外:

cmd = "ffmpeg -f image2 -r 20 -i .\\images\\image%01d.png -c:v libx264 -r 20 .\\images\\output.mp4"
subprocess.check_call(cmd)

(Someone is invariably going to suggest in a comment that you can't use a string with subprocess unless shell=True . That's true on Unix, but not on Windows. In fact, on Windows, if you pass a list of parameters, subprocess will just join them up into a string to pass to CreateProcess . If you don't know how to escape your parameters properly for Windows, a list can still be a good idea—but if you've got a perfectly good command line, just use it.) (有人总是会在注释中建议除非shell=True否则不能在subprocess使用字符串。这在Unix上是正确的,但在Windows上不是。实际上,在Windows中,如果您传递参数列表,则subprocess只是将它们连接成一个字符串传递给CreateProcess 。如果您不知道如何在Windows中正确地转义参数,则列表仍然是一个好主意—但是,如果您有一个非常好的命令行,用它。)

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

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