python - 使用 ffmpeg 作为 Python 子进程的简单示例,以及转换上的 "checking in"

标签 python ffmpeg pyffmpeg

我希望将大量电影目录从一种格式转换为另一种格式,并检查转换的状态。我正在使用 Python 编程。

类似这样的事情:

>> m = MovieGenerator()
>> m.convertMovie('/path/to/movie/movie1.avi')
>> print m.status
>> 35 frames completed

这可能(或推荐)吗?有人有如何使用 ffmpeg 作为子进程的工作示例吗?

转换发生后,是否有任何方法可以“检查”转换的状态(例如,已完成多少帧?)

最佳答案

对于大型电影目录,我会使用 multiprocessing.Pool 来设置池工作人员。然后每个都使用子进程转换文件。我使用以下脚本批量进行AVI到MKV的转换:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Author: R.F. Smith <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="295b5a44405d4169515a1d484545074745" rel="noreferrer noopener nofollow">[email protected]</a>>
#
# To the extent possible under law, Roland Smith has waived all copyright and
# related or neighboring rights to avi2mkv.py. This work is published from the
# Netherlands. See http://creativecommons.org/publicdomain/zero/1.0/

"""Convert all AVI files found in the direcory trees named on the command line
   to Theora/Vorbis streams in a Matroska container."""

import base64
import os
import sys
import subprocess
from multiprocessing import Pool, Lock

globallock = Lock()

def tempname(ext):
    """Create a name for a temporary file in the /tmp firectory.

    Keyword arguments:
    ext -- the extension (without .) to give to the file.
    """
    return '/tmp/' + base64.b64encode(os.urandom(12), '__') + '.' + ext

def findavi(dirlist):
    """Find AVI files and returns their names in a list.

    Keyword arguments:
    dirlist -- a list of directories to seach in
    """
    result = []
    for dirname in dirlist:
        for root, dirs, files in os.walk(dirname):
            for name in files:
                if name.endswith('.avi'):
                    result.append(root + '/' + name)
    return result

def output(txt):
    """Print while holding a global lock."""
    globallock.acquire()
    print txt
    globallock.release()        

def process(fname):
    """Use ffmpeg2theora and mkvmerge to convert an AVI file to Theora/Vorbis
    streams in a Matroska container.

    Keyword arguments:
    fname -- name of the file to convert
    """
    ogv = tempname('ogv')
    args = ['ffmpeg2theora', '--no-oshash', '-o', ogv, '-v', '7', fname]
    args2 = ['mkvmerge', '-o', fname[:-3] + 'mkv', ogv]
    bitbucket = open('/dev/null')
    try:
        output("Converting {} to {}.".format(fname, ogv))
        subprocess.check_call(args, stdout=bitbucket, stderr=bitbucket)
        output("Starting merge for {}.".format(ogv))
        subprocess.check_call(args2, stdout=bitbucket, stderr=bitbucket)
        os.remove(ogv)
        output("Conversion of {} completed.".format(fname))
    except:
        output("ERROR: Converting {} failed.".format(fname))
    bitbucket.close()

def main(argv):
    """Main program.

    Keyword arguments:
    argv -- command line arguments
    """
    if len(argv) == 1:
        path, binary = os.path.split(argv[0])
        print "Usage: {} [directory ...]".format(binary)
        sys.exit(0)
    avis = findavi(argv[1:])
    p = Pool()
    p.map(process, avis)
    p.close()

if __name__ == '__main__':
    main(sys.argv)

关于python - 使用 ffmpeg 作为 Python 子进程的简单示例,以及转换上的 "checking in",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12204678/

相关文章:

python - 如何将 Yapsy 包含在 py2exe 中?

c++ - OpenCV编码为H264

macos - ffmpeg 没有在 OSX 上转换 mp3。安装了自制软件。找不到输出流 #0 :0 的编码器(编解码器 mp3)

python - 从 json 使用 unixtime 从视频创建多个子剪辑?

ffmpeg 从具有多个 channel 的输入中输出单独的 channel

python - 如何在 Django 和 django-jsonfield 中将 JSONField 的默认值设置为空列表?

python - 无法消除 django-cms 产生的警告

ios - FFMpeg hls_time 精度

python - 从python中的视频中提取音频

python - 在requirements.txt文件中使用 "-t"选项