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


使用方法

  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

Python视频压缩的更多相关文章

  1. Python+OpenCV4:读写输入和输出的简单实践(图片、视频、摄像头)

    典型的文件处理流程如下: 利用命令行参数 sys.argv 命令行参数是读取文件时常用的方式. 命令行参数保存在 sys.argv 的列表中,列表的第一个元素是脚本名称,后面的元素是命令行参数: 通过 ...

  2. python使用zlib库压缩图片,使用ffmpeg压缩视频

    python压缩图片.视频 图片压缩使用zlib库 视频压缩使用工具ffmpeg # ffmpeg -i 1.mp4 -r 10 -pix_fmt yuv420p -vcodec libx264 -p ...

  3. Python中的多进程与多线程(一)

    一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...

  4. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  5. Python 小而美的函数

    python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况   any any(iterable) ...

  6. JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议

    软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...

  7. 可爱的豆子——使用Beans思想让Python代码更易维护

    title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...

  8. 使用Python保存屏幕截图(不使用PIL)

    起因 在极客学院讲授<使用Python编写远程控制程序>的课程中,涉及到查看被控制电脑屏幕截图的功能. 如果使用PIL,这个需求只需要三行代码: from PIL import Image ...

  9. Python编码记录

    字节流和字符串 当使用Python定义一个字符串时,实际会存储一个字节串: "abc"--[97][98][99] python2.x默认会把所有的字符串当做ASCII码来对待,但 ...

随机推荐

  1. WPF 基础 - 控件与布局

    1. 前言 1. 数据驱动 UI WPF 是数据核心.主动的,UI 从属数据并表达数据.是被动的: 不再是 UI 驱动数据,而是数据驱动 UI: 2. 控件的定义 控件.数据内容.行为(控件响应用户操 ...

  2. java常见面试题2:求出两个正整数的最大公约数

    概念: 最大公约数:两个整数共有因子中最大的一个 方法一: 如果两个数相等,则最大公约数为它本身,两个数不等,则用两个数依次除 两个数中最小的一个到 1,直到找到同时能被两个数除尽的那个数 代码清单: ...

  3. SpringMVC源码分析和启动流程

    https://yq.aliyun.com/articles/707995 在Spring的web容器启动时会去读取web.xml文件,相关启动顺序为:<context-param> -- ...

  4. 2019 GDUT Rating Contest II : Problem F. Teleportation

    题面: Problem F. Teleportation Input file: standard input Output file: standard output Time limit: 15 se ...

  5. Django之Auth认证模块

    一.Auth模块是什么 Auth模块是Django自带的用户认证模块: 我们在开发网站的时候,无可避免的需要设计实现网站的用户系统,此时我们需要实现包括用户注册,用户登陆,用户认证,注销修改密码等功能 ...

  6. 跨端开发技术 | 拼团商城项目同时开发app和小程序的要点

    此项目为拼团商城类型,主要功能包括商品分类.商品详情.商品搜索.拼团.订单管理等. 项目源码在 https://github.com/apicloudcom/group-ec 仓库的 widget 目 ...

  7. UML第二部分和创建型模式

    状态机视图通过对每个类的对象的生命期进行建模 描述了对象时间上的动态行为 .状态指就某个特定类而言 对于发生的事件具有相同性质响应的一系列对象值.状态机不但可以描述类的行为 而且可以描述用例 协作和方 ...

  8. Python 详解修饰器 附带 js使用修饰器

    修饰器 功能 修饰器的主要功能是,在不改变已有代码的情况下,为某一个类,方法等扩展功能 首先看这样一段代码 def foo(): for i in range(10): print(i) foo() ...

  9. java例题_11 求不重复数

    1 /*11 [程序 11 求不重复数字] 2 题目:有 1.2.3.4 这四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 3 程序分析:可填在百位.十位.个位的数字都是 1.2.3. ...

  10. 远程拷贝文件--scp

    scp [user@host1:]file1 [user@host2:]file2         将主机1下的某一路径下的文件拷贝到另一个主机下的某一路径 scp -r [user@host1:]d ...