惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
GbyAI
GbyAI
Hugging Face - Blog
Hugging Face - Blog
N
Netflix TechBlog - Medium
博客园 - 聂微东
Y
Y Combinator Blog
罗磊的独立博客
博客园_首页
小众软件
小众软件
有赞技术团队
有赞技术团队
爱范儿
爱范儿
F
Fortinet All Blogs
C
Check Point Blog
Google DeepMind News
Google DeepMind News
云风的 BLOG
云风的 BLOG
Apple Machine Learning Research
Apple Machine Learning Research
M
MIT News - Artificial intelligence
月光博客
月光博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
aimingoo的专栏
aimingoo的专栏

又见苍岚

COLMAP PatchMatch Stereo 算法详解 事件驱动的状态机框架:从理论到工程实践 Git 在国内网络环境下无法 Push 的排查与修复 —— 配置 Clash 代理 分段五次多项式插值原理详解 路径插值方法深度对比研究 Claude Code 使用指南 OpenClaw 记忆管理与技能创建指南 CBS(Conflict-Based Search)算法详解 A* 算法及其变种详解 OpenClaw 配置多 Agents Windows Powershell 无法加载文件,因为在此系统上禁止运行脚本问题的解决方案 MaxClaw 安装流程 大模型 AI 名词介绍 AList 网盘聚合工具简介 Protobuf 简介与测试 Claude Code 简介以及 GLM 4.7 模型接入 Github 歌词下载工具 163MusicLyrics Python __getattr__ 懒加载 Python TypedDict 机器人仿真平台 Gazebo 安装记录 机器人仿真平台 Gazebo 简介 多机器人路径规划问题(Multi-Agent Path Finding, MAPF)简介 Python exifread 读取修改过的 jpeg 信息错误问题修复 3D 坐标系变换的理解 3D 旋转矩阵基本概念 MongoDB Compass 介绍 Python 环境管理工具 uv Flutter 开发指南 Snipaste 安装下载与黑屏问题解决方案 全局路径规划算法记录
FFmpeg 视频压缩与 Python 调用方法
Yiwei Zhang · 2023-01-27 · via 又见苍岚
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import sys
import os
import vvdutils as vv
import cv2

class Compress_Pic_or_Video(object):
def __init__(self):
pass

def is_video(self, file_path):
videoSuffixSet = {"WMV","ASF","ASX","RM","RMVB","MP4","3GP","MOV","M4V","AVI","DAT","MKV","FIV","VOB"}
suffix = vv.get_suffix(file_path).upper()
if suffix in videoSuffixSet:
return True
else:
return False

def infer_video_file(self, file_path, delete_origin_file=False):

if not self.is_video(file_path):
return
else:
width, height = self.get_target_size(file_path)
self.SaveVideo(file_path, width, height, delete_origin_file)

def get_target_size(self, file_path):
cap = cv2.VideoCapture(file_path)
cap.set(1, 1) # 取它的第一帧
rval, frame = cap.read() # 如果rval为False表示这个视频有问题,为True则正常
cap.release()
height = frame.shape[0] # 高度
width = frame.shape[1] # 宽度

max_size = 1280
factor = min(1, max_size / max(height, width))

width = int(width * factor // 2 * 2)
height = int(height * factor // 2 * 2)
return width, height

def SaveVideo(self, file_path, width, height, delete_origin_file=False):

print('width', width, 'height', height)

fpsize = os.path.getsize(file_path) / 1024

file_path = file_path.replace(' ', '\ ')
file_path = file_path.replace('(', '\(')
file_path = file_path.replace(')', '\)')

dir_path= vv.OS_dirname(file_path)
stem = vv.get_path_stem(file_path)
output_path = vv.OS_join(dir_path, stem + '_compress.mp4')

print(f"file path: {file_path}")
print(f"output path: {output_path}")

if fpsize >= 150.0: # 大于150KB的视频需要压缩
compress = "ffmpeg -y -i {} -r 12 -s {}x{} -b:v 200k {}".format(file_path, width, height, output_path)
isRun = os.system(compress)
if isRun != 0:
return (isRun,"没有安装ffmpeg")
else:
if delete_origin_file:
vv.remove_file(file_path)
return True
else:
return True

if __name__ == "__main__":
b = sys.argv
try:
dir_path = b[1]
except:
dir_path = '.'

comp_obj = Compress_Pic_or_Video()
mp4_file_path_list = vv.glob_videos(dir_path)

for file_path in vv.tqdm(mp4_file_path_list):
if '_compress.mp4' not in file_path:
try:
comp_obj.infer_video_file(file_path, True)
except Exception as e:
print(e)
print(f" !! Video {file_path} is error. ")