Thursday, September 4

ConvertAudioForYouTube.bat


REM Make a copy of this batch file before modifying.

@echo on
Setlocal enableDelayedExpansion

REM An MP3 audio file passed to this script will be converted into an MP4 file with randomly selected static images for uploading to YouTube. These are not true MP4 files, so they can only be played with VLC, but YouTube recognizes them, and converts them to their own formats in very high quality.

REM A 1280x1280 thumbnail file of the cover art for uploading to YouTube is also generated. Sometimes, these files are larger than 2 megabytes, which YouTube will not accept. When that happens, pass the cover file (bc###.png) to the Interate.bat script to generate successively smaller thumbnail files until one is created that is slightly smaller than 2 megabytes.

Set /a MinFileVal = 119
Set /a MaxFileVal = 159
Set /a MinDurationVal = 14
Set /a MaxDurationVal = 19
REM Set /a AudioFileDurationInSeconds = 9999

For /f %%a in ('C:\MediaInfo\MediaInfo.exe --Inform^=General^;%%Duration%% ^"%~1^"') Do Set /a AudioFileDurationInMilliseconds=%%a

Echo AudioFileDurationInMilliseconds = %AudioFileDurationInMilliseconds%

Set /a AudioFileDurationInSeconds = AudioFileDurationInMilliseconds / 1000

Echo AudioFileDurationInSeconds = %AudioFileDurationInSeconds%

Copy nul "C:\Users\livec\Downloads\Images.txt" > nul

Echo file 'F:\Podcast\%~n1.png' >> "C:\Users\livec\Downloads\Images.txt"

Echo duration 5 >> "C:\Users\livec\Downloads\Images.txt"

Echo Filling Images.txt file with timestamps.

Set /a RunningTimeInSeconds = 0
Set /a RndDurationVal = 0
Set /a MaxIterations = AudioFileDurationInSeconds / MinDurationVal

FOR /L %%i in (0,1,%MaxIterations%) do (
    If !RunningTimeInSeconds! GEQ !AudioFileDurationInSeconds! (
	GoTo :ImagesFileFilled
    )
    Set /a CountDown = AudioFileDurationInSeconds - RunningTimeInSeconds
    Title !CountDown!
    Set /a RndFileVal = "!RANDOM! * (!MaxFileVal! - !MinFileVal! + 1) / 32768 + !MinFileVal!"
    set "PaddedVal=0000000000!RndFileVal!"
    set "FormattedVal=bc!PaddedVal:~-3!.png"
    echo file 'F:\Podcast\!FormattedVal!' >> "C:\Users\livec\Downloads\Images.txt"
    Set /a RndDurationVal = "!RANDOM! * (!MaxDurationVal! - !MinDurationVal! + 1) / 32768 + !MinDurationVal!"
    Echo duration !RndDurationVal! >> "C:\Users\livec\Downloads\Images.txt"
    Set /a RunningTimeInSeconds = RunningTimeInSeconds + RndDurationVal 
    REM Echo "Running Seconds: !RunningTimeInSeconds!" >> "C:\Users\livec\Downloads\Images.txt"
)

:ImagesFileFilled

Title %0

REM Due to a quirk, the last image has to be specified twice - the 2nd time without any duration directive
echo file 'F:\Podcast\!FormattedVal!' >> "C:\Users\livec\Downloads\Images.txt"

Type "C:\Users\livec\Downloads\Images.txt"

c:\ffmpeg\ffmpeg.exe -y -i "%~d1%~p1%~n1.png" -vf scale=1280:1280 "%~d1%~p1%~n1_1280x1280.png"

c:\ffmpeg\ffmpeg.exe -safe 0 -y -f concat -i "C:\Users\livec\Downloads\Images.txt" -i "%~1" -i "F:\Podcast\%~n1.vtt" -c:s mov_text -metadata:s:s:0 language=eng -c:a copy -c:v copy -t 9999 -r 30  -movflags faststart "%~d1%~p1%~n1.mp4"

rundll32.exe cmdext.dll,MessageBeepStub
rundll32 user32.dll,MessageBeep

timeout /t 999

endlocal