2009-10-23 2 views

답변

3

ffmpeg를 사용하여 편도 할 수 있습니다. ffmpeg는 h.264 및 h.263 코덱 지원과 함께 설치해야합니다. 다음은 파이썬 system(command)을 통해 호출 할 수있는 비디오 길이를 검색하는 명령입니다. 이 예는 지나치게 복잡하게 보일 수도 있지만 ffmpeg -i flv_file 2>&1 | grep "Duration" | cut -d ' ' -f 4 | sed s/,//

1


, 내가 더 나은에 운동으로 한 파이썬을 이해하고 쉽게 파일의 지속 시간의 원자 부품을 취급합니다.

#!/usr/bin/env python 

""" 
    duration 
    ========= 
    Display the duration of a video file. Utilizes ffmpeg to retrieve that information 

    Usage: 
    #duration file 
    bash-3.2$ ./dimensions.py src_video_file 

""" 

import subprocess 
import sys 
import re 

FFMPEG = '/usr/local/bin/ffmpeg' 

# ------------------------------------------------------- 
# Get the duration from our input string and return a 
# dictionary of hours, minutes, seconds 
# ------------------------------------------------------- 
def searchForDuration (ffmpeg_output): 

    pattern = re.compile(r'Duration: ([\w.-]+):([\w.-]+):([\w.-]+),') 
    match = pattern.search(ffmpeg_output) 

    if match: 
     hours = match.group(1) 
     minutes = match.group(2) 
     seconds = match.group(3) 
    else: 
     hours = minutes = seconds = 0 

    # return a dictionary containing our duration values 
    return {'hours':hours, 'minutes':minutes, 'seconds':seconds} 

# ----------------------------------------------------------- 
# Get the dimensions from the specified file using ffmpeg 
# ----------------------------------------------------------- 
def getFFMPEGInfo (src_file): 

    p = subprocess.Popen(['ffmpeg', '-i', src_file],stdout=subprocess.PIPE,stderr=subprocess.PIPE) 
    stdout, stderr = p.communicate() 
    return stderr 

# ----------------------------------------------------------- 
# Get the duration by pulling out the FFMPEG info and then 
# searching for the line that contains our duration values 
# ----------------------------------------------------------- 
def getVideoDuration (src_file): 

    ffmpeg_output = getFFMPEGInfo (src_file) 
    return searchForDuration (ffmpeg_output) 

if __name__ == "__main__": 
    try: 
     if 2==len(sys.argv): 
      file_name = sys.argv[1] 
      d = getVideoDuration (file_name) 
      print d['hours'] + ':' + d['minutes'] + ':' + d['seconds'] 

     else: 
      print 'Error: wrong number of arguments' 

    except Exception, e: 
     print e 
     raise SystemExit(1) 
관련 문제