Board logo

标题: [日期时间] 【已解决】批处理如何将获得的视频时间变量按比例9等份? [打印本页]

作者: thp2008    时间: 2021-9-9 12:37     标题: 【已解决】批处理如何将获得的视频时间变量按比例9等份?

本帖最后由 thp2008 于 2021-9-9 22:39 编辑

例如:我想将D:\Movie 目录下的,所有*.mp4电影文件,通过批处理,自动生成9宫格的预览图。

首先,我要获取视频的时长
  1. @echo off
  2. for /f "tokens=2 delims=, " %%a in ('ffmpeg -i "1.mp4" 2^>^&1 ^| find "Duration:"') do (
  3.     set str=%%a
  4. )
  5. echo %str%
  6. pause
复制代码
对于获得的时间变量 %str%,我想将这个时间变量,按照10%,20%,30%,40%,50%,60%,70%,80%,90%的比例,进行9等份的时间变量。
这个步骤,我不知道用,批处理,该如何处理。

比如一个1.mp4  ,总时长是:00:50:00.00,50分钟,另外处理成变量的时候,把最后的毫秒去掉,只要 小时:分:秒
10%  00:05:00  设为变量 BianLiang10
20%  00:10:00  设为变量 BianLiang20
30%  00:15:00  设为变量 BianLiang30
40%  00:20:00  设为变量 BianLiang40
50%  00:25:00  设为变量 BianLiang50
60%  00:30:00  设为变量 BianLiang60
70%  00:35:00  设为变量 BianLiang70
80%  00:40:00  设为变量 BianLiang80
90%  00:45:00  设为变量 BianLiang90

截图:
ffmpeg.exe" -i "%%i" -ss %BianLiang10% -frames:v 1 "%%~dpni01.png"
ffmpeg.exe" -i "%%i" -ss %BianLiang20% -frames:v 1 "%%~dpni02.png"
ffmpeg.exe" -i "%%i" -ss %BianLiang30% -frames:v 1 "%%~dpni03.png"
ffmpeg.exe" -i "%%i" -ss %BianLiang40% -frames:v 1 "%%~dpni04.png"
ffmpeg.exe" -i "%%i" -ss %BianLiang50% -frames:v 1 "%%~dpni05.png"
ffmpeg.exe" -i "%%i" -ss %BianLiang60% -frames:v 1 "%%~dpni06.png"
ffmpeg.exe" -i "%%i" -ss %BianLiang70% -frames:v 1 "%%~dpni07.png"
ffmpeg.exe" -i "%%i" -ss %BianLiang80% -frames:v 1 "%%~dpni08.png"
ffmpeg.exe" -i "%%i" -ss %BianLiang90% -frames:v 1 "%%~dpni09.png"


代码:
@echo off

