Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for ripcd.bat

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

  1. @ECHO OFF
  2. SETLOCAL ENABLEDELAYEDEXPANSION
  3.  
  4. ECHO.
  5.  
  6. :: Initialize variables
  7. SET CDROMDrive=
  8. SET CDROMDrives=0
  9. SET Track=0
  10. SET VerboseOutput=
  11. SET VLCPath=""
  12.  
  13. :: Check if target folder is specified on the command line (mandatory)
  14. IF "%~1"=="" GOTO Syntax
  15. SET TargetFolder=%~1
  16.  
  17. :: If CDROM drive is specified on the command line, it overrules the automatic detection
  18. IF     "%~2"=="" (CALL :FindCD)
  19. IF /I  "%~2"=="/Q" (
  20. 	SET VerboseOutput=REM
  21. 	CALL :FindCD
  22. )
  23. IF NOT "%~2"=="" IF /I NOT "%~2"=="/Q" (
  24. 	REM :: Check if the second command line argument is a drive letter
  25. 	ECHO "%~2" | FINDSTR /R /B /I /C:"\""[D-Z]:\""" >NUL
  26. 	IF ERRORLEVEL 1 GOTO Syntax
  27. 	SET CDROMDrive=%~2
  28. )
  29. IF "%CDROMDrive%"=="" (
  30. 	ECHO ERROR:   Unable to find the CDROM drive.
  31. 	ECHO          Please specify the CDROM drive letter.
  32. 	ECHO.
  33. 	GOTO Syntax
  34. )
  35.  
  36. IF /I "%~3"=="/Q" SET VerboseOutput=REM
  37.  
  38. :: Locate VLC.exe
  39. CALL :FindVLC
  40.  
  41. :: Check if the specified output directory/target folder is a valid local filesystem path
  42. ECHO "%~1" | FINDSTR /R /B /I /C:"\""[A-Z]:\\.*" >NUL
  43. IF ERRORLEVEL 1 (
  44. 	REM :: Check if the specified output directory/target folder is a valid UNC path
  45. 	ECHO "%~1" | FINDSTR /R /B /I /C:"\""\\\\[A-Z0-9_][A-Z0-9_-]*\\[A-Z0-9_][A-Z0-9_-]*\$*\\.*" >NUL
  46. 	IF ERRORLEVEL 1 GOTO Syntax
  47. )
  48.  
  49. :: Add a trailing backslash to the target path if necessary
  50. IF DEFINED TargetFolder IF NOT "%TargetFolder:~-1%"=="\" SET TargetFolder=%TargetFolder%\
  51.  
  52. :: Create the output directory, if necessary
  53. IF EXIST "%TargetFolder%" (
  54. 	IF "%VerboseOutput%"=="" (
  55. 		ECHO STATUS:  Output directory already exists, reusing it . . .
  56. 		IF EXIST "%TargetFolder%Track*.mp3" (
  57. 			ECHO.
  58. 			ECHO WARNING: Output directory contains Track*.mp3 files.
  59. 			REM :: The first character in the prompt after the /M switch is 0x08, otherwise the leading spaces would be ignored.
  60. 			CHOICE /C NY /N /D N /T 10 /M "         Do you want to overwrite existing files in this folder? [y/N] "
  61. 			IF NOT ERRORLEVEL 2 (
  62. 				ECHO.
  63. 				ECHO          Aborting.
  64. 				ENDLOCAL
  65. 				EXIT /B 2
  66. 			)
  67. 			ECHO.
  68. 		)
  69. 	)
  70. )
  71. :: Create target folder if it does not exist
  72. IF NOT EXIST "%TargetFolder%" (
  73. 	%VerboseOutput% ECHO STATUS:  Trying to create output directory
  74. 	MD "%TargetFolder%"
  75. 	IF EXIST "%TargetFolder%" (
  76. 		ECHO STATUS:  Output directory created successfully
  77. 	) ELSE (
  78. 		ECHO ERROR:   Unable to access target folder %TargetFolder%
  79. 		TIMEOUT /T 6 >NUL 2>&1
  80. 		ECHO.
  81. 		GOTO Syntax
  82. 	)
  83. )
  84.  
  85. %VerboseOutput% ECHO STATUS:  Exporting to "%TargetFolder%" . . .
  86.  
  87. :: Read and convert each track
  88. %VerboseOutput% ECHO STATUS:  Reading tracks from CD in drive %CDROMDrive%
  89. FOR /R %CDROMDrive%\ %%G IN (*.cda) DO (CALL :RunVLC "%%G")
  90.  
  91. :: Add leading zeroes if necessary
  92. IF EXIST "%TargetFolder%Track10.mp3" (
  93. 	FOR /L %%A IN (1,1,9) DO (
  94. 		REN "%TargetFolder%Track%%A.mp3" Track0%%A.mp3
  95. 	)
  96. )
  97.  
  98. :: Done
  99. ENDLOCAL
  100. GOTO:EOF
  101.  
  102.  
  103.  
  104.  
  105. :RunVLC
  106. :: Here's where the actual transcoding/conversion happens
  107. SET /A Track += 1
  108. %VerboseOutput% ECHO.
  109. %VerboseOutput% ECHO STATUS:  Transcoding %1
  110. SET OutTrack=%TargetFolder%Track%Track%.mp3
  111. %VerboseOutput% ECHO STATUS:  Output to "%OutTrack%"
  112. %VerboseOutput% ECHO STATUS:  Exporting . . .
  113. :: The next line fires off a command to VLC.exe with the relevant arguments
  114. START /WAIT "" %VLCPath% -I http cdda:///%CDROMDrive%/ --cdda-track=%Track% :sout=#transcode{vcodec=none,acodec=mp3,ab=128,channels=2,samplerate=44100}:std{access="file",mux=raw,dst="%OutTrack%"} vlc://quit
  115. GOTO:EOF
  116.  
  117.  
  118.  
  119.  
  120. :FindCD
  121. :: Will set the variable CDROMDrive to contain the drive letter of the only CDROM drive that contains an audio CD
  122. ::
  123. :: Get the CDROM drive count
  124. FOR /F %%A IN ('WMIC.EXE Path Win32_CDROMDrive Get /Format:CSV ^| MORE /E +2 ^| FIND /C ","') DO (SET CDROMDrives=%%A)
  125. IF "%CDROMDrives%"=="0" (
  126. 	ECHO ERROR:   No CDROM drive detected.
  127. 	ECHO          Please specify the drive to be used.
  128. 	TIMEOUT /T 10 >NUL 2>&1
  129. 	GOTO Syntax
  130. )
  131. :: Get all CDROM drive letters
  132. SET Count=0
  133. SET Drives.=
  134. SET DriveLetters=
  135. FOR /F "tokens=2 delims==" %%A IN ('WMIC.EXE Path Win32_CDROMDrive Get Drive /Format:Value') DO (
  136. 	FOR /F %%B IN ("%%~A") DO (
  137. 		SET Index=%%B
  138. 		SET Index=!Index:~0,1!
  139. 		SET Drives.!Index!=%%B
  140. 	)
  141. )
  142. :: Sort the drive letters and create a string
  143. FOR /F "tokens=2 delims==" %%A IN ('SET Drives.') DO (
  144. 	SET /A Count += 1
  145. 	IF "!DriveLetters!"=="" (
  146. 		SET DriveLetters=%%A
  147. 	) ELSE (
  148. 		IF !Count! EQU %CDROMDrives% (SET DriveLetters=!DriveLetters! or %%A)
  149. 		IF !Count! NEQ %CDROMDrives% (SET DriveLetters=!DriveLetters!, %%A)
  150. 	)
  151. )
  152. :Retry
  153. :: Get the CDROM drives with an audio CD inserted
  154. SET CDLoaded=0
  155. FOR /F "tokens=*" %%A IN ('WMIC.EXE Path Win32_CDROMDrive Where ^(MediaLoaded^=True And VolumeName LIKE "Audio%%"^) Get Drive^,VolumeName /Format:CSV 2^>NUL ^| MORE /E +2 ^| FIND /C ","') DO SET CDLoaded=%%A
  156. :: Prompt to insert audio CD if none found
  157. IF "%CDLoaded%"=="0" (
  158. 	ECHO USER:    Insert an audio CD in one of the CDROM drives ^(%DriveLetters%^)
  159. 	ECHO USER:    Press any key to continue
  160. 	PAUSE > NUL
  161. 	TIMEOUT /T 5 >NUL 2>&1
  162. 	GOTO :Retry
  163. )
  164. :: Abort if multiple audio CDs found
  165. IF %CDLoaded% GTR 1 (
  166. 	ECHO ERROR:   Multiple CDROM drives detected with an audio CD loaded.
  167. 	ECHO          Either remove all audio CDs but one, or specify the CDROM
  168. 	ECHO          drive to be used ^(%DriveLetters%^).
  169. 	REM :: Wait 10 seconds
  170. 	TIMEOUT /T 10 >NUL 2>&1
  171. 	GOTO Syntax
  172. )
  173. :: Return the drive letter for the only CDROM drive containing an audio CD
  174. FOR /F "tokens=*" %%A IN ('WMIC.EXE Path Win32_CDROMDrive Where ^(MediaLoaded^=True And VolumeName LIKE "Audio%%"^) Get Drive^,VolumeName /Format:Value 2^>NUL') DO (
  175. 	SET VolumeName=
  176. 	FOR /F "tokens=1* delims==" %%B IN ("%%~A") DO (
  177. 		FOR /F %%D IN ("%%~C") DO (SET %%B=%%D)
  178. 		SET CDROMDrive=!Drive!
  179. 	)
  180. )
  181. GOTO:EOF
  182.  
  183.  
  184.  
  185.  
  186. :FindVLC
  187. FOR /F "tokens=2*" %%A IN ('REG Query "HKEY_LOCAL_MACHINE\SOFTWARE\VideoLAN\VLC" /ve') DO SET VLCPath="%%~B"
  188. IF ERRORLEVEL 1 GOTO Syntax
  189. IF %VLCPath%=="" (
  190. 	ECHO ERROR:   VLC not found. VLC is required to rip CDs.
  191. 	REM :: The first character in the prompt after the /M switch is 0x08, otherwise the leading spaces would be ignored.
  192. 	CHOICE /C NY /D N /T 10 /M "         Do you want to open the VLC download page in your default browser?"
  193. 	IF ERRORLEVEL 2 START /MAX "" "http://www.videolan.org/"
  194. 	ECHO.
  195. 	GOTO Syntax
  196. )
  197. GOTO:EOF
  198.  
  199.  
  200.  
  201.  
  202. :Syntax
  203. ENDLOCAL
  204.  
  205. ECHO.
  206. ECHO RipCD.bat,  Version 1.01
  207. ECHO Save audio CD tracks as MP3s
  208. ECHO.
  209. ECHO Usage:   RIPCD  targetdir  [ drive: ]  [ /Q ]
  210. ECHO.
  211. ECHO Where:   targetdir    is the target folder to save the MP3s to
  212. ECHO                       ^(e.g. "D:\MP3s\Mike Oldfield\Tubular Bells"^)
  213. ECHO          drive:       is the CDROM drive letter for the audio CD
  214. ECHO                       ^(usually not required, unless there are multiple
  215. ECHO                       CDROM drives simultaneously containing an audio CD^)
  216. ECHO          /Q           Quiet mode: less screen output, and overwrite existing
  217. ECHO                       files without prompting for confirmation
  218. ECHO.
  219. ECHO Notes:   This batch file requires VLC Media Player ^(http://www.videolan.org^)
  220. ECHO          If the batch file cannot find VLC, it will prompt you to download it.
  221. ECHO          MP3 files are named Track1.mp3, Track2.mp3, etc. ^(or Track01.mp3,
  222. ECHO          Track02.mp3, etc. if the number of tracks is 10 or more^).
  223. ECHO.
  224. ECHO Credits: Main functionality by elrobis ^(http://cartometric.com/blog/2015/03/07^)
  225. ECHO          Automatic CDROM drive and VLC path detection by Rob van der Woude
  226. ECHO.
  227. ECHO Written by Rob van der Woude
  228. ECHO http://www.robvanderwoude.com
  229.  
  230. EXIT 1
  231.  

page last uploaded: 2017-08-21, 14:26