zoukankan      html  css  js  c++  java
  • Python视频压缩

    Python视频压缩

    应视频石大的要求,写了一段python代码,来对视频进行压缩,顺便写了一下图片压缩,但发现对.png格式的图片压缩效果不太好,其他的格式都没什么大问题。

    先已经支持的格式:

    • 视频:.wmv,.asf,.asx,.rm,.rmvb,.mp4,.3gp,.mov,.m4v,.avi,.dat,.mkv,.fiv,.vob
    • 图片:.bmp,.gif,.jpeg,.jpg,.TIFF,.svg,.pcx,.wmf,.emf,.lic,.eps,.tga

    所需要的环境

    python3.7 ->python 环境配置 Windows&Mac

    ffmpeg -> ffmpeg安装 Windows&Mac


    使用方法

    1. 新建一个moriarty.py文件,将文章末尾的代码复制到文件中。
    2. 在python文件的文件夹中进入终端(或cmd)输入指令
    python moriarty.py -address -input. -output.
    

    其中-address 是所需要压缩的视频或文件的所在文件夹的绝对地址

    -input.是所需要压缩的视频或图片的名称及格式名。一定要加格式名

    -output. 是压缩后的视频或文件的名称和格式名(名称可以自己取)。

    1. 压缩后的文件会出现在同一文件夹中。

    代码

    #视频压缩
    import sys
    import os
    import zlib
    import threading
    import platform
    from PIL import Image
    
    class Compress_Pic_or_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 SaveVideo(self):
            fpsize = os.path.getsize(self.fileInputPath) / 1024
            if fpsize >= 150.0:
                if self.outName:
                    compress = "ffmpeg -i {} -vcodec libx265 -crf 20 {}".format(self.fileInputPath,self.fileOutPath)
                    isRun = os.system(compress)
                else:
                    compress = "ffmpeg -i {} -vcodec libx265 -crf 20 {}".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.SaveVideo)
            thr.start()
    
    if __name__ == "__main__":
        tag = sys.argv[1:]
        savevid = Compress_Pic_or_Video(tag[0],tag[1],tag[2])
        print(savevid.Compress_Video())
    
    
    #压缩图片
    import sys
    import os
    import zlib
    import threading
    import platform
    from PIL import Image
    
    class Compress_Pic_or_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_picture(self):
            picSuffixSet = {"BMP","GIF","JPEG","TIFF","PNG","SVG","PCX","WMF","EMF","LIC","EPS","TGA","JPG"}
            suffix = self.fileInputPath.rsplit(".",1)[-1].upper()
            if suffix in picSuffixSet:
                return True
            else:
                return False
    
        def SavePic(self):
            fpsize = os.path.getsize(self.fileInputPath) / 1024
            if fpsize >= 50.0:
                im = Image.open(self.fileInputPath)
                imBytes = im.tobytes()
                imBytes = zlib.compress(imBytes, 5)
                im2 = Image.frombytes('RGB', im.size, zlib.decompress(imBytes))
                if self.outName:
                    im2.save(self.fileOutPath)
                    return (self.fileOutPath,os.path.getsize(self.fileOutPath))
                else:
                    im2.save(self.fileInputPath)
                    return (self.fileInputPath,os.path.getsize(self.fileInputPath))
            else:
                return True
    
        def Compress_Picture(self):
            thr = threading.Thread(target=self.SavePic)
            thr.start()
    
    if __name__ == "__main__":
        tag = sys.argv[1:]
        savepic = Compress_Pic_or_Video(tag[0],tag[1],tag[2])
        print(savepic.Compress_Picture())
    
    

    相关链接

    如果出现了任何问题,请将问题出现的情景发送到邮箱 moriarty0305@icloud.com


  • 相关阅读:
    Tomcat安装和使用
    mysql5.7.18安装配置
    Memcached安装与使用
    Redis
    nginx的安装与使用
    python操作mysql
    Paramiko模块
    协程与异步IO
    Queue与生产者消费者模型
    C# 生成验证码 方法二
  • 原文地址:https://www.cnblogs.com/pteromyini/p/12374816.html
Copyright © 2011-2022 走看看