【问题标题】:For loop is slow, any way to speed it up in a batch file?For循环很慢,有什么办法可以在批处理文件中加快它的速度?
【发布时间】:2020-10-11 00:27:15
【问题描述】:

我正在使用批处理文件生成 html 供个人使用,我的一个部分是获取文件夹中所有 mp4 文件的宽度和高度。这个“mp4 库”可以将文件换出、重命名、添加或提取等...所以我不想在每次更新时手动输入宽度和高度。

下面的代码可以运行,但是速度很慢...有没有办法使用批处理文件使其更快?

for /f %%g in ('dir /B /S *.mp4') do (

    set "g=%%g"
    set "g=!g:\=/!"
    
    for /f "tokens=1 delims=x" %%a in ('ffprobe -v error -select_streams v:0 -show_entries stream^=width^,height -of csv^=s^=x:p^=0 %%g') do set sW=%%a
    for /f "tokens=2 delims=x" %%b in ('ffprobe -v error -select_streams v:0 -show_entries stream^=width^,height -of csv^=s^=x:p^=0 %%g') do set sH=%%b
    
    echo %TAB% %TAB% ^<span^>^ ^<a style="text-decoration:none" class="image" href="file:///!g!" target="_blank"^>^ ^<video width="!sW!" height="!sH!" poster="file:///S:/_Stuff/_Data/_o/6.gif" poster="file:///S:/_Stuff/_Data/_o/6.gif" preload="auto" muted autoplay loop^>^ ^<source src="file:///!g!" type="video/mp4"^>^ ^</video^>^ ^</a^>^ ^</span^>^ >> _01.html 2>nul  
)

**更新:我试过这个并且它有效,需要 4 秒和 11 秒,但我刚刚注意到一个新的答案,所以我现在将尝试这个建议!

@echo off
setlocal EnableDelayedExpansion

>_output_test.txt (for /f %%g in ('dir /B /S *.mp4') do (

    set "g=%%g"
    set "g=!g:\=/!"
    REM @echo !g!

    for /f "tokens=1,2 delims=x" %%a in ('ffprobe -v error -select_streams v:0 -show_entries stream^=width^,height -of csv^=s^=x:p^=0 %%g') do (
        set sW=%%a 
        set sH=%%b
    )
    REM @echo !sW!
    REM @echo !sH!
    @echo %TAB% %TAB% ^<span^>^ ^<a style="text-decoration:none" class="image" href="file:///!g!" target="_blank"^>^ ^<video width="!sW!" height="!sH!" poster="file:///S:/_DaveStuff/_Data/_o/6.gif" preload="auto" muted autoplay loop^>^ ^<source src="file:///!g!" type="video/mp4"^>^ ^</video^>^ ^</a^>^ ^</span^>^  
    )
)

endlocal
pause
exit

【问题讨论】:

  • 1. FOR /R 2. 合并两个FOR 循环 3. 将整个FOR 循环括起来并重定向它,只打开一次文件
  • 请注意,要求提高速度的请求(没有显示您自己这样做的尝试)本质上是偏离主题的代码请求。因此,我建议您尝试实施迄今为止给出的建议,我们不是来为您做这一切的。
  • 感谢您的回复。我想我理解“组合”部分,但是重定向它是什么意思?
  • @Compo 对不起,我在这里搜索了很多选项,我会继续自己尝试。我可以发布我已经尝试过的内容吗?
  • 目前,对于外部for /f 循环的每次迭代,您都将输出重定向到_01.html。建议只重定向一次。当您将输出重定向到文件时,它会打开文件,写入文件,然后再次关闭它。对每个.mp4 文件执行此操作比重定向整个for /f 输出要多得多。如果您现在创建的内容需要进一步的帮助,请使用更新的代码更新您的问题。但请准确说明代码的哪一部分需要它,您的问题实际上应该仅限于一个特定的问题,而不是全部。

标签: batch-file ffprobe


【解决方案1】:

您在不同的for /F 循环中执行相同的ffprobe 命令行两次,这当然是低效的。此外,您将每一行单独写入输出文件,因此会发生许多单独的文件 I/O 操作。而且for /Fdir /B /S(应该是dir /B /S /A:-D-H-S)在开始迭代之前首先构建了完整的item列表,这里不需要,因为迭代的item没有改变;所以 for /R 循环在这里会更快,因为它缓存了一些项目然后开始迭代。

所以这里有一个可能的改进:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Redirect to the output files once only (`>` to overwrite, `>>` to append):
>> "_01.html" 2> nul (
    rem /* `for /F` + `dir /B /S` first retrieves the whole list of items before iterating,
    rem    but `for /R` begins to iterate earlier: */
    for /R %%F in (*.mp4) do (
        rem // Store currently iterated file path:
        set "FILE=%%F"
        rem // Get values from `ffprobe` program:
        for /F "tokens=1,2 delims=x" %%a in ('ffprobe -v error -select_streams v:0 -show_entries stream^=width^,height -of csv^=s^=x:p^=0 "%%F"') do set "sW=%%a" & set "sH=%%b"
        rem // Toggle delayed expansion to avoid troubles with `!`:
        setlocal EnableDelayedExpansion
        rem // Replace `\` by `/` in file path:
        set "FILE=!FILE:\=/!"
        rem // Return HTML string (with unnecessary escaping avoided):
        echo %TAB% %TAB% ^<span^> ^<a style="text-decoration:none" class="image" href="file:///!FILE!" target="_blank"^> ^<video width="!sW!" height="!sH!" poster="file:///S:/_Stuff/_Data/_o/6.gif" poster="file:///S:/_Stuff/_Data/_o/6.gif" preload="auto" muted autoplay loop^> ^<source src="file:///!FILE!" type="video/mp4"^> ^</video^> ^</a^> ^</span^> 
        endlocal
    )
)
endlocal

如果迭代的文件路径都不包含感叹号,您可以删除for /R循环内的setlocal/endlocal对,并将开头的DisableDelayedExpansion更改为EnableDelayedExpansion,这可能会稍微进一步提高性能,但几乎不明显。

【讨论】:

    猜你喜欢
    • 2012-03-21
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    相关资源
    最近更新 更多