|
|
@@ -174,6 +174,7 @@ def build_transform(rng: random.Random, cfg: dict, w: int, h: int, has_audio: bo
|
|
|
vf.append(f'setpts=PTS/{sp:.4f}')
|
|
|
af_parts.append(f'atempo={sp:.4f}')
|
|
|
t['变速'] = f'{sp:.3f}x'
|
|
|
+ t['_speed'] = sp
|
|
|
if rng.random() < cfg.get('p_pitch', 0): # 音调偏移:asetrate 升采样+atempo 补偿(时长不变)
|
|
|
pr = rng.uniform(*cfg['pitch'])
|
|
|
af_parts.append(f'asetrate=44100*{pr:.3f}')
|
|
|
@@ -199,7 +200,9 @@ def build_transform(rng: random.Random, cfg: dict, w: int, h: int, has_audio: bo
|
|
|
vf.append(f'fps={nf:.2f}')
|
|
|
t['帧率'] = f'{fps:.2f}→{nf:.2f}'
|
|
|
|
|
|
- return vf, af_parts, noise_amp, t
|
|
|
+ misc = {'speed': float(t.get('_speed', 1.0))} # 供 AB 模式同步辅助流时间轴
|
|
|
+ t.pop('_speed', None)
|
|
|
+ return vf, af_parts, noise_amp, t, misc
|
|
|
|
|
|
|
|
|
def encode_args(hw: bool, interlace: str, crf: int, enc_preset: str):
|
|
|
@@ -269,6 +272,12 @@ def main():
|
|
|
ap.add_argument('--overlay', help='叠加一张半透明图片覆盖全画面(可配合 --overlay-alpha)')
|
|
|
ap.add_argument('--overlay-alpha', type=float, default=0.05,
|
|
|
help='叠加图层不透明度 0~1,默认 0.05(即 5%% 混合,肉眼近无感)')
|
|
|
+ ap.add_argument('--pair-video', metavar='PATH',
|
|
|
+ help='AB双视频模式:辅助视频与主视频帧级交错(8.18 AB视频模式同源)。'
|
|
|
+ '辅助视频缩放到主视频尺寸,时间轴与主视频同步(含变速),音频取主视频')
|
|
|
+ ap.add_argument('--pair-ratio', type=float, default=0.25,
|
|
|
+ help='AB交错中来自辅助视频的帧占比 0.05~0.5,默认 0.25(每4帧换1帧);'
|
|
|
+ '0.5 为逐帧交替(interleave,输出帧率翻倍、两流动作均全速保留)')
|
|
|
ap.add_argument('--no-mirror', action='store_true',
|
|
|
help='禁用镜像翻转(画面不左右翻转;文字/标识不反向)')
|
|
|
ap.add_argument('--same-name', action='store_true', help='输出保持原文件名(默认追加随机后缀)')
|
|
|
@@ -298,10 +307,16 @@ def main():
|
|
|
cfg['p_hflip'] = 0.0
|
|
|
if args.interlace != 'off' and args.hw:
|
|
|
print('注意: 隔行档仅 CPU 编码生效,--hw 下自动忽略隔行参数\n')
|
|
|
+ pv = Path(args.pair_video) if args.pair_video else None
|
|
|
+ if pv is not None:
|
|
|
+ if not pv.is_file():
|
|
|
+ sys.exit(f'辅助视频不存在: {pv}')
|
|
|
+ if not (0.05 <= args.pair_ratio <= 0.5):
|
|
|
+ sys.exit('--pair-ratio 取值范围 0.05~0.5')
|
|
|
report_path = out_dir / '处理报告.csv'
|
|
|
fields = ['文件名', '输出名', '状态', '原分辨率', '原时长(s)', '新时长(s)',
|
|
|
'镜像', '裁剪', '旋转', '色温', 'HSL', '动态色相', '颗粒', '边框', '调色',
|
|
|
- '变速', '音调', '音频压缩', '颤音', '粉噪', '帧率', '叠加图层', '隔行', '收尾']
|
|
|
+ '变速', '音调', '音频压缩', '颤音', '粉噪', '帧率', 'AB交错', '叠加图层', '隔行', '收尾']
|
|
|
rows, ok, fail = [], 0, 0
|
|
|
|
|
|
print(f'共 {len(files)} 个视频,档位 {args.preset},输出 → {out_dir.resolve()}\n')
|
|
|
@@ -314,15 +329,22 @@ def main():
|
|
|
# 帧率微调前置检查:标称帧率与实测包率偏差>10% 视为时间戳异常源,跳过 fps 重采样
|
|
|
fps_bad = (info['fps'] and info['real_fps'] and
|
|
|
abs(info['real_fps'] - info['fps']) / info['fps'] > 0.10)
|
|
|
- if fps_bad:
|
|
|
+ pair_info = probe(pv) if pv is not None else None
|
|
|
+ # AB 模式双流须统一到同一基准帧率:病源用实测帧率取整,正常源用标称
|
|
|
+ base_nf = round(info['real_fps']) if (pair_info and fps_bad and info['real_fps']) else info['fps']
|
|
|
+ if pair_info:
|
|
|
+ cfg_file = dict(cfg, p_fps=0.0)
|
|
|
+ elif fps_bad:
|
|
|
cfg_file = dict(cfg, p_fps=0.0)
|
|
|
else:
|
|
|
cfg_file = cfg
|
|
|
- vf, af_parts, noise_amp, t = build_transform(
|
|
|
+ vf, af_parts, noise_amp, t, misc = build_transform(
|
|
|
rng, cfg_file, w, h, info['has_audio'], info['fps'])
|
|
|
if fps_bad:
|
|
|
t['帧率'] = (f"跳过(标称{info['fps']:.2f}/实测{info['real_fps']:.2f}fps,"
|
|
|
"时间戳异常,重采样会损动作)")
|
|
|
+ elif pair_info:
|
|
|
+ t['帧率'] = f'跳过(AB模式统一基准{base_nf:g}fps)'
|
|
|
row['原分辨率'] = f"{info['w']}x{info['h']}"
|
|
|
row['原时长(s)'] = f"{info['dur']:.1f}"
|
|
|
row.update(t)
|
|
|
@@ -344,20 +366,70 @@ def main():
|
|
|
if ov is not None and not ov.is_file():
|
|
|
raise RuntimeError(f'叠加图层不存在: {ov}')
|
|
|
use_noise = noise_amp is not None
|
|
|
- noise_idx = 2 if ov is not None else 1
|
|
|
+ use_pair = pair_info is not None
|
|
|
+ # 输入序:0=主视频 [1=辅助视频] [2|1=蒙版图] [末=lavfi粉噪]
|
|
|
+ noise_idx = (1 if use_pair else 0) + (2 if ov is not None else 1)
|
|
|
+ sp = misc['speed']
|
|
|
+ # 输出时长基准:主视频变速后时长;辅助视频同步变速,短了按帧数补、混合后 trim 锁长
|
|
|
+ out_dur = info['dur'] / sp
|
|
|
+ n_out = int(out_dur * base_nf + 0.5) + 1
|
|
|
+ pair_shorter = use_pair and pair_info['dur'] < info['dur'] - 0.05
|
|
|
+ pad_frames = (int((info['dur'] - pair_info['dur']) / sp * base_nf) + 8
|
|
|
+ if pair_shorter else 0)
|
|
|
+ pair_m = max(2, min(20, round(1 / args.pair_ratio)))
|
|
|
+ pair_interleave = args.pair_ratio >= 0.5
|
|
|
|
|
|
def assemble(hw: bool):
|
|
|
cmd = ['ffmpeg', '-y', '-hide_banner', '-loglevel', 'error', '-i', str(f)]
|
|
|
+ if use_pair:
|
|
|
+ cmd += ['-i', str(pv)]
|
|
|
if ov is not None:
|
|
|
cmd += ['-i', str(ov)]
|
|
|
if use_noise:
|
|
|
cmd += ['-f', 'lavfi', '-i',
|
|
|
f'anoisesrc=color=pink:amplitude={noise_amp:.5f}:sample_rate=44100']
|
|
|
fc = None
|
|
|
- if ov is not None or use_noise:
|
|
|
+ if use_pair or ov is not None or use_noise:
|
|
|
parts = []
|
|
|
vchain = ','.join(vf)
|
|
|
- if ov is not None:
|
|
|
+ if use_pair:
|
|
|
+ # 主流:随机变换 → 统一基准帧率;辅助流:同尺寸/同变速/同步率,短则末帧补齐
|
|
|
+ if fps_bad and info['real_fps']:
|
|
|
+ # 双pts病源:标称时间戳不可信,按帧计数重建(含变速),否则 fps 规整化会冻结动作
|
|
|
+ a_fix = (f'setpts=N/({info["real_fps"]:.4f}*{sp:.4f})/TB')
|
|
|
+ else:
|
|
|
+ a_fix = None
|
|
|
+ a_tail = (',' + a_fix if a_fix else '') + f',fps={base_nf:g}'
|
|
|
+ parts.append(f'[0:v]{vchain},format=yuv420p{a_tail}[va];')
|
|
|
+ bch = (f'[1:v]scale={w}:{h}:flags=lanczos,setsar=1,format=yuv420p')
|
|
|
+ if sp != 1.0:
|
|
|
+ bch += f',setpts=PTS/{sp:.4f}'
|
|
|
+ bch += f',fps={base_nf:g}'
|
|
|
+ if pair_shorter:
|
|
|
+ bch += f',tpad=stop={pad_frames}:stop_mode=clone'
|
|
|
+ parts.append(bch + '[vb];')
|
|
|
+ # trim 锁定输出为主视频时长(blend 的 framesync 会把先结束侧续到另一侧长度)
|
|
|
+ if pair_interleave:
|
|
|
+ # 8.18 原味:两流逐帧 interleave → 输出 2×基准帧率,两流动作全速保留
|
|
|
+ parts.append(f'[va][vb]interleave,shuffleframes=0 1,'
|
|
|
+ f'setpts=N/(2*{base_nf:g})/TB,'
|
|
|
+ f'trim=end_frame={n_out * 2}[v_mix];')
|
|
|
+ t['AB交错'] = f'interleave逐帧@{base_nf:g}*2fps'
|
|
|
+ else:
|
|
|
+ # 每 m 帧整帧替换为辅助视频(周期切换,时间轴对齐)
|
|
|
+ parts.append(f'[va][vb]blend=all_expr=if(eq(mod(N\\,{pair_m})\\,0)'
|
|
|
+ f'\\,B\\,A),trim=end_frame={n_out},'
|
|
|
+ f'setpts=PTS-STARTPTS[v_mix];')
|
|
|
+ t['AB交错'] = f'每{pair_m}帧1帧@{base_nf:g}fps'
|
|
|
+ if ov is not None:
|
|
|
+ ov_src = 2 if use_pair else 1
|
|
|
+ parts.append(f'[{ov_src}:v]scale={w}:{h},format=rgba,'
|
|
|
+ f'colorchannelmixer=aa={args.overlay_alpha}[ovx];')
|
|
|
+ parts.append('[v_mix][ovx]overlay=0:0[v];') # 静态图铺满全程
|
|
|
+ t['叠加图层'] = f'{args.overlay_alpha * 100:.0f}%'
|
|
|
+ else:
|
|
|
+ parts.append('[v_mix]null[v];')
|
|
|
+ elif ov is not None:
|
|
|
parts.append(f'[0:v]{vchain}[base];')
|
|
|
parts.append(f'[1:v]scale={w}:{h},format=rgba,'
|
|
|
f'colorchannelmixer=aa={args.overlay_alpha}[ov];')
|