Python视频压缩
html, body { font-family: "SF UI Display", ".PingFang SC", "PingFang SC", "Neue Haas Grotesk Text Pro", "Arial Nova", "Segoe UI", "Microsoft YaHei", "Microsoft JhengHei", "Helvetica Neue", "Source Han Sans SC", "Noto Sans CJK SC", "Source Han Sans CN", "Noto Sans SC", "Source Han Sans TC", "Noto Sans CJK TC", "Hiragino Sans GB", sans-serif; font-size: 16px; min-width: 200px; max-width: 760px; margin: 0 auto; padding: 1rem; line-height: 1.5rem }
h1, h2, h3, h4, h5, h6 { font-family: "PT Sans", "SF UI Display", ".PingFang SC", "PingFang SC", "Neue Haas Grotesk Text Pro", "Arial Nova", "Segoe UI", "Microsoft YaHei", "Microsoft JhengHei", "Helvetica Neue", "Source Han Sans SC", "Noto Sans CJK SC", "Source Han Sans CN", "Noto Sans SC", "Source Han Sans TC", "Noto Sans CJK TC", "Hiragino Sans GB", sans-serif; text-rendering: optimizelegibility; margin-bottom: 1em; font-weight: bold; line-height: 1.8rem }
h1, h2 { position: relative; padding-top: 1rem; padding-bottom: 0.2rem; margin-bottom: 1rem; border-bottom: 1px solid rgba(238, 238, 238, 1) }
h2 { padding-top: 0.8rem; padding-bottom: 0.2rem }
h1 { font-size: 1.6rem }
h2 { font-size: 1.4rem }
h3 { font-size: 1.2rem }
h4 { font-size: 1.1rem }
h5 { font-size: 1rem }
h6 { font-size: 0.9rem }
table { border-collapse: collapse; border-spacing: 0; margin-top: 0.8rem; margin-bottom: 1.4rem }
tr { background-color: rgba(255, 255, 255, 1); border-top: 1px solid rgba(204, 204, 204, 1) }
th, td { padding: 5px 14px; border: 1px solid rgba(221, 221, 221, 1) }
blockquote { font-style: italic; font-size: 1.1em; line-height: 1.5em; padding-left: 1em; border-left: 4px solid rgba(213, 213, 213, 1); margin-left: 0; margin-right: 0; margin-bottom: 1.5rem }
a { color: rgba(24, 99, 161, 1) }
a:hover { color: rgba(27, 67, 141, 1) }
pre, code, p code, li code { font-family: Menlo, Monaco, "Andale Mono", "lucida console", "Courier New", monospace }
pre { -webkit-border-radius: 0.4em; -moz-border-radius: 0.4em; -ms-border-radius: 0.4em; -o-border-radius: 0.4em; border-radius: 0.4em; border: 1px solid rgba(231, 222, 195, 1); line-height: 1.45em; font-size: 0.9rem; margin-bottom: 2.1em; padding: 0.8em 1em; color: rgba(88, 110, 117, 1); overflow: auto; background-color: rgba(253, 246, 227, 1) }
:not(pre)>code { display: inline-block; text-indent: 0; background: rgba(255, 255, 255, 1); font-size: 0.9rem; line-height: 1.5em; color: rgba(85, 85, 85, 1); border: 1px solid rgba(221, 221, 221, 1); -webkit-border-radius: 0.4em; -moz-border-radius: 0.4em; -ms-border-radius: 0.4em; -o-border-radius: 0.4em; border-radius: 0.4em; padding: 0 0.3em; margin: -1px 4px }
pre code { font-size: 1em !important; border: none }
img { max-width: 100%; padding: 8px 0 }
hr { height: 0; margin: 15px 0; overflow: hidden; background: rgba(0, 0, 0, 0); border-top: 0; border-right: 0; border-bottom: 1px solid rgba(221, 221, 221, 1); border-left: 0 }
figcaption { text-align: center }
code[class*="language-"], pre[class*="language-"] { color: rgba(101, 123, 131, 1); font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; word-wrap: normal; line-height: 1.5; -moz-tab-size: 4; -o-tab-size: 4; tab-size: 4; -webkit-hyphens: none; -moz-hyphens: none; -ms-hyphens: none; hyphens: none }
{ background: rgba(7, 54, 66, 1) }
pre[class*="language-"]::selection, pre[class*="language-"] ::selection, code[class*="language-"]::selection, code[class*="language-"] ::selection { background: rgba(7, 54, 66, 1) }
pre[class*="language-"] { padding: 1em; margin: 0.5em 0; overflow: auto; border-radius: 0.3em }
:not(pre)>code[class*="language-"], pre[class*="language-"] { background-color: rgba(253, 246, 227, 1) }
:not(pre)>code[class*="language-"] { padding: 0.1em; border-radius: 0.3em }
.token.comment, .token.prolog, .token.doctype, .token.cdata { color: rgba(147, 161, 161, 1) }
.token.punctuation { color: rgba(88, 110, 117, 1) }
.namespace { opacity: 0.7 }
.token.property, .token.tag, .token.boolean, .token.number, .token.constant, .token.symbol, .token.deleted { color: rgba(38, 139, 210, 1) }
.token.selector, .token.attr-name, .token.string, .token.char, .token.builtin, .token.url, .token.inserted { color: rgba(42, 161, 152, 1) }
.token.entity { color: rgba(101, 123, 131, 1); background: rgba(238, 232, 213, 1) }
.token.atrule, .token.attr-value, .token.keyword { color: rgba(133, 153, 0, 1) }
.token.function, .token.class-name { color: rgba(181, 137, 0, 1) }
.token.regex, .token.important, .token.variable { color: rgba(203, 75, 22, 1) }
.token.important, .token.bold { font-weight: bold }
.token.italic { font-style: italic }
.token.entity { cursor: help }
pre[class*="language-"].line-numbers { position: relative; padding-left: 3.8em; counter-reset: linenumber 0 }
pre[class*="language-"].line-numbers>code { position: relative; white-space: inherit }
.line-numbers .line-numbers-rows { position: absolute; pointer-events: none; top: 0; font-size: 100%; left: -3.8em; width: 3em; letter-spacing: -1px; border-right: 1px solid rgba(153, 153, 153, 1); -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none }
.line-numbers-rows>span { pointer-events: none; display: block; counter-increment: linenumber 1 }
.line-numbers-rows>span:before { content: counter(linenumber); color: rgba(153, 153, 153, 1); display: block; padding-right: 0.8em; text-align: right }
@media print { code[class*="language-"], pre[class*="language-"] { overflow: visible; word-wrap: break-word !important } }
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
使用方法
- 新建一个
moriarty.py
文件,将文章末尾的代码复制到文件中。 - 在python文件的文件夹中进入终端(或cmd)输入指令
python moriarty.py -address -input. -output.
其中-address
是所需要压缩的视频或文件的所在文件夹的绝对地址
-input.
是所需要压缩的视频或图片的名称及格式名。一定要加格式名
-output.
是压缩后的视频或文件的名称和格式名(名称可以自己取)。
- 压缩后的文件会出现在同一文件夹中。
代码
#视频压缩
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
Python视频压缩的更多相关文章
- Python+OpenCV4:读写输入和输出的简单实践(图片、视频、摄像头)
典型的文件处理流程如下: 利用命令行参数 sys.argv 命令行参数是读取文件时常用的方式. 命令行参数保存在 sys.argv 的列表中,列表的第一个元素是脚本名称,后面的元素是命令行参数: 通过 ...
- python使用zlib库压缩图片,使用ffmpeg压缩视频
python压缩图片.视频 图片压缩使用zlib库 视频压缩使用工具ffmpeg # ffmpeg -i 1.mp4 -r 10 -pix_fmt yuv420p -vcodec libx264 -p ...
- Python中的多进程与多线程(一)
一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
- Python 小而美的函数
python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况 any any(iterable) ...
- JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议
软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...
- 可爱的豆子——使用Beans思想让Python代码更易维护
title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...
- 使用Python保存屏幕截图(不使用PIL)
起因 在极客学院讲授<使用Python编写远程控制程序>的课程中,涉及到查看被控制电脑屏幕截图的功能. 如果使用PIL,这个需求只需要三行代码: from PIL import Image ...
- Python编码记录
字节流和字符串 当使用Python定义一个字符串时,实际会存储一个字节串: "abc"--[97][98][99] python2.x默认会把所有的字符串当做ASCII码来对待,但 ...
随机推荐
- Springboot的监控
目录 Micrometer 计数器 仪表 摘要 计时器 Prometheus grafana 保存后我们就能在dashboard上看得我我们的监控指标了参考 Spring Boot有个子项目Sprin ...
- c++ 11 是如何简化你的数据库访问接口的
之前写过一篇文章专门分析了 c++ 模板编译过程中报的一个错误:<fatal error C1045: 编译器限制 : 链接规范嵌套太深 >,其中涉及到了 qtl -- 一个使用 c++ ...
- 【博弈论】组合游戏及SG函数浅析
目录 预备知识 普通的Nim游戏 SG函数 预备知识 公平组合游戏(ICG) 若一个游戏满足: 由两名玩家交替行动: 游戏中任意时刻,合法操作集合只取决于这个局面本身: 若轮到某位选手时,若该选手无合 ...
- 8、MyBatis之使用注解开发
9.使用注解开发 mybatis最初配置信息是基于 XML ,映射语句(SQL)也是定义在 XML 中的.而到MyBatis 3提供了新的基于注解的配置.不幸的是,Java 注解的的表达力和灵活性十分 ...
- redis雪崩,穿透,击穿
缓存雪崩:同一时间大量key到过期时间失效,可在设置失效时间时加随机数,如果直接修改数据库,那么一定会有不一致,通过失效时间去反复刷新缩短不一致的时间, 为了避免数据一直存在,一定要设置过期时间如果通 ...
- LevelDB 源码解析之 Varint 编码
GitHub: https://github.com/storagezhang Emai: debugzhang@163.com 华为云社区: https://bbs.huaweicloud.com/ ...
- 什么是一致性hash?
一致性hash 前言 说出来大家可能不相信,我昨天做梦梦到自己在面试,然后面试官问了我这个问题哈哈~然后我就打算按照自己的理解写一写.如果有写的不对的欢迎大家指正! 直接开始 普通hash算法 普通h ...
- SCIP:构造过程抽象--面向对象的解释
心智的活动,除了尽力产生各种简单的认知之外,主要表现为如下三个方面:(1)将若干简单认知组合为一个复合的认识,由此产出各种复杂的认知.(2)将两个认知放在一起对照,不管他们如何简单或者复杂,在这样做时 ...
- A. 【例题1】数字反转
题目解析 字符串的基础操作,注意判断零即可 #include <bits/stdc++.h> using namespace std; int i; char c[15]; int mai ...
- Echarts概述
1. Echarts概述 ECharts是百度开源的纯 Javascript 图表库,目前开源可以与highcharts相匹敌的一个图表库.支持折线图(区域图).柱状图(条状图).散点图(气泡图).K ...