用Python和FFmpeg查找大码率的视频文件
用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查找大码率的视频文件的更多相关文章
- FFmpeg开发实战(六):使用 FFmpeg 将YUV数据编码为视频文件
本文中实现的一个小功能是把一个YUV原始视频数据(时间序列图像)经过h264编码为视频码流,然后在使用mp4封装格式封装. 编码&封装的流程图如下: 使用ffmpeg编码流程: 1.首先使用a ...
- 用find命令查找最近修改过的文件
Linux的终端上,没有windows的搜索那样好用的图形界面工具,但find命令确是很强大的. 比如按名字查找一个文件,可以用 find / -name targetfilename . 唉,如果只 ...
- Linux系统下查找最近修改过的文件
Linux的终端上,没有windows的搜索那样好用的图形界面工具,但find命令确是很强大的. 比如按名字查找一个文件,可以用 find / -name targetfilename . 唉,如果只 ...
- 算法图解学习笔记01:二分查找&大O表示法
二分查找 二分查找又称折半查找,其输入的必须是有序的元素列表.二分查找的基本思想是将n个元素分成大致相等的两部分,取a[n/2]与x做比较,如果x=a[n/2],则找到x,算法中止:如果x<a[ ...
- 【原创】python倒排索引之查找包含某主题或单词的文件
什么是倒排索引? 倒排索引(英语:Inverted index),也常被称为反向索引.置入档案或反向档案,是一种索引方法,被用来存储在全文搜索下某个单词在一个文档或者一组文档中的存储位置的映射.它是文 ...
- Linux如何查找大文件或目录总结
在Windows系统中,我们可以使用TreeSize工具查找一些大文件或文件夹,非常的方便高效,在Linux系统中,如何去搜索一些比较大的文件呢?下面我整理了一下在Linux系统中如何查找大文件或文件 ...
- 在linux/unix中查找大文件
在linux/unix中查找大文件,如查找大于100M文件的位置路径,查找等于10M文件的位置路径等等,下面就介绍几个实现快速查找的命令: 1. 查找指定目录下所有大于100M的文件,命令为 find ...
- CentOS下如何查找大文件
在Windows系统中,我们可以使用TreeSize工具查找一些大文件或文件夹,非常的方便高效,在Linux系统中,如何去搜索一些比较大的文件呢?下面我整理了一下在Linux系统中如何查找大文件或文件 ...
- centos磁盘爆满,查找大文件并清理
今天发现vps敲入crontab -e 居然提示 “Disk quota exceeded” 无法编辑.于是"df -h"查了查发现系统磁盘空间使用100%了.最后定位到是/var ...
随机推荐
- solr的原子更新/局部更新
solr支持三种类型的原子更新: set - to set a field. add - to add to a multi-valued field. inc - to increment a fi ...
- 《ArcGIS Engine+C#实例开发教程》第七讲 图层符号选择器的实现2
原文:<ArcGIS Engine+C#实例开发教程>第七讲 图层符号选择器的实现2 摘要:在第七讲 图层符号选择器的实现的第一阶段中,我们完成了符号选择器窗体的创建与调用.在第二阶段中, ...
- 139. Word Break
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
- A9两款芯片管脚数目
Exynos 4412 bga786; S5P4418 bga513
- 【HDOJ】1222 Wolf and Rabbit
最大公约数,辗转相除. #include <stdio.h> long long gcd(long long a, long long b) { if (a<b) return gc ...
- WCF - IIS Hosting
WCF - IIS Hosting Hosting a WCF service in IIS (Internet Information Services) is a step-by-step pro ...
- MapReduce的数据流程、执行流程
MapReduce的数据流程: 预先加载本地的输入文件 经过MAP处理产生中间结果 经过shuffle程序将相同key的中间结果分发到同一节点上处理 Recude处理产生结果输出 将结果输出保存在hd ...
- BZOJ_1626_[Usaco2007_Dec]_Building_Roads_修建道路_(Kruskal)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1626 给出\(n\)个点的坐标,其中一些点已经连通,现在要把所有点连通,求修路的最小长度. 分 ...
- Hibernate: org.hibernate.exception.SQLGrammarException: could not insert: 错误
最近在学习Java Web,今天刚接触Hibernate.学习的书籍是<轻量级Java EE企业应用实战(第3版)>.书中367页5.2.2中给予的Hibernate例子中的代码运行有以下 ...
- HTTP缓存是如何实现
浏览器是如何知道使用缓存的,其实这都是通过http中,浏览器将最后修改时间发送请求给web服务器,web服务器收到请求后跟服务器上的文档最后修改的时间对比,如果web服务器上最新文档修改时间小于或者等 ...