Python3.6_x86通过FFpmeg获取视频或音频的时长和分辨率

前言

  前段时间公司在做流媒体服务,与许多厂家合作拿了许多视频过来,现在要对这些视频文件进行整理,通过特殊的编码排列,获取他们的时长以及分辨率,这里我遇到一个大坑,请往下面看。

 1 # -*- coding: utf-8 -*-
 2 import os
 3 import sys
 4 import shutil
 5 import subprocess
 6 import json
 7 def getLength(filename):
 8     command = ["ffprobe.exe","-loglevel","quiet","-print_format","json","-show_format","-show_streams","-i",filename]
 9     result = subprocess.Popen(command,shell=True,stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
10     out = result.stdout.read()
11     #print(str(out))
12     temp = str(out.decode('utf-8'))
13     try:
14         data = json.loads(temp)['streams'][1]['width']
15     except:
16         data = json.loads(temp)['streams'][0]['width']
17     return data
18 
19 def getLenTime(filename):
20     command = ["ffprobe.exe","-loglevel","quiet","-print_format","json","-show_format","-show_streams","-i",filename]
21     result = subprocess.Popen(command,shell=True,stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
22     out = result.stdout.read()
23     #print(str(out))
24     temp = str(out.decode('utf-8'))
25     data = json.loads(temp)["format"]['duration']
26     return data

只需要传入文件的完整路径就可以获取到分辨率以及播放时长。

本次使用ffpmeg发现,使用的python3.6 ,32位和64位通过 subprocess.Popen 获取到的值是不一样的,导致后面的方法也稍微不一样,如果你是32位的,使用我上面代码无法获取

out = result.stdout
data = json.loads(out)["format"]['duration']
 
作者感言:重要是事情说三遍 路径全英文,路径全英文,路径全英文!!!!!
posted @ 2017-06-07 10:26  清风徐来#  阅读(6839)  评论(1编辑  收藏  举报