importsysimportosfrom concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor #线程池,进程池#import zlib

importthreading#import platform#from PIL import Image

importffmpegfrom shutil importcopyfiledefget_file_dir(file):"""获取文件目录通用函数"""fullpath=os.path.abspath(os.path.realpath(file))returnos.path.dirname(fullpath)defSaveVideo(input_file):

file_name=os.path.basename(input_file)

arr= file_name.split('.')

new_file_name= arr[0] + '_compress.' + arr[1]

output_path= os.path.join(get_file_dir(input_file), 'compress_output')

output_file=os.path.join(output_path, new_file_name)if notos.path.isdir(output_path):

os.makedirs(output_path)if(os.path.exists(output_file)):print("已存在,跳过压缩")return

#执行probe执行

probe =ffmpeg.probe(input_file)

video_stream= next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)if video_stream isNone:print('No video stream found', file=sys.stderr)return

#sys.exit(1)

#宽度

width = int(video_stream['width'])## 高度

height = int(video_stream['height'])## 帧数

#num_frames = int(video_stream['nb_frames'])

## 时长

#time = (video_stream['duration'])

#比特率

bitrate = (video_stream['bit_rate'])#print('width: {}'.format(width))

#print('height: {}'.format(height))

#print('num_frames: {}'.format(num_frames))

#print('time: {}'.format(time))

#print('bitrate: {}'.format(bitrate))

if ((width == 720 and height == 1280) or (width == 1280 and height == 720)):if (int(bitrate) < 2500 * 1024):print("比特率过小,跳过压缩")try:

copyfile(input_file, output_file)exceptIOError as e:print("Unable to copy file. %s" %e)return

elif ((width == 1080 and height == 1920) or (width == 1920 and height == 1080)):if (int(bitrate) < 5000 * 1024):print("比特率过小,跳过压缩")try:

copyfile(input_file, output_file)exceptIOError as e:print("Unable to copy file. %s" %e)return

else:if (int(bitrate) < 2000 * 1024):print("比特率过小,跳过压缩")try:

copyfile(input_file, output_file)exceptIOError as e:print("Unable to copy file. %s" %e)returnfpsize= os.path.getsize(input_file) / 1024

if fpsize >= 150.0: #大于150KB的视频需要压缩

compress = "ffmpeg -i {} -r 25 -pix_fmt yuv420p -vcodec libx264 -preset veryslow -crf 26 -profile:v baseline -acodec aac -b:a 96k -strict -2 {}".format(input_file, output_file)

isRun=os.system(compress)#if outName:

#compress = "ffmpeg -i {} -r 25 -pix_fmt yuv420p -vcodec libx264 -preset veryslow -crf 24 -profile:v baseline -acodec aac -b:a 128k -strict -2 {}".format(

#fileInputPath, fileOutPath)

#isRun = os.system(compress)

#else:

#compress = "ffmpeg -i {} -r 10 -pix_fmt yuv420p -vcodec libx264 -preset veryslow -profile:v baseline -crf 23 -acodec aac -b:a 32k -strict -5 {}".format(self.fileInputPath, self.fileInputPath)

#isRun = os.system(compress)

if isRun !=0:return (isRun, "没有安装ffmpeg")returnTrueelse:returnTruedefCompress_Video(self):#异步保存打开下面的代码,注释同步保存的代码

thr = threading.Thread(target=self.SaveVideo)

thr.start()defcheck_path(input_path):"""如果输入的是文件则直接压缩,如果是文件夹则先遍历"""

ifos.path.isfile(input_path):

SaveVideo(input_path)elifos.path.isdir(input_path):

dirlist=os.walk(input_path)for root, dirs, files indirlist:if (not (root.endswith("\\compress_output") or root.endswith("/compress_output"))):

i=0for filename infiles:

i= i + 1SaveVideo(os.path.join(root, filename))#process_pool.submit(SaveVideo, os.path.join(root, filename))

#compress_by_tinypng(os.path.join(root, filename))

else:print(u'目标文件(夹)不存在,请确认后重试。')if __name__ == "__main__":

process_pool= ProcessPoolExecutor(1) #定义5个进程

b = sys.argv[1:] #测试压缩

len_param =len(sys.argv)if len_param != 2:print('请使用: %s [inputpath] [outputpath]' %os.path.basename(sys.argv[0]))else:

check_path(b[0])

input("Press 请耐心等待\n")

