[O] Refactor video.py
This commit is contained in:
@@ -2,13 +2,16 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import platform
|
||||||
import re
|
import re
|
||||||
|
from subprocess import Popen
|
||||||
import sys
|
import sys
|
||||||
|
import shlex
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
def comp(input: str = 'latest', proc: str = 'cpu', codec: str = 'x264', crf: int = 24, br: int = 500,
|
def comp(input: str = 'latest', proc: str = 'cpu', codec: str = 'x264', crf: int = 24, br: int = None,
|
||||||
cmd: bool = False, aargs: str = '', suffix: str = 'mp4'):
|
cmd: bool = False, aargs: str = '', suffix: str = 'mp4'):
|
||||||
"""
|
"""
|
||||||
Compress video
|
Compress video
|
||||||
@@ -36,17 +39,26 @@ def comp(input: str = 'latest', proc: str = 'cpu', codec: str = 'x264', crf: int
|
|||||||
out += f'-{crf}'
|
out += f'-{crf}'
|
||||||
out += f'.{suffix}'
|
out += f'.{suffix}'
|
||||||
|
|
||||||
|
c = ['ffmpeg', '-i', i]
|
||||||
|
|
||||||
if proc == 'c':
|
if proc == 'c':
|
||||||
c = f'ffmpeg -i "{i}" -vcodec lib{codec} -crf {crf} {aargs} "{out}"'
|
c += ['-c:v', f'lib{codec}', '-crf', str(crf)]
|
||||||
elif proc == 'g':
|
elif proc == 'g':
|
||||||
c = f'ffmpeg -i "{i}" -c:v {codec}_videotoolbox -b:v {br}k {aargs} "{out}"'
|
if platform.system() == 'Darwin':
|
||||||
|
c += ['-c:v', f'{codec}_videotoolbox']
|
||||||
|
else:
|
||||||
|
c += ['-c:v', f'{codec}_nvenc', '-cq', str(crf)]
|
||||||
else:
|
else:
|
||||||
raise AssertionError(f'Processor is invalid ({codec}[0] not in "cg")')
|
raise AssertionError(f'Processor is invalid ({codec}[0] not in "cg")')
|
||||||
|
|
||||||
|
if br:
|
||||||
|
c += ['-b:v', f'{br}k', '-maxrate', f'{br}k', '-bufsize', f'2M']
|
||||||
|
|
||||||
|
c += shlex.split(aargs) + [out]
|
||||||
|
|
||||||
if cmd:
|
print(c)
|
||||||
print(c)
|
if not cmd:
|
||||||
else:
|
Popen(c).wait()
|
||||||
os.system(c)
|
|
||||||
|
|
||||||
|
|
||||||
def combine(format: str, output: str | Path):
|
def combine(format: str, output: str | Path):
|
||||||
|
|||||||
Reference in New Issue
Block a user