Rem 下面这两行for 语句,不知道如何写,单独是可以的。请大神我帮改一改,合并在一个批处理里面。
for /f "delims=" %%i in ('dir /b/s/a-d "%~dp0*.mp4"') do (

for /f "tokens=2 delims=, " %%a in ('ffmpeg -i "%%i" 2^>^&1 ^| find "Duration:"') do (
    set str=%%a

时间变量str,9等份。这个不会,还需要将时间变量的毫秒去掉,只要 小时:分:秒

)

rem 按时间比例截图9张
ffmpeg.exe" -i "%%i" -ss %BianLiang10% -frames:v 1 "%%~dpni01.png"
ffmpeg.exe" -i "%%i" -ss %BianLiang20% -frames:v 1 "%%~dpni02.png"
ffmpeg.exe" -i "%%i" -ss %BianLiang30% -frames:v 1 "%%~dpni03.png"
ffmpeg.exe" -i "%%i" -ss %BianLiang40% -frames:v 1 "%%~dpni04.png"
ffmpeg.exe" -i "%%i" -ss %BianLiang50% -frames:v 1 "%%~dpni05.png"
ffmpeg.exe" -i "%%i" -ss %BianLiang60% -frames:v 1 "%%~dpni06.png"
ffmpeg.exe" -i "%%i" -ss %BianLiang70% -frames:v 1 "%%~dpni07.png"
ffmpeg.exe" -i "%%i" -ss %BianLiang80% -frames:v 1 "%%~dpni08.png"
ffmpeg.exe" -i "%%i" -ss %BianLiang90% -frames:v 1 "%%~dpni09.png"

rem 合并九宫格
magick.exe" montage *.png -geometry +0+0 -background #00000000 9GongGe.jpg

最后,合并成九宫格就好了,合并九宫格,我知道如何处理,就是不知道,这个不同的时间变量,如何按比例九等份,并设为变量如何处理,感谢各位大神帮助!
作者: idwma    时间: 2021-9-9 15:57

本帖最后由 idwma 于 2021-9-9 16:01 编辑
  1. setlocal enabledelayedexpansion
  2. for /f "delims=" %%i in ('dir /b/s/a-d "%~dp0*.mp4"') do (
  3. for /f "tokens=2,3 delims=,: " %%a in ('ffmpeg -i "%%i" 2^>^&1 ^| find "Duration:"') do set /a str=(%%a*60+%%b)/9
  4. set BianLiang=!str!
  5. if 1==1 (
  6. :loop
  7. set /a n+=1
  8. ffmpeg.exe" -i "%%i" -ss 00:!BianLiang!:00 -frames:v 1 "%%~dpni0!n!.png"
  9. set /a BianLiang=!str!+!BianLiang!
  10. if not "!n!"=="9" goto :loop
  11. )
  12. set n=
  13. )
复制代码

作者: Batcher    时间: 2021-9-9 16:04

回复 2# idwma


    00:!BianLiang!:00 是不是没有考虑超过60分钟的视频?
作者: idwma    时间: 2021-9-9 17:03

本帖最后由 idwma 于 2021-9-9 17:13 编辑

回复 3# Batcher


    这样呀那改成用秒来算
  1. setlocal enabledelayedexpansion
  2. for /f "delims=" %%i in ('dir /b/s/a-d "%~dp0*.mp4"') do (
  3. for /f "tokens=2 delims=: " %%a in ('ffmpeg -i "%%i" 2^>^&1 ^| find "lasttimestamp"') do set /a str=%%a/10
  4. set BianLiang=!str!
  5. if 1==1 (
  6. :loop
  7. set /a n+=1
  8. ffmpeg.exe" -i "%%i" -ss !BianLiang! -frames:v 1 "%%~dpni0!n!.png"
  9. set /a BianLiang=!str!+!BianLiang!
  10. if not "!n!"=="9" goto :loop
  11. )
  12. set n=
  13. )
复制代码

作者: thp2008    时间: 2021-9-9 17:20

本帖最后由 thp2008 于 2021-9-9 17:37 编辑

回复 4# idwma


    你好,感谢您的帮助,代码写的真好,这么精简。
运行出错了,1,是第8行,ffmpeg.exe后面多了个双引号,我去掉了。
2,还是报错,好像是计算的问题,另外可能,是那个时间变量,是00:50:23:28   而ffmpeg.exe 只能使用 00:50:23  。请帮我把最后的毫秒去掉。
作者: thp2008    时间: 2021-9-9 17:23

H:\1>setlocal enabledelayedexpansion

H:\1>for /F "delims=" %i in ('dir /b/s/a-d "H:\1\*.mp4"') do (
for /F "tokens=2 delims=: " %a in ('ffmpeg -i "%i" 2>&1 | find "lasttimestamp"') do set /a str=%a/9
set BianLiang=!str!
if 1 == 1 (
set /a n+=1
ffmpeg.exe -i "%i" -ss !BianLiang! -frames:v 1 "%~dpni0!n!.png"
set /a BianLiang=!str!+!BianLiang!
if not "!n!" == "9" goto :loop
)
set n=
)

H:\1>(
for /F "tokens=2 delims=: " %a in ('ffmpeg -i "H:\1\232.mp4" 2>&1 | find "lasttimestamp"') do set /a str=%a/9
set BianLiang=!str!
if 1 == 1 (
set /a n+=1
ffmpeg.exe -i "H:\1\232.mp4" -ss !BianLiang! -frames:v 1 "H:\1\2320!n!.png"
set /a BianLiang=!str!+!BianLiang!
if not "!n!" == "9" goto :loop
)
set n=
)
ffmpeg version 2021-09-08-git-5e7e2e5031-full_build-www.gyan.dev Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 10.3.0 (Rev5, Built by MSYS2 project)
  configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libdav1d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-libglslang --enable-vulkan --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
  libavutil      57.  4.101 / 57.  4.101
  libavcodec     59.  7.102 / 59.  7.102
  libavformat    59.  5.100 / 59.  5.100
  libavdevice    59.  0.101 / 59.  0.101
  libavfilter     8.  7.101 /  8.  7.101
  libswscale      6.  1.100 /  6.  1.100
  libswresample   4.  0.100 /  4.  0.100
  libpostproc    56.  0.100 / 56.  0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'H:\1\232.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.35.100
  Duration: 00:10:20.48, start: 0.000000, bitrate: 1823 kb/s
  Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 1621 kb/s, 59.97 fps, 59.97 tbr, 383839 tbn (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
  Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 188 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> png (native))
Press [q] to stop, [?] for help
Output #0, image2, to 'H:\1\23201.png':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf59.5.100
  Stream #0:0(und): Video: png, rgb24, 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 59.97 fps, 59.97 tbn (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
      encoder         : Lavc59.7.102 png
frame=    0 fps=0.0 q=0.0 Lsize=N/A time=00:00:00.00 bitrate=N/A speed=   0x
video:0kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)
运算符不存在。

