• 首页
  • ML/DL
  • 视觉相关
  • 数据结构与算法
  • Python
  • Java
  • WEB开发
  • 不知道分到哪里
Pteromyini的小站
没错Pteromyini就是树树的网名
  1. 首页
  2. Python
  3. 正文

Python视频压缩

2020年01月18日 40点热度 1人点赞 0条评论

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


本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: 暂无
最后更新:2020年01月19日

Pteromyini

树树虽然是个小辣鸡,但是树树想变强~~

打赏 点赞
下一篇 >

树树虽然是个小辣鸡,但是树树想变强~~

最新 热点 随机
最新 热点 随机
将博客搬至CSDN Dijkstra 最短路算法 windows安装TeX Live 2019及TeXstudio iwrite复制攻略 爬取纽约时报特定关键词新闻并计数 简单写个logictic回归 植树节快到了-那就种棵决策树吧 简简单单做个房价预测 (ML邹博)回归
将博客搬至CSDN
(Github搬砖)动手学深度学习(TF2.0版)-3.6 softmax回归的从零开始实现 windows安装TeX Live 2019及TeXstudio (Py练习)判断101-200之间的素数个数并输出 web技术培训(二)-Flask后端框架初识 JAVA8的安装 (机器学习基石)Perceptron Learning Algorithm(PLA) Dijkstra 最短路算法 (Github搬砖)动手学深度学习(TF2.0版)-3.11 模型选择、欠拟合和过拟合 (邹博ML)矩阵和线性代数
友情链接

博客园博客

远处有泽细细说




标签聚合
微机原理 ty-Python ty-未分类 贪心CV py爬虫 ty-深度学习 ty-WEB开发 ty-数据结构与算法

COPYRIGHT © 2020 .Pteromyini ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS

鲁ICP备19000938号-2

鲁公网安备 37160202000443号