Skip to content

Instantly share code, notes, and snippets.

@16892434
Forked from Tmn07/fp.py
Created May 11, 2023 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 16892434/e1acc7d1a4da05e7670138c35d50b9b5 to your computer and use it in GitHub Desktop.
Save 16892434/e1acc7d1a4da05e7670138c35d50b9b5 to your computer and use it in GitHub Desktop.
懒人压制脚本-linux平台
#!/usr/bin/env python3
import subprocess
import json
import argparse
from os import listdir
filename = "ML2nd-day1-2.mp4"
for i in range(3):
ss = str(i).zfill(2)
to = str(i+1).zfill(2)
cmd = f'ffmpeg -ss {ss}:53 -to {to}:10 -accurate_seek -i "{filename}" -codec copy -avoid_negative_ts 1 -map_metadata -1 out{i}.mp4'
print(cmd)
# print("[CMD]: "+cmd)
# subprocess.run(cmd, shell=True)

说明

压制参数来自对丸子工具箱2pass压制的测试得到的,或许通用性会有问题,仅做个人使用。 原意是为了批量压制高清视频符合B站的码率要求,不过现在B站码率放开了,脚本在我写完用完的第二天可能失去了意义,现在简单公开一下吧..

软件安装相关参考

ffmpeg new version https://blog.csdn.net/thomaszhaoyc/article/details/78233555?utm_source=blogxgwz8

sudo apt-get install libfaac-dev sudo apt-get install libfdk-aac-dev libass-dev libopus-dev
libtheora-dev libvorbis-dev libvpx-dev libssl-dev https://www.cnblogs.com/blackhumour2018/p/9400415.html

sudo apt install x264 https://tari.in/www/software/neroaac/ https://www.videohelp.com/software/MP4Box

#!/usr/bin/env python3
import subprocess
import json
import argparse
def loadJson(json_file):
with open(json_file, 'r') as fp:
data = json.load(fp)
return data
def ChangeSuffix(filename, suf):
tmp = filename.split('.')
tmp[-1] = suf
return '.'.join(tmp)
def RunCmd(cmd):
print("[CMD]: "+cmd)
subprocess.run(cmd, shell=True)
def getAudioInfo(filename):
cmd = f"ffprobe -v quiet -print_format json -show_format -show_streams '{filename}' > tmp.json"
RunCmd(cmd)
data = loadJson("tmp.json")
acode = data["streams"][1]["codec_name"]
return acode
def CopyAudio(filename):
# aac 原始文件超过320的....
cmd = f'ffmpeg -i "{filename}" -vn -sn -c:a copy -y -map 0:a:0 atemp.aac'
RunCmd(cmd)
return 'atemp.aac'
def ProcessAudio(filename, sr=320000):
cmd = f'ffmpeg -i "{filename}" -vn -sn -v 0 -c:a pcm_s16le -f wav pipe: | neroAacEnc -ignorelength -lc -br {sr} -if - -of "atemp.mp4"'
RunCmd(cmd)
return "atemp.mp4"
def MergeAV(vfile, afile, filename):
filename = ChangeSuffix(filename, 'mp4')
cmd = f'MP4Box -add "{vfile}#trackID=1:name=" -add "{afile}#trackID=1:name=" -new "./finish/{filename}"'
RunCmd(cmd)
def TwoPass(filename, sr=5600):
pass1_cmd = f'/usr/bin/x264 --pass 1 --bitrate {sr} --stats "vtemp.stats" --preset 8 -I 300 -r 4 -b 3 --me umh -i 1 --scenecut 60 -f 1:1 --qcomp 0.5 --psy-rd 0.3:0 --aq-mode 2 --aq-strength 0.8 -o NUL "{filename}"'
pass2_cmd = f'/usr/bin/x264 --pass 2 --bitrate {sr} --stats "vtemp.stats" --preset 8 -I 300 -r 4 -b 3 --me umh -i 1 --scenecut 60 -f 1:1 --qcomp 0.5 --psy-rd 0.3:0 --aq-mode 2 --aq-strength 0.8 -o "vtemp.mp4" "{filename}"'
RunCmd(pass1_cmd)
RunCmd(pass2_cmd)
return "vtemp.mp4"
def Del():
cmd = "rm tmp.json atemp.mp4 vtemp.mp4 NUL vtemp.stats vtemp.stats.mbtree"
RunCmd(cmd)
def main():
parser = argparse.ArgumentParser(description='懒人压制脚本...')
parser.add_argument('-i', '--input', help='set the filename')
parser.add_argument('-a', default=320000, help='set audio sample rate')
parser.add_argument('-v', default=5600, help='set video sample rate')
# parser.add_argument('-o', '--output', help='set the filename to store')
args = parser.parse_args()
filename = args.input
asr = args.a
vsr = args.v
if filename==None:
return 0
acode = getAudioInfo(filename)
print(f'acode = {acode}')
if acode == "acc":
afile = CopyAudio(filename)
else:
afile = ProcessAudio(filename, asr)
vfile = TwoPass(filename, vsr)
MergeAV(afile, vfile, filename)
# Del()
main()
#!/usr/bin/env python3
import subprocess
from os import listdir
def RunCmd(cmd):
print("[CMD]: "+cmd)
subprocess.run(cmd, shell=True)
files = listdir('.')
files.sort()
for f in files:
if f.startswith("0"):
cmd = f"python ff.py -i '{f}'"
# print(cmd)
RunCmd(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment