有没有办法用 ffmpeg 按章节批量拆分文件,然后在 windows 中用 mkvmerge 重新组合?

Is there a way to batch split a file by chapter with ffmpeg and then reassemble with mkvmerge in windows?

所以我制作了一个批处理脚本,本来可以相对精确地 trim 将视频分成章节,而不必 运行 按关键帧,但是代码看起来很糟糕,我无法获取它遍历所有 mp4 文件,也不让 mkvmerge 在拆分文件后追加文件。代码在下面,但请注意这是我的第一次尝试。

@echo off
setlocal enableDelayedExpansion

REM CODE BELOW CREATES JSON FILES FOR ALL MP4 FILES WITHIN THE SAME DIRECTORY
ffprobe -v quiet -print_format json -show_chapters -loglevel error "01x01.mp4" > "01x01.json"

REM CODE BELOW SETS VARIABLES FROM EACH SPECIFIC JSON
FOR /F "delims=" %%i in ('jq .chapters[2].start ^< 01x01.json') DO SET /A start1=%%i
FOR /F "delims=" %%j in ('jq .chapters[2].end ^< 01x01.json') DO SET /A end1=%%j

FOR /F "delims=" %%k in ('jq .chapters[4].start ^< 01x01.json') DO SET /A start2=%%k
FOR /F "delims=" %%l in ('jq .chapters[4].end ^< 01x01.json') DO SET /A end2=%%l

FOR /F "delims=" %%m in ('jq .chapters[6].start ^< 01x01.json') DO SET /A start3=%%m
FOR /F "delims=" %%n in ('jq .chapters[6].end ^< 01x01.json') DO SET /A end3=%%n

FOR /F "delims=" %%o in ('jq .chapters[8].start ^< 01x01.json') DO SET /A start4=%%o
FOR /F "delims=" %%p in ('jq .chapters[8].end ^< 01x01.json') DO SET /A end4=%%p

REM SETS THE DURATION OF EACH FILE TO USE PRECISION TIMING FOR START AND STOP TIMES
CALL vbs (%end1%-%start1%)/1000
SET duration1=%val%
CALL vbs (%end2%-%start2%)/1000
SET duration2=%val%
CALL vbs (%end3%-%start3%)/1000
SET duration3=%val%
CALL vbs (%end4%-%start4%)/1000
SET duration4=%val%

REM SETS THE START TIME IN SECONDS VS MILLISECONDS
CALL vbs (%start1%)/1000
SET start1=%val%
CALL vbs (%start2%)/1000
SET start2=%val%
CALL vbs (%start3%)/1000
SET start3=%val%
CALL vbs (%start4%)/1000
SET start4=%val%

REM TRIM AND SPLIT ORIGINAL FILE INTO SEPERATE SECTIONS BASED ON CHAPTER MARKERS
ffmpeg -ss %START1% -i 01x01.mp4 -ss 0 -c copy -to %DURATION1% -avoid_negative_ts make_zero 01x01-1.mp4
ffmpeg -ss %START2% -i 01x01.mp4 -ss 0 -c copy -to %DURATION2% -avoid_negative_ts make_zero 01x01-2.mp4
ffmpeg -ss %START3% -i 01x01.mp4 -ss 0 -c copy -to %DURATION3% -avoid_negative_ts make_zero 01x01-3.mp4
ffmpeg -ss %START4% -i 01x01.mp4 -ss 0 -c copy -to %DURATION4% -avoid_negative_ts make_zero 01x01-4.mp4

REM DELETES UNNEEDED JSON AFTER USE
del /s *.json

REM APPEND ALL MP4 FILES INTO COHESIVE MKV
for /d /r %%D in (*) do (
    pushd %%D
    set files=
    for %%F in (*.mp4) do set files=!files! + ^( "%%F" ^)
    if not "!files!"=="" %mkvmerge% -o "01x01-FINAL.mkv" !files:~2!
    popd
)

REM DELETE UNNEEDED MP4 ORIGINALS AND SPLIT FILES
del /s *.mp4

我知道它超长,每次我尝试使用变量或循环 运行 遍历所有文件时,它都无法读取 json 文件。我一整天都在做这个,我可以按原样使用脚本,但我必须为每次迭代制作一个文件。

我也希望它能够只提取标记为“视频”的章节,但我还没有完全弄清楚这一点。

如果有帮助,我将添加算术部分的 vbs 批处理文件以及示例 json。

