1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| import sys import os import zlib import threading import platform from PIL import Image
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: 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())
|