python压缩视频文件_python 批量压缩手机视频相关推荐

  1. python读视频文件_python读取和保存视频文件

    如何用python实现视频关键帧提取并保存为图片?也许你会觉得对小编多做一点事你会觉得你很爽,可是在小编看来这是不屑的 import cv2vc = cv2.VideoCapture('Test.av ...

  2. python:电脑硬盘视频文件的批量压缩替换程序

    作为一名技术人员,电脑硬盘上存的许多视频都是教程.录像,而非电影. 这些长期保存的视频文件,如果能够有效压缩,则对我们帮助甚大. 小丸工具箱 我一般使用 小丸工具箱 进行压缩.这是目前公认的最好用的压 ...

  3. python 实现文件的批量压缩为.zip格式+.zip格式文件的解析

    python 实现文件的批量压缩为.zip格式+.zip格式文件的解析 python 实现文件的批量压缩为.zip格式 Python解析.zip文件的常见函数 python 实现文件的批量压缩为.zi ...

  4. 视频文件怎样进行压缩缩小

    为什么会写一篇关于视频文件时怎样压缩的呢,小编发现现在很多的平台上传视频都会限制视频文件的大小,超过限制大小我们就不可以上传我们想要上传的视频,那么为了不影响到视频文件的清晰度我们应该怎样压缩视频文件 ...

  5. 【python应用】 文件名称批量重命名工具

    [python应用] 文件名称批量重命名工具 1.背景 2.代码 3.打包 4.使用方法 5.资源 1.背景 参考[python应用] 视频捕获及保存工具 https://blog.csdn.net/ ...

  6. 快手今日头条火山秒拍陌陌美拍抖音小咖秀视频下载软件神器或手机 视频去重消重伪原创 批量处...

    今日头条趣东方头条凤凰新浪看点网易企鹅UC大鱼一点资讯自媒体快传视频处理软件 今日头条推广视频教程自媒体推广短视频教程今日头条引流小白入门 视频解析下载支持今日头条快手抖音火山映客陌陌西瓜美拍微博等 ...

  7. android手机拍摄视频格式,怎么用手机给自己拍摄的视频加上字幕?安卓手机视频编辑器给视频加字幕的方法...

    狸窝是帮助用户解决问题 提供教程解决方案 在这个过程中有使用我们自己开发的软件 也有网上找的工具 只要帮助用户解决问题就好!同意即往下继续了解下载 ... 我们知道,平时看电影或者是电视的时候,都会有 ...

  8. 赤兔android手机视频恢复软件,赤兔安卓手机视频恢复软件

    下面我们对赤兔安卓手机视频恢复软件v10.5官方版文件阐述相关使用资料和赤兔安卓手机视频恢复软件v10.5官方版文件的更新信息. 赤兔安卓手机视频恢复软件 赤兔安卓手机视频恢复软件是一款专门针对安卓系 ...

  9. 自媒体批量下载短视频软件,批量下载短视频有诀窍

    自媒体批量下载短视频软件,批量下载短视频有诀窍,现在随着短视频的爆火,越来越多的自媒体人和公司都注意到短视频这个平台流量有多大,短视频很容易变现也很容易引流,接下来我们来看看自媒体批量下载短视频软件有 ...

最新文章

  1. 谷歌发布地图「时光机」:100年前,你家街道长啥样?
  2. 图像处理之基础---特征向量的 几何意义
  3. 《scikit-learn》数据预处理与特征工程(二)数值转换
  4. c++ primer 函数传值1
  5. 复合页( Compound Page )
  6. html中语音聊天怎么实现,微信小程序语音聊天功能怎么实现?
  7. eNSP初学者配置以太网2种链路端口:access、trunk
  8. 计算机系统:系统级I/O
  9. 梳理企业业务流程四步法
  10. 什么是搜索引擎?搜索引擎有哪些?
  11. 微信开发者工具模拟扫描二维码调试
  12. 用VS实现图片的读取、显示与存储
  13. ubuntu ogv
  14. iOS双重验证设备无法接收验证码,更新手机号。
  15. css文件插入背景音乐,在HTML中添加背景音乐
  16. Mysql安装(转自韩顺平教育)
  17. 百度网盘文件下载工具
  18. 鸿蒙系统中的 WebView 加载H5页面出现net::ERR_CLEARTEXT_NOT_PERMITTED的解决方法
  19. QWidget,QMainWindow和QDialog的区别
  20. Generator函数的理解和使用

热门文章

  1. 离散小波变换 python_用python中的“haar”小波对图像进行离散小波变换
  2. 为什么女生读博(或直博)的比例越来越高?
  3. 计算机 联合培养博士后,联合培养博士继续读博后的有吗 - 公派出国 - 小木虫 - 学术 科研 互动社区...
  4. SOLO模型环境搭建训练流程及问题
  5. Xshell修改字体外观大小
  6. 人工智能让你更美 | 来自美图影像实验室发布会实录
  7. elasticsearch学习——spring整合
  8. 无代码为企业快速开发管理软件
  9. java 动态导出word文档 文档合并分页,浏览器下载,生成,动态表格,加图片
  10. 学好英语网html首页制作,网页 英语