@echo off
>"%temp%\VBS.vbs" echo Set fso = CreateObject("Scripting.FileSystemObject") : Wscript.echo (%*)
for /f "delims=" %%a in ('cscript /nologo "%temp%\VBS.vbs"') do set "val=%%a"
del "%temp%\VBS.vbs"
{
    "chapters": [
        {
            "id": 0,
            "time_base": "1/1000",
            "start": 0,
            "start_time": "0.000000",
            "end": 5590,
            "end_time": "5.590000",
            "tags": {
                "title": "Video"
            }
        },
        {
            "id": 1,
            "time_base": "1/1000",
            "start": 5590,
            "start_time": "5.590000",
            "end": 13994,
            "end_time": "13.994000",
            "tags": {
                "title": "Advertisement"
            }
        },
        {
            "id": 2,
            "time_base": "1/1000",
            "start": 13994,
            "start_time": "13.994000",
            "end": 163964,
            "end_time": "163.964000",
            "tags": {
                "title": "Video"
            }
        },
        {
            "id": 3,
            "time_base": "1/1000",
            "start": 163964,
            "start_time": "163.964000",
            "end": 195940,
            "end_time": "195.940000",
            "tags": {
                "title": "Advertisement"
            }
        },
        {
            "id": 4,
            "time_base": "1/1000",
            "start": 195940,
            "start_time": "195.940000",
            "end": 547849,
            "end_time": "547.849000",
            "tags": {
                "title": "Video"
            }
        },
        {
            "id": 5,
            "time_base": "1/1000",
            "start": 547849,
            "start_time": "547.849000",
            "end": 595850,
            "end_time": "595.850000",
            "tags": {
                "title": "Advertisement"
            }
        },
        {
            "id": 6,
            "time_base": "1/1000",
            "start": 595850,
            "start_time": "595.850000",
            "end": 1413588,
            "end_time": "1413.588000",
            "tags": {
                "title": "Video"
            }
        },
        {
            "id": 7,
            "time_base": "1/1000",
            "start": 1413588,
            "start_time": "1413.588000",
            "end": 1477569,
            "end_time": "1477.569000",
            "tags": {
                "title": "Advertisement"
            }
        },
        {
            "id": 8,
            "time_base": "1/1000",
            "start": 1477569,
            "start_time": "1477.569000",
            "end": 1529696,
            "end_time": "1529.696000",
            "tags": {
                "title": "Video"
            }
        }
    ]
}

我也试过使用 start_time 所以我不需要做额外的计算但是 jq 也不喜欢那样。

mkvmerge 甚至没有尝试 运行 当我把它放在这里时,我仍然需要在它的末尾缩短 7 秒,在它的前面缩短 12 秒,一旦它再次成为一个文件。

如有任何帮助,我们将不胜感激,我知道帮助很多,但我似乎遇到了障碍,或者此时睡眠不足。

更新

这太棒了我只需要弄清楚如何使用带空格的文件就可以了。我想我可以先 运行 一批,然后用下划线替换所有空格。这可能行得通,但如果可以的话,我不想更改文件名。

@echo off

for %%i in (*.mp4) do (
FOR /F "delims=" %%A IN ('ffprobe -v quiet -print_format json -show_chapters -loglevel error "%%i" ^| xidel - -se "$json/(chapters)()[id!=0 and tags/title='Video']/concat('ffmpeg -ss ',start div 1000,' -i %%i -to ',((end - start) div 1000),' -c copy -avoid_negative_ts make_zero %%~ni-',position(),'.mp4')"') DO %%A
FOR /F "delims=" %%A IN ('xidel -s --xquery "concat('mkvmerge -o &quot;%%~ni-FINAL.mkv&quot; &quot;',join(file:list(.,false(),'%%~ni-*.mp4'),'&quot; + &quot;'),'&quot;')"') DO %%A
)

如果我没理解错的话,你想删除“广告”章节。
这可能完全可以用 ffmpeg 完成,但如果没有源视频我不能肯定地说。

无论如何,让我在的帮助下为您提供一个非常优雅的拆分过程解决方案。

首先,您不必将 ffprobe 的输出保存到 JSON-file。您可以将其通过管道传输到 xidel:

ffprobe -v quiet -print_format json -show_chapters -loglevel error "01x01.mp4" | xidel - -se "$json"

Select 标记为“视频”的 objects/chapters:

ffprobe [...] | xidel - -se "$json/(chapters)()[id!=0 and tags/title='Video']"
{
  "id": 2,
  "time_base": "1/1000",
  "start": 13994,
  "start_time": "13.994000",
  "end": 163964,
  "end_time": "163.964000",
  "tags": {
    "title": "Video"
  }
}
[...]
{
  "id": 8,
  "time_base": "1/1000",
  "start": 1477569,
  "start_time": "1477.569000",
  "end": 1529696,
  "end_time": "1529.696000",
  "tags": {
    "title": "Video"
  }
}

