2012-08-30 3 views
2

영화의 큰 디렉토리를 한 형식에서 다른 형식으로 변환하고 변환 상태를 확인하려고합니다. 나는 파이썬으로 프로그래밍하고있다. 이 같은파이썬 하위 프로세스로 ffmpeg를 사용하고 변환시 "체크인"하는 간단한 예제

뭔가 :

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

이 가능 (또는 추천)인가? 누구든지 ffmpeg를 하위 프로세스로 사용하는 방법에 대한 실제 예제가 있습니까? 변환이 발생되면

, (예를 들어, 얼마나 많은 프레임이 완료된?) 변환의 상태에 "체크인"할

+0

특히 파이썬 셸에서이 작업을 수행하고 싶습니까? –

+0

대형 영화 라이브러리에서 가장 효율적인 것은 무엇이든 – ensnare

답변

4

을 영화의 큰 디렉토리를 들어 어떤 방법이, 나 ' d 풀 사용을 설정하려면 multiprocessing.Pool을 사용하십시오. 그런 다음 각각은 subprocess을 사용하여 파일을 변환합니다. 다음 스크립트를 사용하여 AVI를 일괄 적으로 MKV로 변환합니다.

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
# 
# Author: R.F. Smith <[email protected]> 
# 
# 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)