batch-intro-outro

A standardized workflow for batch-adding intros and outros to large numbers of videos. Supports automatic matching of H.264 or HEVC encoding, fast concat demuxer concatenation without re-encoding, error repair, and duration statistics with Excel report output. Intended for scenarios of batch-adding intros/outros to courses or video libraries.

Installation

Review the source and dependencies with a prompt. A package is available to download.

Install with an AI assistant

Copy the prompt so your assistant can review the pinned revision and dependencies first.

View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "batch-intro-outro" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-654d6caeb9588953274eedd62ee11b110e94abbc3af27df5678da945727391f2. Verify the upstream source and version against https://skillhub.cn/skills/user_2ed69ff3/batch-intro-outro before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.

Source and setup

skillhub.cn

Setup guidance pending

See the original source for supported tools, dependencies, and permissions.

Installation test pending

A standardized workflow for batch-adding intros and outros to large numbers of videos. Supports automatic matching of H.264 or HEVC encoding, fast concat demuxer concatenation without re-encoding, error repair, and duration statistics with Excel report output. Intended for scenarios of batch-adding intros/outros to courses or video libraries.

0 Downloads

Introduction source: third_party · user_2ed69ff3 · 2.1.0 Reference

SkillPi interpretation

Tasks
Translation pending; see source documentation.
Software, accounts and paid services
Translation pending; see source documentation.
Limits and differences
Translation pending; see source documentation.
Testing
Not tested

Browse files

Browse files at the source ↗

Original SKILL.md

Open original ↗
Frontmatter
name: batch-intro-outro
description: "批量给大量视频添加片头片尾的标准化工作流。支持自动匹配编码H264或HEVC、concat demuxer快速拼接无重编码、异常修复、时长统计Excel报告。当用户提到批量添加片头片尾、给视频加片头片尾、课程视频加片头片尾时触发。"
agent_created: true

批量添加片头片尾 Skill

概述

本 skill 提供批量给大量视频(数百~数千个)添加统一片头和片尾的完整工作流,包括环境准备、批量处理、质量检查、异常修复和统计报告生成。

v2.1 新增:Windows/macOS 跨平台支持。


触发条件

当用户提到以下任意内容时,激活本 skill:

  • "批量添加片头片尾"
  • "给视频加片头片尾"
  • "视频拼接片头片尾"
  • "课程视频添加片头片尾"
  • "给视频加开头和结尾"

工作流程

按以下 7 个阶段顺序执行。

Phase 1:环境检查

获取 ffmpeg 路径(v2.1 跨平台自动检测,优先级从高到低):

  1. 环境变量 FFMPEG_PATH(用户显式指定)
  2. imageio_ffmpeg 捆绑版(推荐,跨平台兼容性最好)
  3. 系统 PATH 中的 ffmpeg(shutil.which("ffmpeg") / shutil.which("ffmpeg.exe"))
import os, shutil
from pathlib import Path

def get_ffmpeg():
    # 1. 环境变量
    env_ffmpeg = os.environ.get("FFMPEG_PATH", "")
    if env_ffmpeg and Path(env_ffmpeg).exists():
        return env_ffmpeg
    # 2. imageio_ffmpeg(跨平台推荐)
    try:
        import imageio_ffmpeg
        return imageio_ffmpeg.get_ffmpeg_exe()
    except Exception:
        pass
    # 3. 系统 PATH
    for name in ["ffmpeg", "ffmpeg.exe"]:
        found = shutil.which(name)
        if found:
            return found
    raise RuntimeError("找不到 ffmpeg!")

⚠️ 不要用系统/homebrew 的 ffmpeg,经常出现 x265 库版本不匹配导致无法运行。 imageio_ffmpeg 不提供 ffprobe,获取时长用 ffmpeg -i file 2>&1 | grep Duration。

