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码来对待,但 ...
随机推荐
- js 数组的浅拷贝和深拷贝
1.背景介绍 javascript分原始类型与引用类型.Array是引用类型,直接用"="号赋值的话,只是把源数组的地址(或叫指针)赋值给目的数组,指向的是同一个内存地址,其中一个 ...
- 计算异质性H值(运用arcgis和Python进行区域分析)
最近需要对ecognition分割结果进行统计分析,以此来进一步判断其分割结果中的欠分割和过分割对象,在看了一篇论文后,发现了可以用一个参数H来判断每个切割对象的异质性,由于此方法需要用到arcgis ...
- HTML5中window.postMessage,在两个页面之间的数据传递
HTML5中window.postMessage,在两个页面之间的数据传递 2015年11月3日 8536次浏览 关于postMessage window.postMessage虽然说是html5的功 ...
- C++11多线程编程(常见面试题)
[题目1] 子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次,如此循环50次,试写出代码 [题解] 首先我们来分析一下这道题...( ...
- 蛇形填数(JAVA语言)
package 第三章; import java.util.Scanner; public class 蛇形填数 { public static void main(String[] args) { ...
- PTA 数组循环右移
6-2 数组循环右移 (20 分) 本题要求实现一个对数组进行循环右移的简单函数:一个数组a中存有n(>)个整数,将每个整数循环向右移m(≥)个位置,即将a中的数据由(a0a1⋯ ...
- 实践解析丨Rust 内置 trait:PartialEq 和 Eq
摘要:Rust 在很多地方使用了 traits, 从非常浅显的操作符重载, 到 Send, Sync 这种非常微妙的特性. Rust 在很多地方使用了 traits, 从非常浅显的操作符重载, 到 S ...
- 如何获取占用U盘的进程
依次打开开始---所有程序---附件---系统工具---资源监视器. 打开CPU标签栏,在"关联的句柄"中的搜索框中输入U盘的盘符,如G: 按回车搜索即可出结果. 在搜索结果中右键 ...
- 【pytorch学习笔记0】-CNN与LSTM输入输出维度含义
卷积data的四个维度: batch, input channel, height, width Conv2d的四个维度: input channel, output channel, kernel, ...
- (数据科学学习手札114)Python+Dash快速web应用开发——上传下载篇
本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 这是我的系列教程Python+Dash快速web ...