1. ::######################################
  2. :: Usage: ffmpeg_grid 8 8 160 90 "-c:v libx264 -an" output.mp4 input1.mp4 ...
  3. ::######################################
  4.  
  5. @echo off
  6. setlocal enabledelayedexpansion
  7.  
  8. :: Copy mandatory args into named vars
  9. set cols=%1
  10. set rows=%2
  11.  
  12. set w=%3
  13. set h=%4
  14.  
  15. set extra_args=%~5
  16. set output=%6
  17.  
  18. :: Load all remaining args into array 'inputs'
  19. for /l %%i in (1,1,6) do shift
  20. set i=0
  21. :args_loop
  22. if "%1"=="" goto args_done
  23. set inputs[%i%]=%1
  24. set /a i+=1
  25. shift
  26. goto args_loop
  27. :args_done
  28.  
  29. del %output% 2>nul
  30.  
  31. set /a w_total=%w% * %cols%
  32. set /a h_total=%h% * %rows%
  33. set /a max_x=%cols%-1
  34. set /a max_y=%rows%-1
  35. set /a cnt=%cols% * %rows%
  36. set ffmpeg_cmd=ffmpeg -hide_banner
  37.  
  38. del %tmp%\~ffscr1 2>nul
  39. del %tmp%\~ffscr2 2>nul
  40.  
  41. set i=0
  42.  
  43. set input=%1
  44.  
  45. :: Loop over all videos in grid
  46. for /l %%y in (0,1,%max_y%) do (
  47.     for /l %%x in (0,1,%max_x%) do (
  48.         call set test=%%inputs[!i!]%%
  49.         if not "!test!"=="" set input=!test!
  50.         set ffmpeg_cmd=!ffmpeg_cmd! -i !input!
  51.  
  52.         echo [!i!:v] setpts=PTS-STARTPTS, scale=%w%x%h% [%%x%%y];>>%tmp%\~ffscr1
  53.  
  54.         set /a j=!i!+1
  55.         set /a x=%%x * %w%
  56.         set /a y=%%y * %h%
  57.         echo [tmp!i!][%%x%%y] overlay=shortest=1:x=!x!:y=!y!>>%tmp%\~ffscr2
  58.        
  59.         set /a i=!i!+1
  60.         if !i! lss %cnt% echo [tmp!j!];>>%tmp%\~ffscr2
  61.     )
  62. )
  63.  
  64. :: Create filter_complex_script
  65. echo nullsrc=size=%w_total%x%h_total% [tmp0];>%tmp%\~ffscr
  66. type %tmp%\~ffscr1>>%tmp%\~ffscr
  67. type %tmp%\~ffscr2>>%tmp%\~ffscr
  68.  
  69. :: Run FFmpeg command
  70. !ffmpeg_cmd! -filter_complex_script %tmp%\~ffscr %extra_args% %output%
  71.  
  72. :: Clean up tmp files
  73. del %tmp%\~ffscr 2>nul
  74. del %tmp%\~ffscr1 2>nul
  75. del %tmp%\~ffscr2 2>nul
  76.  
  77. endlocal
  78.  
[raw code]