H:\1>set /a n+=1

H:\1>ffmpeg.exe -i "%i" -ss !BianLiang! -frames:v 1 "%~dpni0!n!.png"
ffmpeg version 2021-09-08-git-5e7e2e5031-full_build-www.gyan.dev Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 10.3.0 (Rev5, Built by MSYS2 project)
  configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libdav1d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-libglslang --enable-vulkan --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
  libavutil      57.  4.101 / 57.  4.101
  libavcodec     59.  7.102 / 59.  7.102
  libavformat    59.  5.100 / 59.  5.100
  libavdevice    59.  0.101 / 59.  0.101
  libavfilter     8.  7.101 /  8.  7.101
  libswscale      6.  1.100 /  6.  1.100
  libswresample   4.  0.100 /  4.  0.100
  libpostproc    56.  0.100 / 56.  0.100
%i: No such file or directory

H:\1>set /a BianLiang=!str!+!BianLiang!
运算符不存在。

H:\1>if not "!n!" == "9" goto :loop

H:\1>set /a n+=1

H:\1>ffmpeg.exe -i "%i" -ss !BianLiang! -frames:v 1 "%~dpni0!n!.png"
ffmpeg version 2021-09-08-git-5e7e2e5031-full_build-www.gyan.dev Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 10.3.0 (Rev5, Built by MSYS2 project)
  configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libdav1d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-libglslang --enable-vulkan --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
  libavutil      57.  4.101 / 57.  4.101
  libavcodec     59.  7.102 / 59.  7.102
  libavformat    59.  5.100 / 59.  5.100
  libavdevice    59.  0.101 / 59.  0.101
  libavfilter     8.  7.101 /  8.  7.101
  libswscale      6.  1.100 /  6.  1.100
  libswresample   4.  0.100 /  4.  0.100
  libpostproc    56.  0.100 / 56.  0.100
%i: No such file or directory

H:\1>set /a BianLiang=!str!+!BianLiang!
运算符不存在。
作者: idwma    时间: 2021-9-9 18:15

回复 6# thp2008
  1. setlocal enabledelayedexpansion
  2. for /f "delims=" %%i in ('dir /b/s/a-d "%~dp0*.mp4"') do (
  3. for /f "tokens=2 delims=: " %%a in ('ffmpeg -i "%%i" 2^>^&1 ^| find "lasttimestamp"') do set /a str=%%a/10
  4. set BianLiang=!str!
  5. if 1==1 (
  6. set file=%%i
  7. set out=%%~dpni
  8. :loop
  9. set /a n+=1
  10. ffmpeg -i "!file!" -ss !BianLiang! -frames:v 1 "!out!0!n!.png"
  11. set /a BianLiang=!str!+!BianLiang!
  12. if not "!n!"=="9" goto :loop
  13. )
  14. set n=
  15. )
复制代码

作者: thp2008    时间: 2021-9-9 18:23

本帖最后由 thp2008 于 2021-9-9 18:24 编辑

回复 7# idwma


    这次,可以生成了
1、生成的序号是从02--09,共8张,没有01。而且时间都是第一秒的,8张都是一样的。
2、第一个视频生成完,就结束了,退出了。后来的视频没有处理。

3、还是有这个提示:
H:\1>set /a BianLiang=!str!+!BianLiang!
运算符不存在。
作者: idwma    时间: 2021-9-9 18:29

回复 8# thp2008


    有的视频没有lasttimestamp信息,尴尬
作者: idwma    时间: 2021-9-9 19:04