(我不知道你为什么不想要第一章。如果你想要,那么就删除 id!=0 and。)

计算4个章节的时长:

ffprobe [...] | xidel - -se "$json/(chapters)()[id!=0 and tags/title='Video']/round-half-to-even(end_time - start_time,3)"
#or
ffprobe [...] | xidel - -se "$json/(chapters)()[id!=0 and tags/title='Video']/((end - start) div 1000)"
149.97
351.909
817.738
52.127

使用这些值和 start:

创建 ffmpeg strings/commands
ffprobe [...] | xidel - -se "$json/(chapters)()[id!=0 and tags/title='Video']/concat('ffmpeg -ss ',start div 1000,' -i 01x01.mp4 -to ',((end - start) div 1000),' -c copy -avoid_negative_ts make_zero 01x01-',position(),'.mp4')"
ffmpeg -ss 13.994 -i 01x01.mp4 -to 149.97 -c copy -avoid_negative_ts make_zero 01x01-1.mp4
ffmpeg -ss 195.94 -i 01x01.mp4 -to 351.909 -c copy -avoid_negative_ts make_zero 01x01-2.mp4
ffmpeg -ss 595.85 -i 01x01.mp4 -to 817.738 -c copy -avoid_negative_ts make_zero 01x01-3.mp4
ffmpeg -ss 1477.569 -i 01x01.mp4 -to 52.127 -c copy -avoid_negative_ts make_zero 01x01-4.mp4

美化command/query:

ffprobe [...] | xidel - -se ^"^
  $json/(chapters)()[id!=0 and tags/title='Video']/concat(^
    'ffmpeg -ss ',^
    start div 1000,^
    ' -i 01x01.mp4 -to ',^
    ((end - start) div 1000),^
    ' -c copy -avoid_negative_ts make_zero 01x01-',^
    position(),^
    '.mp4'^
  )^
"

要执行这些 ffmpeg 命令,只需将整个 ffprobe/xidel 命令放在 FOR 循环中:

FOR /F "delims=" %A IN ('ffprobe [...] ^| xidel - -se "$json/(chapters)()[id!=0 and tags/title='Video']/concat('ffmpeg -ss ',start div 1000,' -i 01x01.mp4 -to ',((end - start) div 1000),' -c copy -avoid_negative_ts make_zero 01x01-',position(),'.mp4')"') DO %A

(在 Batch-script 中使用 %%A 如果是 SETLOCAL ENABLEDELAYEDEXPANSION 转义感叹号; id^!=0)

考虑到 cmd 的所有缺点,我会再次使用 xidel 来创建 mkvmerge string/command:

xidel -s -e "file:list(.,false(),'01x01-*.mp4')"
01x01-1.mp4
01x01-2.mp4
01x01-3.mp4
01x01-4.mp4

xidel -s -e "join(file:list(.,false(),'01x01-*.mp4'),' + ')"
01x01-1.mp4 + 01x01-2.mp4 + 01x01-3.mp4 + 01x01-4.mp4

xidel -s -e "concat('\"',join(file:list(.,false(),'01x01-*.mp4'),'\" + \"'),'\"')"
#or
xidel -s --xquery "concat('&quot;',join(file:list(.,false(),'01x01-*.mp4'),'&quot; + &quot;'),'&quot;')"
"01x01-1.mp4" + "01x01-2.mp4" + "01x01-3.mp4" + "01x01-4.mp4"

xidel -s --xquery "concat('mkvmerge -o &quot;01x01-FINAL.mkv&quot; &quot;',join(file:list(.,false(),'01x01-*.mp4'),'&quot; + &quot;'),'&quot;')"
mkvmerge -o "01x01-FINAL.mkv" "01x01-1.mp4" + "01x01-2.mp4" + "01x01-3.mp4" + "01x01-4.mp4"

要再次执行 mkvmerge 命令,只需将整个 xidel 命令放入 FOR 循环中即可:

FOR /F "delims=" %A IN ('xidel -s --xquery "concat('mkvmerge -o &quot;01x01-FINAL.mkv&quot; &quot;',join(file:list(.,false(),'01x01-*.mp4'),'&quot; + &quot;'),'&quot;')"') DO %A

(为什么 --xquery &quot;?因为 \" 将在 FOR 循环中导致“escape-character-hell”

如果您只使用这两个 FOR 循环,这也意味着不再需要 SETLOCAL ENABLEDELAYEDEXPANSION