Windows 用户特别注意:

  • 安装 imageio_ffmpeg 后会自动下载 Windows 版 ffmpeg 二进制
  • 如果使用系统 PATH 中的 ffmpeg,确保路径不含空格或特殊字符
  • 临时目录自动使用 %TEMP%(通常是 C:\Users\<用户名>\AppData\Local\Temp)

确认目录结构:

/路径/片头.mp4
/路径/片尾.mp4
/路径/原始视频目录/    ← 按课程分子目录
/路径/输出目录/        ← 自动创建

Phase 2:分析原始视频

扫描原始视频目录,统计关键信息,指导片头片尾标准化:

统计项 用途
分辨率分布 片头片尾缩放目标(取最常见)
编码分布(H264/HEVC) 决定标准化时用 libx264 还是 libx265
帧率分布 片头片尾帧率匹配
音频参数(采样率、声道) 音频必须完全匹配,否则 concat+copy 失败
色彩空间(color_primaries/trc/space) v2.0 新增:色彩空间突变可导致部分播放器卡住
Profile/Level v2.0 新增:参数集不一致可导致硬件解码器卡死

Phase 3:片头片尾标准化(scripts/standardize.py)

核心原则:按(分辨率 × 编码 × 帧率 × 音频参数 × 色彩空间 × pix_fmt)分组,每组只标准化一次,缓存结果。

标准化分两步:

  • Step A:分辨率/帧率/编码/音频标准化
  • Step B:时间戳标准化(-fflags +genpts+igndts + 剥离 SEI + 统一 timebase)

标准化命令:

# H264 版本
ffmpeg -i 片头.mp4 -c:v libx264 -crf 23 -preset medium \
  -vf "scale=W:H,fps=FPS" \
  -pix_fmt yuv420p \
  -c:a aac -ar 采样率 -ac 声道数 -b:a 128k \
  -force_key_frames "expr:eq(n,0)" \
  片头_标准化.mp4

# HEVC 版本(仅在原视频是 HEVC 时使用)
ffmpeg -i 片头.mp4 -c:v libx265 -crf 28 -preset medium \
  -vf "scale=W:H,fps=FPS" \
  -pix_fmt yuv420p \
  -c:a aac -ar 采样率 -ac 声道数 -b:a 128k \
  -force_key_frames "expr:eq(n,0)" \
  片头_标准化_HEVC.mp4

# v2.0 新增:时间戳标准化(Step B)
ffmpeg -i 片头_标准化.mp4 -c copy \
  -fflags +genpts+igndts \
  -map_metadata -1 \
  -video_track_timescale 90000 \
  -movflags +faststart \
  片头_标准化_fixed.mp4

Phase 4:批量拼接(scripts/batch_process.py)

优先使用 concat demuxer(无重编码,秒级/视频):

生成 concat_list.txt:

file '/path/to/intro_std_fixed.mp4'
file '/path/to/main_video_tsfix.mp4'
file '/path/to/outro_std_fixed.mp4'

执行(v2.0 增强版,彻底解决时间戳导致的卡住问题):

ffmpeg -f concat -safe 0 -i concat_list.txt \
  -c copy \
  -fflags +genpts+igndts \
  -map_metadata -1 \
  -video_track_timescale 90000 \
  -movflags +faststart \
  output.mp4

⚠️ v2.0 关键参数说明:

  • -fflags +genpts+igndts:+genpts 重新生成缺失的 PTS;+igndts 忽略无效的 DTS,防止非单调时间戳
  • -map_metadata -1:清除所有旧元数据,防止 MP4 edit list (elst) 错乱
  • -video_track_timescale 90000:强制统一 timebase,防止不同视频 timebase 不一致导致溢出/不连续
  • 预处理阶段已剥离 SEI (filter_units=remove_types=6):防止 pic_timing SEI 携带过期时序信息

concat+copy 失败时,用 concat filter 重编码:

