admin管理员组文章数量:1033331
Python 压缩 mov 格式视频示例代码
Python 压缩 mov 格式视频示例代码:
代码语言:javascript代码运行次数:0运行复制import sys
import os
import zlib
import threading
import platform
from PIL import Image
# 压缩视频的 Python 文件
# 主要依赖 ffmpeg 需要本机安装
class Compress_Video(object):
def __init__(self,filePath,inputName,outName=""):
#文件地址
self.filePath = filePath
#输入的文件名字
self.inputName = inputName
#输出的文件名字
self.outName = outName
self.system_ = platform.platform().split("-",1)[0]
if self.system_ == "Windows":
self.filePath = (self.filePath + "\\") if self.filePath.rsplit("\\",1)[-1] else self.filePath
elif self.system_ == "Linux":
self.filePath = (self.filePath + "/") if self.filePath.rsplit("/",1)[-1] else self.filePath
self.fileInputPath = self.filePath + inputName
self.fileOutPath = self.filePath + outName
@property
def is_video(self):
videoSuffixSet = {"WMV","ASF","ASX","RM","RMVB","MP4","3GP","MOV","M4V","AVI","DAT","MKV","FIV","VOB"}
suffix = self.fileInputPath.rsplit(".",1)[-1].upper()
if suffix in videoSuffixSet:
return True
else:
return False
def Save_Video(self):
fpsize = os.path.getsize(self.fileInputPath) / 1024
if fpsize >= 150.0: #大于150KB的视频需要压缩
if self.outName:
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)
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")
return True
else:
return True
def Compress_Video(self):
thr = threading.Thread(target=self.Save_Video)
thr.start()
if __name__ == "__main__":
b = sys.argv[1:]
savevideo = Compress_Video(b[0],b[1],b[2])
print(savevideo.Compress_Video())
代码主要依赖 ffmpeg 需要本机安装,在 macOS 上实际测试了下,一个 2.xG 左右的 mov 压缩后可以到 500MB 左右。
Python 压缩 mov 格式视频示例代码
Python 压缩 mov 格式视频示例代码:
代码语言:javascript代码运行次数:0运行复制import sys
import os
import zlib
import threading
import platform
from PIL import Image
# 压缩视频的 Python 文件
# 主要依赖 ffmpeg 需要本机安装
class Compress_Video(object):
def __init__(self,filePath,inputName,outName=""):
#文件地址
self.filePath = filePath
#输入的文件名字
self.inputName = inputName
#输出的文件名字
self.outName = outName
self.system_ = platform.platform().split("-",1)[0]
if self.system_ == "Windows":
self.filePath = (self.filePath + "\\") if self.filePath.rsplit("\\",1)[-1] else self.filePath
elif self.system_ == "Linux":
self.filePath = (self.filePath + "/") if self.filePath.rsplit("/",1)[-1] else self.filePath
self.fileInputPath = self.filePath + inputName
self.fileOutPath = self.filePath + outName
@property
def is_video(self):
videoSuffixSet = {"WMV","ASF","ASX","RM","RMVB","MP4","3GP","MOV","M4V","AVI","DAT","MKV","FIV","VOB"}
suffix = self.fileInputPath.rsplit(".",1)[-1].upper()
if suffix in videoSuffixSet:
return True
else:
return False
def Save_Video(self):
fpsize = os.path.getsize(self.fileInputPath) / 1024
if fpsize >= 150.0: #大于150KB的视频需要压缩
if self.outName:
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)
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")
return True
else:
return True
def Compress_Video(self):
thr = threading.Thread(target=self.Save_Video)
thr.start()
if __name__ == "__main__":
b = sys.argv[1:]
savevideo = Compress_Video(b[0],b[1],b[2])
print(savevideo.Compress_Video())
代码主要依赖 ffmpeg 需要本机安装,在 macOS 上实际测试了下,一个 2.xG 左右的 mov 压缩后可以到 500MB 左右。
本文标签: Python 压缩 mov 格式视频示例代码
版权声明:本文标题:Python 压缩 mov 格式视频示例代码 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/jiaocheng/1748036542a2244811.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论