【问题标题】:automatically rename subtitle file with video file batch script使用视频文件批处理脚本自动重命名字幕文件
【发布时间】:2018-05-30 19:07:06
【问题描述】:

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

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在每个文件中不一定相同,可以包含()

如果可能的话,我想要的是用相应的mkv 文件名用批处理脚本重命名srt 文件,但我不知道该怎么做。

一些真实的例子是:

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 上运行的东西...

【问题讨论】:

    标签: batch-file rename


    【解决方案1】:

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

    @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 命令行。

    阅读this answer了解有关命令SETLOCALENDLOCALWhere does GOTO :EOF return to?

    的详细信息

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2010-09-20
        • 1970-01-01
        • 2021-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多