ffmpeg -i intro.mp4 -i main.mp4 -i outro.mp4 \
  -filter_complex "[0:v]setpts=PTS-STARTPTS[v0];[1:v]setpts=PTS-STARTPTS[v1];[2:v]setpts=PTS-STARTPTS[v2];[v0][0:a][v1][1:a][v2][2:a]concat=n=3:v=1:a=1[outv][outa]" \
  -map "[outv]" -map "[outa]" \
  -c:v libx264 -preset ultrafast -crf 18 \
  -c:a aac -b:a 128k -fflags +genpts -movflags +faststart \
  output.mp4

⚠️ 必须用 libx264 -preset ultrafast,编码速度是 libx265 的 10~20 倍。 v2.0 filter 中显式 setpts=PTS-STARTPTS,彻底消除三个 segment 的时间戳不连续。

Phase 5:质量检查(scripts/check_durations.py)

处理后必须检查时长,标记异常视频。

检查逻辑:

  1. 获取片头时长 T_intro、片尾时长 T_outro
  2. 对每个处理后视频,获取 T_actual(实际时长)和 T_original(原视频时长)
  3. 异常判定:T_actual < T_original + T_intro + T_outro - 5(容差5秒)

⚠️ 验证规则:用 T_actual >= T_original + 5s 视为成功,不要用精确到秒的期望时长对比,精度误差会导致误判。

输出:异常视频列表.csv(含课程名、文件名、期望时长、实际时长、差异)

Phase 6:异常修复(scripts/fix_abnormal.py)

三阶递进修复策略(v2.0 升级):

策略 A(最轻量,~5 秒/视频):修复原视频时间戳 + concat copy

# 修复原视频
ffmpeg -i original.mp4 -c copy \
  -fflags +genpts+igndts \
  -bsf:v filter_units=remove_types=6 \
  -map_metadata -1 -video_track_timescale 90000 \
  -movflags +faststart \
  original_tsfix.mp4
# 用修复后的文件做 concat+copy

策略 B(中度,~8 秒/视频):同时修复片头片尾 + 原视频时间戳 + concat copy

# 修复片头片尾时间戳
ffmpeg -i intro.mp4 -c copy -fflags +genpts+igndts \
  -map_metadata -1 -video_track_timescale 90000 -movflags +faststart \
  intro_tsfix.mp4
# 同理修复片尾,然后 concat+copy

策略 C(保底,慢):concat filter + libx264 ultrafast(适用于所有视频)

⚠️ 4K 视频避免重编码,优先用策略 A/B。

Phase 7:生成统计报告(scripts/gen_report.py)

用 openpyxl 生成 Excel,包含 2 个 sheet:

Sheet 1:按课程统计(首 sheet,表头:课程、主讲老师、时长(分钟)、时长(小时)、视频数量)

Sheet 2:视频时长统计(明细,表头:序号、课程名、文件名、相对路径、时长(秒)、时长(分钟))

  • 课程名:取 -高等学历继续教育 之前的部分,去掉末尾"课程"二字
  • 主讲老师:取最后一个括号 (老师名) 中的内容

视频合并后卡住问题:完整诊断与预防

v2.0 新增章节。以下梳理了所有可能导致 concat 合并后视频播放卡住的根因,以及对应的预防和修复策略。

根因分类

A. 时间戳相关(6 类)

# 根因 现象 预防措施 修复方法
A1 非单调 PTS/DTS 播放到拼接点画面静止,音频继续 -fflags +genpts+igndts(不仅 genpts,要加 igndts) 策略 A:重写时间戳 + copy
A2 timebase 不一致 不同 segment 的时间戳数量级不同(如 1/12800 vs 1/90000),拼接后 PTS 跳变 -video_track_timescale 90000 强制统一 同上
A3 GOP 边界非 IDR 拼接点第一帧不是 IDR 关键帧,解码器无法开始解码,画面静止直到下一个 IDR 标准化时 -force_key_frames "expr:eq(n,0)" 确保首帧为 IDR 策略 C:重编码
A4 SPS/PPS/VPS 参数集突变 硬件解码器无法热切换参数集,直接卡死 标准化时匹配 profile/level/pix_fmt 策略 C:重编码
A5 SEI 消息冲突 pic_timing SEI 携带过期 HRD 参数,解码器缓冲计算出错 -bsf:v filter_units=remove_types=6 剥离 SEI 策略 A 含 SEI 剥离
A6 MP4 edit list (elst) 错乱 moov atom 中的 edit list 指向错误偏移,播放器时间线错位 -map_metadata -1 清除旧元数据 策略 A/B 含元数据清除