本帖最后由 idwma 于 2021-9-9 20:45 编辑

回复 8# thp2008
  1. setlocal enabledelayedexpansion
  2. for /f "delims=" %%i in ('dir /b/s/a-d "*.mp4"') do (
  3. for /f "tokens=2,3,4 delims=:. " %%a in ('ffmpeg -i "%%i" 2^>^&1 ^| find "Duration:"') do (
  4. set hh=%%a
  5. set mm=%%b
  6. set ss=%%c
  7. )
  8. if "!hh:~0,1!"=="0" (set /a hh=!hh:~-1!*3600) else (set /a hh=!hh!*3600)
  9. if "!mm:~0,1!"=="0" (set /a mm=!mm:~-1!*60) else (set /a mm=!mm!*60)
  10. set /a str=!hh!+!mm!+!ss!
  11. set /a BianLiang=!str!/10
  12. set str=!BianLiang!
  13. for /l %%d in (1,1,9) do (
  14. ffmpeg -i "%%i" -ss !BianLiang! -frames:v 1 "%%~dpni0%%d.png"
  15. set /a BianLiang=!str!+!BianLiang!
  16. )
  17. )
复制代码

作者: thp2008    时间: 2021-9-9 21:31

回复 10# idwma

这次终于可以了,太感谢您的帮助了。

另外,我还有个小问题,就是我以前分开写的,合并后,就自动删除生成的9个截图,只保留合成后的大图。
我以前的代码是这么做的,你帮我改一下,看如何集成到您写的批处理中,万分感谢
  1. echo "%%~dpni01.png" >filelist.txt
  2. echo "%%~dpni02.png" >>filelist.txt
  3. echo "%%~dpni03.png" >>filelist.txt
  4. echo "%%~dpni04.png" >>filelist.txt
  5. echo "%%~dpni05.png" >>filelist.txt
  6. echo "%%~dpni06.png" >>filelist.txt
  7. echo "%%~dpni07.png" >>filelist.txt
  8. echo "%%~dpni08.png" >>filelist.txt
  9. echo "%%~dpni09.png" >>filelist.txt
  10. "%~dp0MagickFull\magick.exe" montage @filelist.txt -geometry +0+0 -background #00000000 "%%~dpni-YuLan.jpg"
  11. del filelist.txt /q
  12. del "%%~dpni01.png" /q
  13. del "%%~dpni02.png" /q
  14. del "%%~dpni03.png" /q
  15. del "%%~dpni04.png" /q
  16. del "%%~dpni05.png" /q
  17. del "%%~dpni06.png" /q
  18. del "%%~dpni07.png" /q
  19. del "%%~dpni08.png" /q
  20. del "%%~dpni09.png" /q
复制代码

作者: idwma    时间: 2021-9-9 21:40

回复 11# thp2008
  1. setlocal enabledelayedexpansion
  2. for /f "delims=" %%i in ('dir /b/s/a-d "*.mp4"') do (
  3. for /f "tokens=2,3,4 delims=:. " %%a in ('ffmpeg -i "%%i" 2^>^&1 ^| find "Duration:"') do (
  4. set hh=%%a
  5. set mm=%%b
  6. set ss=%%c
  7. )
  8. if "!hh:~0,1!"=="0" (set /a hh=!hh:~-1!*3600) else (set /a hh=!hh!*3600)
  9. if "!mm:~0,1!"=="0" (set /a mm=!mm:~-1!*60) else (set /a mm=!mm!*60)
  10. set /a str=!hh!+!mm!+!ss!
  11. set /a BianLiang=!str!/10
  12. set str=!BianLiang!
  13. for /l %%d in (1,1,9) do (
  14. ffmpeg -i "%%i" -ss !BianLiang! -frames:v 1 "%%~dpni0%%d.png"
  15. set /a BianLiang=!str!+!BianLiang!
  16. )
  17. echo "%%~dpni01.png" >filelist.txt
  18. echo "%%~dpni02.png" >>filelist.txt
  19. echo "%%~dpni03.png" >>filelist.txt
  20. echo "%%~dpni04.png" >>filelist.txt
  21. echo "%%~dpni05.png" >>filelist.txt
  22. echo "%%~dpni06.png" >>filelist.txt
  23. echo "%%~dpni07.png" >>filelist.txt
  24. echo "%%~dpni08.png" >>filelist.txt
  25. echo "%%~dpni09.png" >>filelist.txt
  26. "%~dp0MagickFull\magick.exe" montage @filelist.txt -geometry +0+0 -background #00000000 "%%~dpni-YuLan.jpg"
  27. del filelist.txt /q
  28. del "%%~dpni01.png" /q
  29. del "%%~dpni02.png" /q
  30. del "%%~dpni03.png" /q
  31. del "%%~dpni04.png" /q
  32. del "%%~dpni05.png" /q
  33. del "%%~dpni06.png" /q
  34. del "%%~dpni07.png" /q
  35. del "%%~dpni08.png" /q
  36. del "%%~dpni09.png" /q
  37. )
