Skip to main content

Topic: Batch file to split FLAC image files into tracks (Read 2296 times) previous topic - next topic

0 Members and 1 Guest are viewing this topic.
Batch file to split FLAC image files into tracks
This question comes up often enough, so in the hopes that it might be useful to someone else, here's a batch file I wrote to split a FLAC image file into individual tracks.  It's rudimentary, but it does what I need it to.  Note that it requires the freeware utilities Tag.exe and SHNTool.exe, and that you'll need to change the %EXEPATH% variable to point to whatever location you install them in.

Code: [Select]
@echo off
setlocal EnableDelayedExpansion

REM *** define variables
set EXEPATH=C:\Portable Apps
set TAG="%EXEPATH%\Tag.exe"
set SHNTOOL="%EXEPATH%\SHNTool.exe"
set "FILE_SCHEME="^%%n ^^ ^%%t""
set "TAG_SCHEME=[A] L\N ^ T"

REM *** make a folder to hold the output
for /f "tokens=*" %%g in ('dir /b *.flac') do          (
  set "NAME=%%~ng"
  set "FOLDER=%%~ng"
  if not exist "!FOLDER!" md "!FOLDER!"

  REM *** extract the embedded CUE sheet
  %TAG% --tocuea "!FILE!.flac"
  ren "!FILE!.flac.cue" "!FILE!.cue"

  REM *** split the image file into individual tracks
  %SHNTOOL% split -f "!FILE!.cue" "!FILE!.flac" -d "!FOLDER!" -t !FILE_SCHEME! -o flac

  REM *** and make tags from the file names
  %TAG% --auto --scheme "!TAG_SCHEME!" "!FOLDER!\*.flac"
  )
endlocal
echo Finished.  Press any key to exit.
pause > nul
exit
  • Last Edit: 20 February, 2017, 07:36:11 PM by Wally Walters

Re: Batch file to split FLAC image files into tracks
Reply #1
Here's an updated version that does a better job handling tags (the first version reassembled the tags from the file names, which meant all "reserved" characters like question marks, colons and forward-slashes were lost) and prevents tiny "pregap" files from being created.

Code: [Select]
REM *****  initialize  *****

cls
@echo off
setlocal EnableDelayedExpansion
set "FILE_SCHEME="^%%n ^^ ^%%t""
set "TAG_SCHEME=[A] L\N ^ T"

REM *****  split FLACs  *****

for /f "tokens=*" %%g in ('dir /b *.flac') do (
  set "FILE=%%~ng"
  set "FOLDER=%%~ng"
  if exist "!FILE!.cue" copy "!FILE!.cue" "!FILE! (old).cue" & del "!FILE!.cue"
  tag --hidetags --hideinfo --hidenames --simple --tocuea "!FILE!.flac"
  if exist "!FILE!.flac.cue" ren "!FILE!.flac.cue" "!FILE!.cue"
  REM *****  parse CUE sheet  ***** 
  (
  for /f "usebackqdelims=" %%a in ("!FILE!.cue") do (
      set "LINE=%%a"
      echo !LINE! | findstr /i /c:"INDEX" > nul & if errorlevel 1 (
        set "LINE=!LINE:?=!"
        set "LINE=!LINE:/= - !"
        set "LINE=!LINE::= -- !"
        set "LINE=!LINE:  -= -!"
        set "LINE=!LINE:-  =- !"
        )
      echo !LINE!
  )
  )>"!FILE!.tmp.cue"
  if exist "!FILE!.tmp.cue" (
    ren "!FILE!.cue" "!FILE! (embedded).cue"
    ren "!FILE!.tmp.cue" "!FILE!.cue"
  )
  if not exist "!FOLDER!" md "!FOLDER!"
  SHNTool split -f "!FILE!.cue" "!FILE!.flac" -d "!FOLDER!" -t !FILE_SCHEME! -o flac
  if exist "!FOLDER!\* pregap.flac" del "!FOLDER!\* pregap.flac"

REM *****  tag FLACs  *****

  if not exist "!FILE! (embedded).cue" (
    tag --auto --scheme "!TAG_SCHEME!" "!FOLDER!\*.flac"
    goto JUMP
  )
  set "CUE_SHEET=!FILE! (embedded).cue"
  set COUNT=0
  for /f "tokens=*" %%a in ('findstr /i /v /r /c:"^ *INDEX " /c:"^ *FILE " /c:"^ *REM " "!CUE_SHEET!"') do (
    set "INPUT=%%a"
    set INPUT=!INPUT:"=!
    echo !INPUT! | findstr /C:"TRACK" > nul & if not errorlevel 1 (
      call :TAG
      set "ARTIST="
      set "TRACK=!INPUT:TRACK =!"
      set "TRACK=!TRACK: AUDIO=!"
      )
    echo !INPUT! | findstr /C:"TITLE" > nul & if not errorlevel 1 (
      if defined TRACK (set "TITLE=!INPUT:TITLE =!") else (set "ALBUM=!INPUT:TITLE =!" )
      )
    echo !INPUT! | findstr /C:"PERFORMER" > nul & if not errorlevel 1 (
      set "ARTIST=!INPUT:PERFORMER =!"
      if not defined TRACK set ALBUM_ARTIST=!ARTIST!
      )
  )
  call :TAG
  set TRACK_ID=0
  for /f "tokens=*" %%i in ('dir /b "!FOLDER!\*.flac"') do (
    set /a TRACK_ID=TRACK_ID + 1
    set "TRACK_NAME=%%i"
    call set TAG=%%VAR[!TRACK_ID!]%%
    tag "!FOLDER!\!TRACK_NAME!" !TAG!
    )
  :JUMP
   echo > nul

REM *****  clean up and exit  *****

  del "!FILE! (embedded).cue"
  md "!FOLDER! (album)"
  move "!FILE!.cue" "!FOLDER! (album)"
  move "!FILE!.flac" "!FOLDER! (album)"
  move "!FOLDER!" "!FOLDER! (tracks)"
)
endlocal
cls
echo. & echo. & echo. & echo. & echo. & echo.
echo           Finished.  Press any key to exit.
pause > nul
exit

REM *****  SUBROUTINES  *****

:TAG
 if defined TRACK (
    if defined TITLE (
      if not defined ARTIST set "ARTIST=!ALBUM_ARTIST!"
      set /a COUNT=!COUNT!+1
      set VAR[!COUNT!]=--track !TRACK! --artist "!ARTIST!" --album "!ALBUM!" --title "!TITLE!"
      call echo %%VAR[!COUNT!]%%
    )
  )
  goto :eof
  • Last Edit: 07 March, 2017, 12:12:10 AM by Wally Walters

  • kola
  • [*]
Re: Batch file to split FLAC image files into tracks
Reply #2
nothing works here...but interesting idea

  • Porcus
  • [*][*][*][*][*]
Re: Batch file to split FLAC image files into tracks
Reply #3
An alternative tool could be the CUETools command-line: http://cue.tools/wiki/CUETools#Command_line_options