B. 非时间戳相关(5 类)

# 根因 现象 预防措施 修复方法
B1 音频编码参数不匹配 音频/视频不同步,播放器反复尝试 resync → 卡顿 AAC profile 统一为 LC-AAC,采样率/声道数匹配 策略 C:重编码
B2 容器层级 duration 不一致 mvhd/tkhd/mdhd 三处 duration 冲突,QuickTime/Safari 在边界暂停 -movflags +faststart 重写 moov 策略 A 含 moov 重写
B3 色彩空间突变 拼接点处 color_primaries/trc/space 切换,iOS/Android 播放器重建渲染管线 → 黑屏/卡顿 标准化时匹配色彩空间参数 策略 C:重编码
B4 末帧缺少 End-of-Sequence 中间段的末尾被误判为流结束,解码器提前终止 concat filter 重编码(写入完整流) 策略 C:重编码
B5 外部存储 IO 瓶颈 高码率视频从外部 SSD 读取+写入同一块盘,缓冲区溢出导致 bit corruption concat 临时文件写入系统临时目录(macOS: /tmp, Windows: %TEMP%) 脚本逻辑已处理

预防策略总览

┌─ 预处理阶段 ──────────────────────────────────────────┐
│                                                        │
│ 片头/片尾标准化后 → 时间戳修复:                         │
│   -fflags +genpts+igndts                               │
│   -map_metadata -1                                     │
│   -video_track_timescale 90000                         │
│   -bsf:v filter_units=remove_types=6 (剥离 SEI)       │
│                                                        │
│ 原视频处理前 → 时间戳修复: 同上                         │
│                                                        │
│ concat 拼接时 → 增强参数:                               │
│   -fflags +genpts+igndts  (非单调修复)                 │
│   -map_metadata -1         (elst 清除)                 │
│   -video_track_timescale 90000 (timebase 统一)         │
│                                                        │
└────────────────────────────────────────────────────────┘

修复策略选择指南

情况 推荐策略 预计耗时
时长正常但播放卡住 策略 A(修复时间戳 + copy) ~5 秒/视频
策略 A 失败 策略 B(同时修复片头片尾时间戳 + copy) ~8 秒/视频
A/B 均失败,或视频本身有问题 策略 C(concat filter 重编码) 视频时长 × 0.1~0.3
4K 视频 优先 A/B,避免 C -

常见问题速查