复制代码

作者: Batcher    时间: 2021-9-9 22:23

回复 12# idwma


他那两段可以简化一下
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. cd /d "%~dp0"
  4. for /f "delims=" %%i in ('dir /b /s /a-d "*.mp4"') do (
  5.     for /f "tokens=2,3,4 delims=:. " %%a in ('ffmpeg -i "%%i" 2^>^&1 ^| find "Duration:"') do (
  6.         set hh=%%a
  7.         set mm=%%b
  8.         set ss=%%c
  9.     )
  10.     if "!hh:~0,1!"=="0" (set /a hh=!hh:~-1!*3600) else (set /a hh=!hh!*3600)
  11.     if "!mm:~0,1!"=="0" (set /a mm=!mm:~-1!*60) else (set /a mm=!mm!*60)
  12.     set /a str=!hh!+!mm!+!ss!
  13.     set /a BianLiang=!str!/10
  14.     set str=!BianLiang!
  15.     for /l %%d in (1,1,9) do (
  16.         ffmpeg -i "%%i" -ss !BianLiang! -frames:v 1 "%%~dpni0%%d.png"
  17.         set /a BianLiang=!str!+!BianLiang!
  18.     )
  19.     (for /l %%x in (1,1,9) do (
  20.         echo "%%~dpni0%%x.png"
  21.     ))>"filelist.txt"
  22.     "%~dp0MagickFull\magick.exe" montage @filelist.txt -geometry +0+0 -background #00000000 "%%~dpni-YuLan.jpg"
  23.     del /q /f filelist.txt "%%~dpni0*.png"
  24. )
复制代码

作者: thp2008    时间: 2021-9-9 22:38

感谢两位大神的顶力相助,完美解决!

PS:小的视频,用这个批处理,还算比较快的,大的视频,就比较慢了,对CPU资源占用较高。至少批处理,是这可以实现自动化,预览图的。
作者: idwma    时间: 2021-9-9 23:33

回复 14# thp2008


    把时间放在前面会快很多
ffmpeg -ss !BianLiang! -i "%%i" -frames:v 1 "%%~dpni0%%d.png"
作者: thp2008    时间: 2021-9-9 23:42

回复 15# idwma

好的,谢谢,我试试
作者: thp2008    时间: 2021-9-9 23:59

本帖最后由 thp2008 于 2021-9-10 00:29 编辑

按着idwma的指导我修改了,速度很快,1分钟能生成好几部电影的九宫格预览图。但是有一点,不支持中文文件名,不是批处理不支持,是合并的magick.exe不支持,所以没有办法,把文件名改成英文或者数字,就可以了,生成完,再改回来就可以了。
思路1:如果能在代码中自动实现,首先将文件名记录下来,然后,改个纯字母或者纯数字的名字,生成完九宫格预览图,合成完后,删除掉生成的临时文件,再把文件改回成开始记录的原文件名。就完美的,解决了magick.exe不支持中文文件名的问题。
思路2:其实我在想,因为仅仅是magick.exe不支持中文文件名,我们在截图的时候,命名,就不要使用原文件名,而是纯英文或者数字的方式去命名,反正都是临时文件,合并后,我们再给合并后的预览图,改名成原视频的前缀名就好了,这样也可以解决这个问题,似乎更简单处理。
但是我不知道该如何写,大神们,有空的时候,可以研究一下。

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. cd /d "%~dp0"
  4. for /f "delims=" %%i in ('dir /b /s /a-d "*.mp4" "*.mkv"') do (
  5.     for /f "tokens=2,3,4 delims=:. " %%a in ('ffmpeg -i "%%i" 2^>^&1 ^| find "Duration:"') do (
  6.         set hh=%%a
  7.         set mm=%%b
  8.         set ss=%%c
  9.     )
  10.     if "!hh:~0,1!"=="0" (set /a hh=!hh:~-1!*3600) else (set /a hh=!hh!*3600)
  11.     if "!mm:~0,1!"=="0" (set /a mm=!mm:~-1!*60) else (set /a mm=!mm!*60)
  12.     set /a str=!hh!+!mm!+!ss!
  13.     set /a BianLiang=!str!/10
  14.     set str=!BianLiang!
  15.     for /l %%d in (1,1,9) do (
  16.         ffmpeg -ss !BianLiang! -i "%%i" -frames:v 1 "%%~dpni0%%d.jpg"
  17.         set /a BianLiang=!str!+!BianLiang!
  18.     )
  19.     (for /l %%x in (1,1,9) do (
  20.         echo "%%~dpni0%%x.jpg"
  21.     ))>"filelist.txt"
  22.     "%~dp0MagickFull\magick.exe" montage @filelist.txt -geometry +0+0 -background #00000000 "%%~dpni-YuLan.jpg"
  23.     del /q /f filelist.txt "%%~dpni0*.jpg"
  24. )
