Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for appendsilence.bat

(view source code of appendsilence.bat as plain text)

  1. @ECHO OFF
  2. :: Check command line arguments
  3. IF "%~2"==""               GOTO Syntax
  4. IF NOT "%~3"==""           GOTO Syntax
  5. IF /I NOT "%~x1"==".mp3"   GOTO Syntax
  6. IF %2 LSS    1             GOTO Syntax
  7. IF %2 GTR 3600             GOTO Syntax
  8. FFMPEG.EXE -h >NUL 2>&1 || GOTO Syntax
  9.  
  10. SETLOCAL
  11. SET RC=1
  12.  
  13. :: FFMPEG command by Davide Piras
  14. :: https://stackoverflow.com/a/7465173
  15. FOR /F "tokens=5-7 delims=, " %%A IN ('FFMPEG.EXE -i "%~1" 2^>^&1') DO (
  16. 	IF "%%~B"=="Hz" (
  17. 		SET SampleRate=%%~A
  18. 		SET Channels=%%C
  19. 		SET RC=0
  20. 	)
  21. )
  22.  
  23. :: Technique explained by Ilogan
  24. :: https://superuser.com/a/579110
  25. :: -safe 0 option discussed at
  26. :: https://stackoverflow.com/a/38999363
  27. FFMPEG.EXE -f lavfi -i anullsrc=channel_layout=%Channels%:sample_rate=%SampleRate% -t %~2 silence_%~2sec.mp3
  28. IF ERRORLEVEL 1 SET RC=1
  29. :: Note: to PREPEND the silence, swap the order of files in the next 2 lines
  30. >  %~n0.txt echo file '%~s1'
  31. >> %~n0.txt echo file 'silence_%~2sec.mp3'
  32. FFMPEG.EXE -f concat -safe 0 -i %~n0.txt -codec copy "%~n1_%~2sec_silence.mp3"
  33. IF ERRORLEVEL 1 SET RC=1
  34.  
  35. :: Remove temporary files
  36. DEL silence_%~2sec.mp3
  37. DEL %~n0.txt
  38.  
  39. ENDLOCAL & EXIT /B %RC%
  40.  
  41.  
  42. :Syntax
  43. ECHO.
  44. ECHO AppendSilence.bat,  Version 1.02
  45. ECHO Use FFMPEG to append silence to an existing mp3 file
  46. ECHO.
  47. ECHO Usage:   AppendSilence.bat  mp3inputfile.mp3  seconds
  48. ECHO.
  49. ECHO Where:   mp3inputfile.mp3   is the mp3 file to which silence will be appended
  50. ECHO          seconds            is the number of seconds of silence to be appended
  51. ECHO.
  52. ECHO Notes:   The output file name will be {mp3inputfile}_{seconds}sec_silence.mp3.
  53. ECHO          Return code (ErrorLevel) will be 1 in case of errors, otherwise 0.
  54. ECHO.
  55. ECHO Credits: FFMPEG: https://ffmpeg.org/
  56. ECHO          Use FFMPEG to get information on input file:
  57. ECHO          Davide Piras, https://stackoverflow.com/a/7465173
  58. ECHO          Use FFMPEG to create and append silence:
  59. ECHO          Ilogan, https://superuser.com/a/579110
  60. ECHO          Avoid "concat: unsafe file name" error:
  61. ECHO          https://stackoverflow.com/a/38999363
  62. ECHO.
  63. ECHO Written by Rob van der Woude
  64. ECHO https://www.robvanderwoude.com
  65.  
  66. EXIT /B 1
  67.  

page last modified: 2024-04-16; loaded in 0.0186 seconds