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. FFMPEG.EXE -f lavfi -i anullsrc=channel_layout=%Channels%:sample_rate=%SampleRate% -t 64 silence_64sec.mp3
  26. IF ERRORLEVEL 1 SET RC=1
  27. >  %~n0.txt echo file '%~s1'
  28. >> %~n0.txt echo file 'silence_%~2sec.mp3'
  29. FFMPEG.EXE -f concat -i %~n0.txt -codec copy "%~n1_%~2sec_silence.mp3"
  30. IF ERRORLEVEL 1 SET RC=1
  31.  
  32. :: Remove temporary files
  33. DEL silence_%~2sec.mp3
  34. DEL %~n0.txt
  35.  
  36. ENDLOCAL & EXIT /B %RC%
  37.  
  38.  
  39. :Syntax
  40. ECHO.
  41. ECHO AppendSilence.bat,  Version 1.00
  42. ECHO Use FFMPEG to append silence to an existing mp3 file
  43. ECHO.
  44. ECHO Usage:   AppendSilence.bat  mp3inputfile.mp3  seconds
  45. ECHO.
  46. ECHO Where:   mp3inputfile.mp3   is the mp3 file to which silence will be appended
  47. ECHO          seconds            is the number of seconds of silence to be appended
  48. ECHO.
  49. ECHO Notes:   The output file name will be {mp3inputfile}_{seconds}sec_silence.mp3.
  50. ECHO          Return code (ErrorLevel) will be 1 in case of errors, otherwise 0.
  51. ECHO.
  52. ECHO Credits: FFMPEG: https://ffmpeg.org/
  53. ECHO          Use FFMPEG to get information on input file:
  54. ECHO          Davide Piras, https://stackoverflow.com/a/7465173
  55. ECHO          Use FFMPEG to create and append silence:
  56. ECHO          Ilogan, https://superuser.com/a/579110
  57. ECHO.
  58. ECHO Written by Rob van der Woude
  59. ECHO https://www.robvanderwoude.com
  60.  
  61. EXIT /B 1
  62.  

page last modified: 2022-10-20