脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python实现视频压缩功能

python实现视频压缩功能

2021-08-15 23:59山有木兮I Python

这篇文章主要介绍了python实现视频压缩功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

引言

实现一个视频压缩的功能,
性能优良 压缩视频 从61M 到 11M或者80M到15M
视频看起来没有太大损伤
缺点:耗时20s (win10,CPU:intel i7 8G运存 )

python实现视频压缩功能

由于压缩运算需要占用CPU资源,所以时间和性能其实很难兼顾,这个是我个人比较满意的一版 ,记录一下

话不多说,直接上代码

视频压缩:

  1. # 视频压缩第二版
  2. import sys
  3. import os
  4. import zlib
  5. import threading
  6. import platform
  7. from PIL import Image
  8.  
  9. class Compress_Pic_or_Video(object):
  10. def __init__(self,filePath,inputName,outName=""):
  11. self.filePath = filePath #文件地址
  12. self.inputName = inputName #输入的文件名字
  13. self.outName = outName #输出的文件名字
  14. self.system_ = platform.platform().split("-",1)[0]
  15. if self.system_ == "Windows":
  16. self.filePath = (self.filePath + "\\") if self.filePath.rsplit("\\",1)[-1] else self.filePath
  17. elif self.system_ == "Linux":
  18. self.filePath = (self.filePath + "/") if self.filePath.rsplit("/",1)[-1] else self.filePath
  19. self.fileInputPath = self.filePath + inputName
  20. self.fileOutPath = self.filePath + outName
  21.  
  22. @property
  23. def is_video(self):
  24. videoSuffixSet = {"WMV","ASF","ASX","RM","RMVB","MP4","3GP","MOV","M4V","AVI","DAT","MKV","FIV","VOB"}
  25. suffix = self.fileInputPath.rsplit(".",1)[-1].upper()
  26. if suffix in videoSuffixSet:
  27. return True
  28. else:
  29. return False
  30.  
  31. def SaveVideo(self):
  32. fpsize = os.path.getsize(self.fileInputPath) / 1024
  33. if fpsize >= 150.0: #大于150KB的视频需要压缩
  34. if self.outName:
  35. 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.fileOutPath)
  36. isRun = os.system(compress)
  37. else:
  38. 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)
  39. isRun = os.system(compress)
  40. if isRun != 0:
  41. return (isRun,"没有安装ffmpeg")
  42. return True
  43. else:
  44. return True
  45.  
  46. def Compress_Video(self):
  47. # 异步保存打开下面的代码,注释同步保存的代码
  48. thr = threading.Thread(target=self.SaveVideo)
  49. thr.start()
  50. #下面为同步代码
  51. # fpsize = os.path.getsize(self.fileInputPath) / 1024
  52. # if fpsize >= 150.0: # 大于150KB的视频需要压缩
  53. # 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(
  54. # self.fileInputPath, self.fileOutPath)
  55. # isRun = os.system(compress)
  56. # if isRun != 0:
  57. # return (isRun, "没有安装ffmpeg")
  58. # return True
  59. # else:
  60. # return True
  61.  
  62. if __name__ == "__main__":
  63. b = sys.argv[1:] #测试压缩
  64. savevideo = Compress_Pic_or_Video(b[0],b[1],b[2])
  65. print(savevideo.Compress_Video())
  66. # 这一版性能优良 压缩 从61M 到 11M 视频看起来没有太大损伤 缺点:inteli7 8G运存 耗时20s

启动方式:

在上述 .py文件所在目录下,shift+鼠标右键点击空白处,打开powershell窗口,运行以下命令:

  1. python shipinyasuo-2.py D:\yasuoship test.avi test1.avi

我的文件名叫 shipinyasuo-2.py ,把这个文件名替换成自己的,

D:\yasuoship 替换成要压缩的视频的文件夹的绝对路径

test.avi  压缩的视频的文件名

test1.avi  压缩后的文件名 , 和要压缩的文件在同一目录下

python实现视频压缩功能

后续我还会再走一个牺牲图像大小啊 质量啊换取速度和大小的版本

最后贴出参考:
https://blog.csdn.net/a849992683/article/details/90030326

延伸 · 阅读

精彩推荐