Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @echo off
- setlocal enabledelayedexpansion
- :: Set the base path of the batch file
- set "BASE_DIR=%~dp0"
- set "INPUT_DIR=%BASE_DIR%original"
- set "OUTPUT_DIR=%BASE_DIR%lp2converted"
- set "LOG_FILE=%BASE_DIR%conversion_log.txt"
- echo Starting conversion process > "%LOG_FILE%"
- echo =========================== >> "%LOG_FILE%"
- for /f "usebackq delims=" %%F in (`powershell -NoProfile -Command ^
- "Get-ChildItem -File -Path '%INPUT_DIR%' -Recurse | Where-Object { $_.Extension -match '\.(flac|mp3|ogg|m4a|alac|aac|wav|wma|opus|aiff|ape)$' } | ForEach-Object { $_.FullName }"`) do (
- echo. >> "%LOG_FILE%"
- echo Processing: %%~nxF >> "%LOG_FILE%"
- echo --------------------------- >> "%LOG_FILE%"
- :: Extract metadata using ffprobe JSON
- set "TEMP_JSON=%TEMP%\meta_%%~nF.json"
- ffprobe -v quiet -print_format json -show_format "%%F" > "!TEMP_JSON!"
- :: Parse metadata using PowerShell
- for /f "usebackq delims=" %%a in (`powershell -NoProfile -Command ^
- "(Get-Content -Raw '!TEMP_JSON!' | ConvertFrom-Json).format.tags.artist"`) do set "ARTIST=%%a"
- for /f "usebackq delims=" %%a in (`powershell -NoProfile -Command ^
- "(Get-Content -Raw '!TEMP_JSON!' | ConvertFrom-Json).format.tags.album"`) do set "ALBUM=%%a"
- for /f "usebackq delims=" %%a in (`powershell -NoProfile -Command ^
- "(Get-Content -Raw '!TEMP_JSON!' | ConvertFrom-Json).format.tags.title"`) do set "TITLE=%%a"
- for /f "usebackq delims=" %%a in (`powershell -NoProfile -Command ^
- "(Get-Content -Raw '!TEMP_JSON!' | ConvertFrom-Json).format.tags.track"`) do set "TRACK=%%a"
- :: Extract bit depth using ffprobe
- for /f "usebackq delims=" %%b in (`ffprobe -v quiet -select_streams a:0 -show_entries stream=bits_per_sample -of default=nw=1:nk=1 "%%F"`) do set "BITDEPTH=%%b"
- :: Choose sample format based on bit depth
- if "!BITDEPTH!"=="24" (
- set "SAMPLEFMT=s32"
- ) else (
- set "SAMPLEFMT=s16"
- )
- :: Set defaults
- if not defined ARTIST set "ARTIST=Unknown_Artist"
- if not defined ALBUM set "ALBUM=Unknown_Album"
- if not defined TITLE set "TITLE=Unknown_Title"
- if not defined TRACK set "TRACK=00"
- :: Sanitize metadata
- call :SanitizeString ARTIST
- call :SanitizeString ALBUM
- call :SanitizeString TITLE
- :: Strip "1/13" to just "1"
- for /f "tokens=1 delims=/" %%t in ("!TRACK!") do set "TRACK=%%t"
- if 1!TRACK! LSS 110 set "TRACK=0!TRACK!"
- :: Log metadata
- echo Artist: !ARTIST! >> "%LOG_FILE%"
- echo Album : !ALBUM! >> "%LOG_FILE%"
- echo Title : !TITLE! >> "%LOG_FILE%"
- echo Track : !TRACK! >> "%LOG_FILE%"
- echo BitDepth: !BITDEPTH! → SampleFmt: !SAMPLEFMT! >> "%LOG_FILE%"
- :: Set final path and filename
- set "TARGET_DIR=%OUTPUT_DIR%\!ARTIST!\!ALBUM!"
- if not exist "!TARGET_DIR!" mkdir "!TARGET_DIR!"
- set "OUTPUT_FILE=!TRACK! - !TITLE!.wav"
- echo Saving to: !TARGET_DIR!\!OUTPUT_FILE! >> "%LOG_FILE%"
- :: Convert to WAV using detected bit depth
- ffmpeg -y -i "%%F" -sample_fmt !SAMPLEFMT! "!TARGET_DIR!\!OUTPUT_FILE!" >> "%LOG_FILE%" 2>&1
- :: Encode to ATRAC3 at 132kbps stereo, save .at3 in same folder as .wav
- set "ATRAC_FILE=!OUTPUT_FILE:.wav=.at3!"
- at3tool.exe -e -br 132 "!TARGET_DIR!\!OUTPUT_FILE!" "!TARGET_DIR!\!ATRAC_FILE!" >> "%LOG_FILE%" 2>&1
- if exist "!TARGET_DIR!\!ATRAC_FILE!" (
- echo Created ATRAC: !TARGET_DIR!\!ATRAC_FILE! >> "%LOG_FILE%"
- ) else (
- echo ERROR: ATRAC not found at !TARGET_DIR!\!ATRAC_FILE! >> "%LOG_FILE%"
- )
- echo Done converting %%~nxF >> "%LOG_FILE%"
- del "!TEMP_JSON!"
- )
- echo.
- echo All files processed. See conversion_log.txt for details.
- :: Ask user if WAV files should be deleted
- echo.
- set /p DELWAV=Delete all converted WAV files? (y/n):
- if /i "%DELWAV%"=="y" (
- echo Deleting WAV files...
- for /r "%OUTPUT_DIR%" %%W in (*.wav) do del "%%W"
- echo WAV files deleted.
- ) else (
- echo Keeping WAV files.
- )
- :: Ask user if log file should be deleted
- echo.
- set /p DELLOG=Delete conversion log file? (y/n):
- if /i "%DELLOG%"=="y" (
- del "%LOG_FILE%"
- echo Log file deleted.
- ) else (
- echo Keeping log file.
- )
- echo.
- pause
- goto :eof
- :SanitizeString
- :: Properly sanitize invalid filename characters in the value of a variable
- set "varname=%~1"
- call set "value=%%%varname%%%"
- setlocal enabledelayedexpansion
- set "value=!value:^<=_!"
- set "value=!value:^>=_!"
- set "value=!value::=_!"
- set "value=!value:"=_!"
- set "value=!value:/=_!"
- set "value=!value:\=_!"
- set "value=!value:|=_!"
- set "value=!value:?=_!"
- for /f "tokens=* delims= " %%x in ("!value!") do set "value=%%x"
- endlocal & set "%varname%=%value%"
- goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement