Advertisement
Big_Jim

Video merger

Feb 8th, 2024 (edited)
1,761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;This script combines all videos in a folder into a single video file, fixing resolution differences by
  2. ;  making the width and height of the resulting video the largest width and height of the consituent videos.
  3. ;
  4. ;Requires all videos use the same file extension and have a two digit serialized suffix. This allows the script
  5. ;  to group together videos i.e. "abc 01.mp4|abc 02.mp4|abc 03.mp4" from "def 01.mp4|def 02.mp4"
  6. ;
  7. ;Also requires this function (https://www.autohotkey.com/boards/viewtopic.php?style=7&t=3806) as well as
  8. ;  ffmpeg to be added to your path variable.
  9. ;
  10. ;Final output is rendered into a subfolder, and original files are not deleted.
  11.  
  12. #NoEnv
  13. #include FileGetProperties.ahk
  14. SetWorkingDir %A_ScriptDir%
  15.  
  16. aWidth := []
  17. aHeight := []
  18. aName := []
  19. FileSelectFolder, vWorkingDirectory
  20. if ErrorLevel
  21.     ExitApp
  22.  
  23. FileCreateDir, %vWorkingDirectory%\Concatenated Videos
  24.  
  25. vFileList := ""
  26. Loop, Files, %vWorkingDirectory%\*
  27.     vFileList .= A_LoopFileLongPath "`n"
  28. Sort, vFileList
  29.  
  30. Loop, parse, vFileList, `n
  31. {
  32.     if (((aName.Length() > 1) and (InStr(A_LoopField, "01." . vExtension))) or (A_LoopField = ""))
  33.     {
  34.         vMaxWidth := Max(aWidth*)
  35.         vMaxHeight := Max(aHeight*)
  36.         GoSub, FFMPEG
  37.         if (A_LoopField = "")
  38.         {
  39.             FileDelete, %vWorkingDirectory%\ConcatList.txt
  40.             Break
  41.         }
  42.         aWidth := []
  43.         aHeight := []
  44.         aName := []
  45.     }
  46.     aWidth.Push(FGP_Value(A_LoopField,316))
  47.     aHeight.Push(FGP_Value(A_LoopField,314))
  48.     aName.Push(A_LoopField)
  49.     SplitPath, A_LoopField,vFileName,,vExtension
  50. }
  51.  
  52. ExitApp
  53.  
  54. FFMPEG:
  55.  
  56. FileDelete, %vWorkingDirectory%\ConcatList.txt
  57. Loop
  58. {
  59.     vPadX := Format("{:d}",((vMaxWidth - aWidth[A_Index])/2))
  60.     vPadY := Format("{:d}",((vMaxHeight - aHeight[A_Index])/2))
  61.     vConcatName := vWorkingDirectory . "\Concatenated Videos\" . SubStr(vFileName, 1, -7) . "." . vExtension
  62.     vTempName1 := aName[A_Index]
  63.     vTempName2 := SubStr(aName[A_Index], 1, -4) . "b." . vExtension
  64.     FileAppend, file '%vTempName2%'`n, %vWorkingDirectory%\ConcatList.txt
  65.     runwait, ffmpeg -i "%vTempName1%" -filter_complex "[0]pad=w=%vMaxWidth%:h=%vMaxHeight%:x=%vPadX%:y=%vPadY%:color=black" "%vTempName2%"
  66.     if (A_Index = aName.Length())
  67.         Break
  68. }
  69. Sleep 500
  70. runwait, ffmpeg -f concat -safe 0 -i "%vWorkingDirectory%\ConcatList.txt" -c copy "%vConcatName%"
  71. FileDelete, %vWorkingDirectory%\*b.%vExtension%
  72. Return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement