Repository navigation
Expand file tree
/
Copy pathBitNowHigh.py
More file actions
129 lines (110 loc) · 4.37 KB
/
Copy pathBitNowHigh.py
File metadata and controls
129 lines (110 loc) · 4.37 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 视频生成与导出模块
from future.backports.email.utils import encode_rfc2231
from tkinter import ttk, messagebox, filedialog
from fractions import Fraction
from PIL import Image, ImageTk
import BitNowHigh as bnh
import tkinter as tk
import subprocess
import webbrowser
import pathlib
import cv2
import os
import subprocess, pathlib, sys, os
VERSION = '0.0.2'
def generate_command(srcfilepath,outfilename='output',outfilepath='./',width=1920,height=1080,fps=60,qual=30,speed=1.0,encoder="libx264"):
'''
生成视频
:param srcfilepath:str,源文件路径,可以相对可以绝对
:param outfilename:str,输出文件基础名称(输出视频名会在基础名后加上表示规格的字符)
:param outfilepath:str,末尾带斜杠的文件输出路径(不含文件名)
:param width,height:int,宽高
:param fps:float,帧率,为None则使用原视频帧率
:param qual:int,画质crf值,默认为30,取值1~51,越低画质越高,体积越大
:param speed:float,播放倍速,默认为1
:param encoder:str,编码器,默认为libx264。有两个选择:'libx264'与'libx265',前者效率高体积大
:return: command:list,代码列表。要得到一行代码需要' '.join(list)
'''
if outfilepath[-1] != '/':
outfilepath += '/'
outfilepath=fr"{outfilepath}{outfilename}[{qual}-{width}x{height}].mp4"
src = pathlib.Path(srcfilepath)
dst = pathlib.Path(outfilepath)
if not src.exists():
sys.exit(f"找不到文件 {src}")
return {'code':'001','err':f"找不到文件 {src}"}
video_filters = [f"scale={width}:{height}"]
audio_filters = []
if speed != 1.0:
video_filters.append(f"setpts=PTS/{speed}")
if speed != 1.0:
video_filters.append(f"setpts=PTS/{speed}")
if speed >= 0.5 and speed <= 2.0:
audio_filters.append(f"atempo={speed}")
elif speed > 2.0:
temp_speed = speed
while temp_speed > 2.0:
audio_filters.append("atempo=2.0")
temp_speed /= 2.0
if temp_speed > 0.5:
audio_filters.append(f"atempo={temp_speed}")
elif speed < 0.5:
temp_speed = speed
while temp_speed < 0.5:
audio_filters.append("atempo=0.5")
temp_speed /= 0.5
if temp_speed < 2.0 and temp_speed >= 0.5:
audio_filters.append(f"atempo={temp_speed}")
cmd = [
"ffmpeg", "-y",
"-i", str(src),
"-c:v", encoder,
"-preset", "7",
"-crf", f"{qual}",
"-c:a", "aac",
'-r',f'{fps}',
]
if video_filters:
cmd.extend(['-vf',','.join(video_filters)])
if audio_filters:
cmd.extend(['-af',','.join(audio_filters)])
cmd.extend([str(dst)])
return cmd
def generate_video_by_list(opts):
'''
通过传入
'''
def generate_video(srcfilepath,outfilename='output',outfilepath='./',width=1920,height=1080,fps=60,qual=30,speed=1,encoder="libx264"):
'''
通过参数生成视频
:param srcfilepath:str,源文件路径,可以相对可以绝对
:param outfilename:str,输出文件基础名称(输出视频名会在基础名后加上表示规格的字符)
:param outfilepath:str,末尾带斜杠的文件输出路径(不含文件名)
:param width,height:int,宽高
:param fps:float,帧率,为None则使用原视频帧率
:param qual:int,画质crf值,默认为30,取值1~51,越低画质越高,体积越大
:param speed:float,播放倍速,默认为1
:param encoder:str,编码器,默认为libx264。有两个选择:'libx264'与'libx265',前者效率高体积大
:return: dict,按照以下格式存储信息:{'code':'000','err':none,},000表示顺利完成
'''
dst = pathlib.Path(outfilepath)
cmd=generate_command(srcfilepath,outfilename,outfilepath,width,height,fps,qual,speed,encoder)
proc = subprocess.Popen(
cmd,
encoding="utf-8",
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1
)
for line in proc.stdout:
print(line, end='')
proc.wait()
print(str(dst))
return {
'code':'000',
'file':str(dst),
'err':None,
}
if __name__ == "__main__":
print(generate_command(r"video.mp4"))