使用视频文件批处理脚本自动重命名字幕文件

时间:2017-12-17 01:38:22

标签: batch-file rename

我有带文件名的视频文件:

Series.Name.S01E01.andalotofstuff.mkv
Series.Name.S01E02.andalotofstuff.mkv
Series.Name.S02E01.andalotofstuff.mkv
etc.

其中andalotofstuff在每个文件中不一定相同,可以包括.-[]

我还有带文件名的字幕文件:

Series Name 1x01 - Episode Name andotherstuff.srt
Series Name 1x02 - Episode Name andotherstuff.srt
Series Name 2x01 - Episode Name andotherstuff.srt
etc.

其中andotherstuff在每个文件中不一定相同,可以包含()

我想要的是使用批处理脚本重命名具有相应srt文件名的mkv个文件,如果可能的话,我不知道如何。

一些真实的例子是:

Marvels.Agents.of.S.H.I.E.L.D.S05E04.720p.HDTV.x264-AVS[eztv].mkv
Marvel's Agents of S.H.I.E.L.D. 5x04 - A Life Earned (Español (Latinoamérica)).srt

The.Shannara.Chronicles.S02E08.720p.HDTV.x264-AVS[eztv].mkv
The Shannara Chronicles 2x08 - Amberle (Español (España)).srt

The.Shannara.Chronicles.S02E10.720p.HDTV.x264-KILLERS[eztv].mkv
The Shannara Chronicles 2x10 - Blood (English).srt

请注意,并非所有视频文件都有相应的字幕文件。

Here我找到了一些适用于Linux的东西......

2 个答案:

答案 0 :(得分:0)

以下是此任务的注释批处理文件:

@echo off

rem Setup a local environment for this batch file creating lots
rem of environment variables which should not exist anymore after
rem finishing batch file execution.

setlocal EnableExtensions DisableDelayedExpansion

rem Get all *.mkv and *.srt file names in current directory without path
rem and without file extension loaded into memory assigned to environment
rem variables with name mkv_1, mkv_2, ..., srt_1, srt_2, ... using the
rem two subroutines GetFileList and AddFileToList.

call :GetFileList mkv
call :GetFileList srt
goto ValidateFileCounts

rem Subroutine GetFileList runs command FOR which executes command DIR in
rem a separate command process in background which outputs all file names
rem with the file extension passed to the subroutine as first parameter
rem sorted by name. Each file name output by DIR is assigned to an environment
rem variable with passed file extension, an underscore and an incremented
rem number as variable name. The number of files with given file extension
rem is assigned to an environment variable with name FileCount_Extension.

:GetFileList
set "FileNumber=0"
set "FileExtension=%1"
for /F "delims=" %%I in ('dir *.%FileExtension% /A-D /B /ON 2^>nul') do call :AddFileToList "%%~nI"
set "FileCount_%FileExtension%=%FileNumber%"
goto :EOF

rem Subroutine AddFileToList increments current file number by one and then
rem assigns the file name without file extension and without path passed
rem from calling subroutine GetFileList as first and only parameter to
rem an environment variable with automatically generated varible name.

:AddFileToList
set /A FileNumber+=1
set "%FileExtension%_%FileNumber%=%~1"
goto :EOF

rem After creating the two file lists in memory it is validated that the
rem file rename operation can be started at all. There must be *.srt files
rem found in current directory and the number of *.mkv files must be equal
rem the number of *.srt files.

:ValidateFileCounts
if %FileCount_srt% == 0 (
    echo/
    echo There are no *.srt files in directory:
    echo/
    echo %CD%
    echo/
    pause
    goto EndBatch
)

if not %FileCount_mkv% == %FileCount_srt% (
    echo/
    echo Number of *.mkv files is not equal number of *.srt files in directory:
    echo/
    echo %CD%
    echo/
    pause
    goto EndBatch
)

rem Now delayed environment variable expansion is needed for the file rename
rem operation in a loop which could not be enabled at beginning of the batch
rem file in case of any file name contains one or more exclamation marks.

rem *.srt files having already the same file name as corresponding *.mkv
rem file detected by identical current and new file name are ignored for
rem the rename operation.

rem It is also not possible to rename a *.srt file to name of a *.srt which
rem already exists. In this case an error message is output for the *.srt
rem file which cannot be renamed and finally PAUSE is executed to give the
rem batch file user the possibility to read all those file rename errors.
rem But it is very unlikely that this error message is displayed ever.

setlocal EnableDelayedExpansion
set "RenameError="
for /L %%I in (1,1,%FileCount_srt%) do (
    set "FileNameNew=!mkv_%%I!.srt"
    set "FileNameNow=!srt_%%I!.srt"
    if not "!FileNameNew!" == "!FileNameNow!" (
        if not exist "!FileNameNew!" (
            ren "!FileNameNow!" "!FileNameNew!"
        ) else (
            echo Rename file !FileNameNow!
            echo to new name !FileNameNew!
            echo not possible as such a file exists already.
            echo/
            set "RenameError=1"
        )
    )
)
if defined RenameError pause
endlocal

rem The previous environment is restored finally which means all environment
rem variables created by this batch file are removed from memory. There is
rem neither a message output nor batch file processing halted if no error
rem occurred during entire process.

:EndBatch
endlocal

没有使用真正的文件名匹配算法,因为它看起来像算法是人类语言智能。因此,批处理文件只是将按名称排序的* .mkv文件名列表和按名称排序的* .srt文件名列表加载到内存中,然后将第一个* .srt文件重命名为第一个* .mkv文件的名称,第二个* .srt文件到第二个* .mkv文件的名称,依此类推。这个简单的解决方案适用于给定的示例。

可以将命令echo左侧插入命令ren并在批处理文件末尾附加命令pause,或者在命令提示符窗口中运行它以查看所有在真正重命名* .srt文件之前,文件重命名操作并没有真正用于用户验证。

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • call /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • rem /?
  • ren /?
  • setlocal /?

另请阅读有关Using Command Redirection Operators的Microsoft文章,了解2>nul的说明。重定向运算符>必须使用 FOR 命令行上的插入符^进行转义,以便在执行命令之前Windows命令解释程序处理此命令行时将其解释为文字字符FOR ,它在后台启动的单独命令进程中执行嵌入式dir命令行。

有关命令 SETLOCAL ENDLOCAL this answer

的详细信息,请阅读Where does GOTO :EOF return to?

答案 1 :(得分:0)

在Windows中,您可以使用名为“字幕和视频重命名器”的程序来完成此操作。我刚刚测试了0.5.1版,并点击了2个按钮,将重命名为30个字幕。