问题 原因 解决方案
合并后视频播放到拼接点卡住/静止 非单调 PTS/DTS、timebase 不一致、GOP 边界、SEI 冲突 v2.0:-fflags +genpts+igndts + -video_track_timescale 90000 + SEI 剥离 + 预处理时间戳
时长显示异常(远超实际) HEVC 视频 moov duration 错误 加 -fflags +genpts -movflags +faststart
片头片尾重复添加(时长×2) 脚本重跑时未跳过已处理文件 处理前检查目标文件是否存在且正常
concat+copy 失败率高 片头片尾编码与原视频不匹配 标准化时匹配原视频编码(H264→H264,HEVC→HEVC)
修复后时长仍异常 验证逻辑误判 用 >= 原视频+5s 验证,不用精确匹配
ffprobe 不可用 imageio_ffmpeg 不提供 ffprobe 用 ffmpeg -i file 2>&1 \| grep Duration 获取时长
4K 视频重编码极慢 用了 libx265 或 slow preset 优先用 -c copy -fflags +genpts+igndts 修复时间戳
外部 SSD 路径变化 硬编码路径 所有路径用变量,不要硬编码
外部 SSD 创建 .cache 失败 exFAT/NTFS 不兼容 dot-file 改用 _cache(下划线前缀)
get_video_info 分辨率解析为 0x31637661 正则误匹配 FourCC 代码 分开匹配:编码用 Video:\s*(\w+)、分辨率用 (\d{3,5})x(\d{3,5})、帧率用 (\d+(?:\.\d+)?) fps,不要用单一正则同时匹配所有字段
硬件解码器(iOS/Android/浏览器)卡死 SPS/PPS 参数集突变、色彩空间切换 标准化时匹配 profile/level/pix_fmt/colorspace;无法修复时用策略 C 重编码
音频播放但视频画面不动 GOP 边界非 IDR,解码器等下一个关键帧 标准化时 -force_key_frames "expr:eq(n,0)" 确保首帧为 IDR
Windows:找不到 ffmpeg 未安装 imageio_ffmpeg 或 PATH 中无 ffmpeg pip install imageio_ffmpeg,或设置环境变量 FFMPEG_PATH
Windows:路径含空格导致 ffmpeg 报错 Windows 路径可能含空格(如 C:\Users\My Name\...) 所有路径参数用引号包裹,或使用不含空格的路径

性能参考

操作 速度 说明
concat+copy(无重编码) ~10秒/视频 优先使用
时间戳修复(-c copy) ~5秒/视频 v2.0 新增:预处理步骤
concat+filter + libx264 ultrafast 视频时长 × 0.1~0.3 中等速度
concat+filter + libx265 视频时长 × 1~2 极慢,避免
ffmpeg 获取时长(只读 header) ~0.05秒/视频 快速扫描用

平台支持

操作系统 状态 说明
macOS (arm64/x86_64) ✅ 完全支持 imageio_ffmpeg 自动下载对应架构
Windows 10/11 (x86_64) ✅ v2.1 新增 自动检测 ffmpeg.exe,temp 目录自适应
Linux (x86_64) ✅ 支持 imageio_ffmpeg 或系统 ffmpeg

Windows 特别说明

  1. 安装依赖:pip install imageio_ffmpeg openpyxl(与 macOS 相同)
  2. ffmpeg 自动检测:脚本优先使用 imageio_ffmpeg,无需手动安装 ffmpeg
  3. 路径格式:使用正斜杠 / 或 pathlib.Path 处理路径,脚本自动兼容
  4. 临时目录:自动使用 %TEMP%\batch_intro_outro\,不再硬编码 /tmp
  5. 文件权限:确保输出目录有写入权限
  6. 长路径支持:Windows 长路径(>260 字符)需在注册表启用 LongPathsEnabled

脚本说明

本 skill 的 scripts/ 目录包含以下可执行脚本:

脚本 用途
batch_process.py 主脚本:批量添加片头片尾(含时间戳预处理)
check_durations.py 检查处理后视频时长是否异常
fix_abnormal.py 修复异常视频(三阶递进:A→B→C)
gen_report.py 生成 Excel 统计报告(含按课程汇总)

这些脚本需要根据实际路径和目录结构进行少量调整后再运行。核心逻辑已封装,只需修改变量(如 INTRO_FILE、OUTRO_FILE、BASE_DIR)。 v2.1:ffmpeg 路径改为自动检测(imageio_ffmpeg → 环境变量 → 系统 PATH),Windows/macOS 均可直接运行。


输出文件清单

输出目录/
├── 课程A/
│   └── 第1课时.mp4
├── 课程B/
│   └── ...
├── _cache/                  # 片头片尾标准化缓存
├── _tsfix_cache/            # v2.0:原视频时间戳修复缓存
├── 处理日志.csv
├── 修复日志_v*.csv
├── 异常视频列表_全面检查.csv
└── 视频时长统计_最新.xlsx

