Advertisement
Guest User

avs-sidecar-maker.bat

a guest
Apr 3rd, 2022
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 3.59 KB | None | 0 0
  1. @echo off
  2. chcp 65001
  3.  
  4. for %%i in (*.mkv *.mp4) do (
  5.     echo %%~nxi
  6.     call :write-avs "%%~nxi" "%%~nxi.avs"
  7. )
  8. pause & exit
  9.  
  10. :write-avs
  11.     (
  12.         echo path = "%~1" # 8.3 name is "%~snx1"
  13.         echo Assert^(FunctionExists^("FFVideoSource"^), "FFMS2 plugin is required"^)
  14.         echo Assert^(Exist^(path, utf8=true^), "'" + path + ^"^"^"' does not exist
  15.         echo  or .avs is not in UTF8-no-BOM
  16.         echo  or Unicode path is not supported by this AviSynth compatible program
  17.         echo    ^(AvsPmod issue - as a workaround, use 8.3 path^)^"^"^"^)
  18.         echo # mpv-related issue with Unicode .avs filename ^(mpv crash at launch^):
  19.         echo #  https://forum.doom9.org/showthread.php?p=1903673#post1903673
  20.         echo #  as a workaround, rename .avs
  21.         echo v = FFVideoSource^(path^)
  22.         echo try {
  23.         echo     a = FFAudioSource^(path^)
  24.         echo } catch^(err_msg^) { # if file has no audio track
  25.         echo     return v.Subtitle^(err_msg^)
  26.         echo }
  27.         echo a_length = a.AudioDuration
  28.         echo v_length = v.FrameCount/v.FrameRate
  29.         echo #                        v--- real clip length
  30.         echo # ^|----------a-----------^|
  31.         echo # ^|---v---^|---v---^|---v---^|
  32.         echo #                         ^^--- to be trimmed by AudioTrim^(0, a_length^)
  33.         echo v_repeat_count = Ceil^(a_length/v_length^)
  34.         echo v = v.Loop^(v_repeat_count^)
  35.         echo c = AudioDub^(v, a^).AudioTrim^(0, a_length^)
  36.         echo c_repeat_count = int^(10*60*60/a_length^) # 10 hours ^(even longer "-1" has
  37.         echo c = c.Loop^(c_repeat_count^)              #  compatibility issue with MPC-BE^)
  38.         echo return c
  39.     ) > "%~2"
  40.     exit /b
  41.  
  42. :: Drawbacks:
  43. ::  1) AviSynth+ & AviSynth FFMS2 plugin dependency
  44. ::  2) junk index files (.ffindex)
  45. ::  3) counter-intuitive .avs playback (not .mp4/.mkv)
  46. ::  4) AviSynth Unicode issues
  47.  
  48.  
  49. :: ============================ Readable AviSynth+ code ============================
  50. :: without escaping needed for .bat:   ^)   ^(   ^^
  51.  
  52. :: ### Naive solution with desync ###
  53. ::
  54. :: path = "___.mp4"
  55. :: a = FFAudioSource(path).Loop(-1)
  56. :: v = FFVideoSource(path).Loop(-1)
  57. :: c = AudioDub(v, a)
  58. :: # |----------a-----------|----------a-----------|--...
  59. :: # |---v---|---v---|---v---|---v---|---v---|---v---|...
  60. :: #                        ^--- desync
  61.  
  62. :: ### Final solution ###
  63. ::
  64. :: path = "___.mp4" # 8.3 name is "___~1.MP4"
  65. :: Assert(FunctionExists("FFVideoSource"), "FFMS2 plugin is required")
  66. :: Assert(Exist(path, utf8=true), "'" + path + """' does not exist
  67. ::  or .avs is not in UTF8-no-BOM
  68. ::  or Unicode path is not supported by this AviSynth compatible program
  69. ::  (AvsPmod issue - as a workaround, use 8.3 path)""")
  70. :: # mpv-related issue with Unicode .avs filename (mpv crash at launch):
  71. :: #  https://forum.doom9.org/showthread.php?p=1903673#post1903673
  72. :: #  as a workaround, rename .avs
  73. :: v = FFVideoSource(path)
  74. :: try {
  75. ::     a = FFAudioSource(path)
  76. :: } catch(err_msg) { # if file has no audio track
  77. ::     return v.Subtitle(err_msg)
  78. :: }
  79. :: a_length = a.AudioDuration
  80. :: v_length = v.FrameCount/v.FrameRate
  81. :: #                        v--- real clip length
  82. :: # |----------a-----------|
  83. :: # |---v---|---v---|---v---|
  84. :: #                         ^--- to be trimmed by AudioTrim(0, a_length)
  85. :: v_repeat_count = Ceil(a_length/v_length)
  86. :: v = v.Loop(v_repeat_count)
  87. :: c = AudioDub(v, a).AudioTrim(0, a_length)
  88. :: c_repeat_count = int(10*60*60/a_length) # 10 hours (even longer "-1" has
  89. :: c = c.Loop(c_repeat_count)              #  compatibility issue with MPC-BE)
  90. :: return c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement