用Python和FFmpeg查找大码率的视频文件
2014-05-12 22:30
519 查看
用Python和FFmpeg查找大码率的视频文件
本文使用Python2.7, 这个工作分两步遍历文件夹下的视频文件
用ffprobe获取是视频文件的码率信息
用ffprobe 获取json格式的视频信息
用ffprobe.exe是FFmpeg自带的查看视频信息的工具,其获取json格式的信息命令如下ffprobe -v quiet -print_format json -show_format -show_streams -i filename
这个命令会输出 带有 streams和format项的json结构
Python读取json
用os.popen(strCmd).read() 来获取命令行的输出用json.loads 解析json, 这个必须加try,否则某些乱码会导致挂机
import os,re,json # ffprobe 需放置在 system32, not user's PATH # 调用ffprobo 获取信息的json格式 def getJsonString(strFileName): strCmd = 'ffprobe -v quiet -print_format json -show_format -show_streams -i "' + strFileName + '"' mystring = os.popen(strCmd).read() return mystring # UnicodeDecodeError: 'utf8' codec can't decode byte 0xc0 in position 57: invalid start byte filecontent = getJsonString(strFileName) try: js = json.loads(filecontent) except Exception,e: print Exception,":",e, strFileName return
获取视频信息
有时候video项中没有bit_rate这一项,这时需要从format项中取iVideoWidth = 0 iVideoHeight = 0 iVideoBitRate = 0 iAllBitRate = 0 strCodecName = '' for stream in arrStreams: if(stream['codec_type'] == 'video'): strCodecName = stream['codec_name'] iVideoWidth = int(stream['width']) iVideoHeight = int(stream['height']) # h264 可能没有这一项 if 'bit_rate' in stream.keys() : iVideoBitRate = int (stream['bit_rate']) break iAllBitRate = int(js['format']['bit_rate']) print 'CodecName (%s), width(%d), height(%d), video bit_rate(%d), all bit_rate (%d)' % (strCodecName, iVideoWidth, iVideoHeight, iVideoBitRate, iAllBitRate )
获取文件夹里的所有文件名称
这个网上比较多,取了一个实现简单的递归版本g_fileList = [] def getFiles(path): if os.path.exists(path): files = os.listdir(path) for f in files : subpath=os.path.join(path,f) if os.path.isfile(subpath): g_fileList.append(subpath) else: getFiles(subpath)
过滤视频文件
# 按扩展名过滤 def filterExname (fileList, arrExtnames): filterList = [] for strFile in fileList: strLowFileName = strFile.lower() # 转小写先 for strExtName in arrExtnames : if strLowFileName.endswith(strExtName) : filterList.append(strFile) return filterList g_fileList = [] # 如果是网络路径,可以先映射到本地, python有可能不支持网络路径 \\ getFiles('.') print 'g_fileList len = ', len(g_fileList) arrExtName = ['.mkv', '.rmvb', '.rm', '.wmv', '.avi', '.mp4', '.mov', '.mpg', '.xvid', '.asf', '.mpeg', '.vob', '.3gp', '.flv', '.ts'] arrVideoFiles = filterExname (g_fileList, arrExtName)
过滤大的码率文件
# 设置单位像素 比特率 阈值 2.5 - 4.0 PIEXL_RATE_MAX = 3.9 def isLargeBps(iWidth, iHeight, iBitrate): # 基准 每像素字节数 fCurrentBitRatePixel = float(iBitrate) / (iWidth * iHeight) print 'isNeedConvert input = ', iWidth, iHeight, iBitrate, fCurrentBitRatePixel return (fCurrentBitRatePixel > PIEXL_RATE_MAX)
总结
大致就是这样,至于输出batch命令行,输出csv结果就不必细讲了。相关文章推荐
- 用Python和FFmpeg查找大码率的视频文件
- python整合ffmpeg实现视频文件的批量转换
- python opencv 读取本地视频文件 修改 ffmpeg
- python模块学习---glob 文件路径查找
- Android本地视频播放器开发--ffmpeg解码视频文件中的音频(2)
- Ffmpeg进行视频文件转换
- 音视频文件码率与大小计算
- python小程序:查找nginx日志文件的ip点击率并排序
- ffmpeg对视频文件进行解码的大致流程
- javaCV开发详解之3:收流器实现,录制流媒体服务器的rtsp/rtmp视频文件(基于javaCV-FFMPEG)
- 使用ffmpeg合并视频文件的三种方法
- Python:在指定目录下查找满足条件的文件
- Python实现查找指定目录下包括某关键字的所有文件脚本
- python查找并删除相同文件-UNIQ File-wxPython-v6
- 使用ffmpeg处理 视频文件中帧间时间戳异常、关键帧间隔异常
- ffmpeg实现转码一个普通视频文件为视频mpeg4,音频mp3的
- [原]通过配合ffmpeg.exe获取视频文件时长
- Python+opencv 无法正确读取视频文件(已解决)
- python opencv 读取视频文件并保存
- python程序2(递归查找某一个文件夹下所有的文件是否含有某个特定的字符串,并打印该文件名)