注意事项

  1. Python 输出缓冲:用 python3 -u script.py 或 print(..., flush=True) 实时查看进度
  2. 后台任务超时:长视频处理用 run_in_background=true,设置 timeout=600000
  3. Excel 正则提取课程名:^(.+?)-高等学历继续教育 → 去掉末尾"课程"二字
  4. 处理前检查目标文件是否已存在:避免重复处理导致片头片尾被添加多次
  5. 嵌套子文件夹重命名:用 os.walk() 递归扫描,不要只用 iterdir() 遍历根目录,否则子文件夹中的视频不会被重命名
  6. 临时文件放系统临时目录:v2.1 改为 tempfile.gettempdir()(macOS=/tmp, Windows=%TEMP%),避免外部 SSD 的 IO 瓶颈和 exFAT/NTFS 的 dot-file 兼容问题
  7. v2.1 跨平台:所有脚本使用 pathlib.Path 处理路径,tempfile.gettempdir() 管理临时目录,ffmpeg 自动检测

补充:批量重命名(添加课程简称前缀)

如需要为所有视频文件名添加课程简称前缀(格式 简称-原名.mp4),注意以下事项:

  • 必须递归扫描:使用 os.walk() 而非 iterdir(),确保嵌套子文件夹中的文件也能被处理
  • 简称前缀格式:全大写字母 + -,如 LDX-第10课时.mp4
  • 幂等性:检查文件名是否已有前缀(^[A-Z]+-),避免重复添加
  • 常见陷阱:课程目录下可能有深度嵌套的子文件夹(如 跨文化交际/跨文化第8课/),忘记递归将遗漏这些视频

Current listing version

2.1.0

Verify the source revision ↗

Safety and assessment details

Quality assessment

Out of 100 · Assessment report from SkillPi LAB.

Scenario Value92/100

Highlights

  • 提供完整的批量视频片头片尾添加工作流,支持数百至数千视频处理
  • 跨平台支持(Windows/macOS/Linux),自动检测 ffmpeg 路径
  • 包含异常修复、时长统计和 Excel 报告生成,显著提升用户效率
Trigger Precision95/100

Highlights

  • 触发词覆盖全面且精准('批量添加片头片尾'、'课程视频加片头片尾'等)
  • 边界清晰,无与其他 Skill 重叠风险
Structure and Standards94/100

Areas to improve

  • 缺少显式 When to Use 章节 Reported severity: Minor

    文档未设置独立的 'When to Use' 章节,但触发条件和适用场景已在概述和触发条件中充分说明

    Recommendation: 建议增加显式 'When to Use' 章节以提升可读性

Highlights

  • Phase 划分清晰(7个阶段),每阶段有明确步骤和出口条件
  • 包含端到端示例和完整 frontmatter,文档结构完整
Execution Reliability93/100

Highlights

  • 工具来源明确(ffmpeg 自动检测),参数正确
  • 覆盖主要异常(时间戳、编码、色彩空间等),修复策略递进(A→B→C)
  • 不可逆操作有确认机制(检查目标文件是否存在)
Evidence Verifiability (mean of the first four)94/100

The arithmetic mean of the first four original scores, rounded to a whole number.

Static text checks

100 / 100 · skillpi-20261123-no-r3-v1

No current rules matched the text.

This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.

Permissions and checks before use

Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.

  • Install or fetch external dependencies

    Use official sources and pin versions. Check install scripts in an isolated environment. This document check does not cover code downloaded later.

    Mentioned in main document · lines 300, 327

These notices grant no permissions. Your host tool must still confirm file changes, secrets, external transfers or costs as needed.

Security rating and access scope

safe · Static evidence review; not a runtime safety guarantee.

Translation pending; see source documentation.

Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.