复制代码

作者: thp2008    时间: 2021-9-10 00:30

我自已研究了一下,按着上面第二种思路,解决了中文文件名的问题,已测试成功。
附上最终版本,给有需要的有缘人。
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. cd /d "%~dp0"
  4. for /f "delims=" %%i in ('dir /b /s /a-d "*.mp4" "*.mkv"') do (
  5.     for /f "tokens=2,3,4 delims=:. " %%a in ('ffmpeg -i "%%i" 2^>^&1 ^| find "Duration:"') do (
  6.         set hh=%%a
  7.         set mm=%%b
  8.         set ss=%%c
  9.     )
  10.     if "!hh:~0,1!"=="0" (set /a hh=!hh:~-1!*3600) else (set /a hh=!hh!*3600)
  11.     if "!mm:~0,1!"=="0" (set /a mm=!mm:~-1!*60) else (set /a mm=!mm!*60)
  12.     set /a str=!hh!+!mm!+!ss!
  13.     set /a BianLiang=!str!/10
  14.     set str=!BianLiang!
  15.     for /l %%d in (1,1,9) do (
  16.         ffmpeg -ss !BianLiang! -i "%%i" -frames:v 1 "TempABC0%%d.jpg"
  17.         set /a BianLiang=!str!+!BianLiang!
  18.     )
  19.     (for /l %%x in (1,1,9) do (
  20.         echo "TempABC0%%x.jpg"
  21.     ))>"filelist.txt"
  22.     "%~dp0MagickFull\magick.exe" montage @filelist.txt -geometry +0+0 -background #00000000 "Temp999-Full.jpg"
  23.      move /y "Temp999-Full.jpg" "%%~dpni-YuLan.jpg"
  24.     del /q /f filelist.txt "TempABC0*.jpg"
  25. )
复制代码

作者: thp2008    时间: 2021-9-10 12:30

回复 15# idwma

大神,我又遇到个问题,今天我放在实际目录中运行了,效果较好,速度很快,但是遇到个小问题。我发现有些文件名里有特殊字符,批处理会出错,但也没事。
我修改后,想重新运行,但是里面大部分已经生成完了,已存在,我想在这个批处理里面加上一句,运行前检测 "%%~dpni-YuLan.jpg"这个文件存不存在,如果存在的话就跳过,这样就不会重复了。感谢!
作者: idwma    时间: 2021-9-10 13:30

回复 19# thp2008
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. cd /d "%~dp0"
  4. for /f "delims=" %%i in ('dir /b /s /a-d "*.mp4" "*.mkv"') do (
  5. if not exist "%%~dpni-YuLan.jpg" (
  6.     for /f "tokens=2,3,4 delims=:. " %%a in ('ffmpeg -i "%%i" 2^>^&1 ^| find "Duration:"') do (
  7.         set hh=%%a
  8.         set mm=%%b
  9.         set ss=%%c
  10.     )
  11.     if "!hh:~0,1!"=="0" (set /a hh=!hh:~-1!*3600) else (set /a hh=!hh!*3600)
  12.     if "!mm:~0,1!"=="0" (set /a mm=!mm:~-1!*60) else (set /a mm=!mm!*60)
  13.     set /a str=!hh!+!mm!+!ss!
  14.     set /a BianLiang=!str!/10
  15.     set str=!BianLiang!
  16.     for /l %%d in (1,1,9) do (
  17.         ffmpeg -ss !BianLiang! -i "%%i" -frames:v 1 "TempABC0%%d.jpg"
  18.         set /a BianLiang=!str!+!BianLiang!
  19.     )
  20.     (for /l %%x in (1,1,9) do (
  21.         echo "TempABC0%%x.jpg"
  22.     ))>"filelist.txt"
  23.     "%~dp0MagickFull\magick.exe" montage @filelist.txt -geometry +0+0 -background #00000000 "Temp999-Full.jpg"
  24.      move /y "Temp999-Full.jpg" "%%~dpni-YuLan.jpg"
  25.     del /q /f filelist.txt "TempABC0*.jpg"
  26. )
  27. )
