9

我已经阅读了几篇关于这个问题的类似帖子,但似乎没有一个能直接帮助我。如果这实际上是重复的帖子,请引导我到包含解决方案的线程!

我正在保存一堆图像,然后使用 subprocess.call 对它们调用 ffmpeg。我为不同图像的集合做了几次。这基本上就是我正在做的事情:

from subprocess import call
for video in videos:
  call(['ffmpeg', ..., '-i', video, video+'.mp4')])

单独来看,这很好用。但是,当我在这些调用之前还完成了一些其他处理时(不在循环内,实际上只是在循环开始之前将值保存在内存中),它在制作了几个视频后因内存错误而崩溃(实际上是在制作最后一个视频时)一)。根据此评论, subprocess.call 分叉/克隆当前进程,这似乎意味着它请求的内存分配等于我当前在内存中的内存量,这对于我在调用 ffmpeg 时想要做的事情来说似乎有点过头了.

如何在不要求分配不必要的内存量的情况下从 python 中调用 ffmpeg?

4

2 回答 2

4

While it is true that subprocess.call does fork the process, and this child process does have its own memory space that is initially identical to the parent process (your python program), modern operating systems will use copy-on-write memory. The memory overhead of the forked process is initially relatively small, only requiring a few KB of memory in the kernel for process accounting. It's not until that child process starts making changes to its memory that extra memory is required.

After forking, the child process spawned by subprocess.call will call one of the exec system calls, which loads ffmpeg into memory and starts executing it.

Furthermore, fork is usually the only mechanism to create a new process on a POSIX system (see here), so I don't think there's an alternative to the fork-then-exec sequence that subprocess.call implements.

You may try running your program through strace or Valgrind to see exactly what system call is not getting the memory it requests. This may help you determine how to lower your memory requirements.

于 2013-10-02T18:18:13.820 回答
2

我今天遇到了同样的问题,我只是使用 os 解决了这个问题:

import os
for video in videos:
    job = ' ffmpeg ' + ... + ' -i ' + video + '.mp4'
    os.system( job )
于 2016-08-05T15:25:25.993 回答