复制代码

作者: thp2008    时间: 2021-9-10 20:49

本帖最后由 thp2008 于 2021-9-10 21:23 编辑

回复 20# idwma


   再次表示感谢,实际环境运行发现,跳过一部分文件后,就退出,好像这样判断不完善。部分信息如下。

omaprint
  libavutil      57.  4.101 / 57.  4.101
  libavcodec     59.  7.102 / 59.  7.102
  libavformat    59.  5.100 / 59.  5.100
  libavdevice    59.  0.101 / 59.  0.101
  libavfilter     8.  7.101 /  8.  7.101
  libswscale      6.  1.100 /  6.  1.100
  libswresample   4.  0.100 /  4.  0.100
  libpostproc    56.  0.100 / 56.  0.100
Invalid duration specification for ss: -i
找不到操作数。
ffmpeg version 2021-09-08-git-5e7e2e5031-full_build-www.gyan.dev Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 10.3.0 (Rev5, Built by MSYS2 project)
  configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libdav1d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-libglslang --enable-vulkan --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
  libavutil      57.  4.101 / 57.  4.101
  libavcodec     59.  7.102 / 59.  7.102
  libavformat    59.  5.100 / 59.  5.100
  libavdevice    59.  0.101 / 59.  0.101
  libavfilter     8.  7.101 /  8.  7.101
  libswscale      6.  1.100 /  6.  1.100
  libswresample   4.  0.100 /  4.  0.100
  libpostproc    56.  0.100 / 56.  0.100
Invalid duration specification for ss: -i
找不到操作数。
montage: unable to open image 'TempABC01.jpg': No such file or directory @ error/blob.c/OpenBlob/3536.
montage: unable to open image 'TempABC02.jpg': No such file or directory @ error/blob.c/OpenBlob/3536.
montage: unable to open image 'TempABC02.jpg': No such file or directory @ error/blob.c/OpenBlob/3536.
montage: unable to open image 'TempABC03.jpg': No such file or directory @ error/blob.c/OpenBlob/3536.
montage: unable to open image 'TempABC03.jpg': No such file or directory @ error/blob.c/OpenBlob/3536.
montage: unable to open image 'TempABC04.jpg': No such file or directory @ error/blob.c/OpenBlob/3536.
montage: unable to open image 'TempABC04.jpg': No such file or directory @ error/blob.c/OpenBlob/3536.
montage: unable to open image 'TempABC05.jpg': No such file or directory @ error/blob.c/OpenBlob/3536.
montage: unable to open image 'TempABC05.jpg': No such file or directory @ error/blob.c/OpenBlob/3536.
montage: unable to open image 'TempABC06.jpg': No such file or directory @ error/blob.c/OpenBlob/3536.
montage: unable to open image 'TempABC06.jpg': No such file or directory @ error/blob.c/OpenBlob/3536.
montage: unable to open image 'TempABC07.jpg': No such file or directory @ error/blob.c/OpenBlob/3536.
montage: unable to open image 'TempABC07.jpg': No such file or directory @ error/blob.c/OpenBlob/3536.
montage: unable to open image 'TempABC08.jpg': No such file or directory @ error/blob.c/OpenBlob/3536.
montage: unable to open image 'TempABC08.jpg': No such file or directory @ error/blob.c/OpenBlob/3536.
montage: unable to open image 'TempABC09.jpg': No such file or directory @ error/blob.c/OpenBlob/3536.
montage: unable to open image 'TempABC09.jpg': No such file or directory @ error/blob.c/OpenBlob/3536.
montage:  `Temp999-Full.jpg' @ error/montage.c/MontageImageCommand/1806.
系统找不到指定的文件。




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2