windows batch .bat automatic .m3u playlist creation / update

陌路散爱 提交于 2019-12-11 09:29:28

问题


tired of manually updating my huge mp3/mp4 DB each time i add new files on my computer and/or smartphone, so i'm looking for some help to create a batch file that create/update all my playlists .m3u automatically.

2 batch files are required (1st is calling the 2nd) :


1st one is easy, i've almost got it ;-)

create_all_playlist.bat (no arguments required, customized file)

create_m3u.bat  misc-70-80.m3u            misc\70\ misc\80\
create_m3u.bat  misc-70-80-90.m3u         misc\70\ misc\80\ misc\90\
create_m3u.bat  misc-60-70-2000-2010.m3u  misc\60\ misc\70\ misc\2000\ misc\2010\
[...]
create_m3u.bat  albums-2010.m3u           albums\2010\

pretty easy huh?


I need your help to write the 2nd .bat, the dynamic one :

create_m3u.bat output-playlist-name.m3u folder-1 folder-2 [...] folder-n

to play it easy, the output file.m3u should looks like this (note all are relative path and the search for *.mp3 *.mp4 *.ogg (whatever type i cutomize/hard-code) would be recursive into the designated folder)

(raw .m3u compatible with most android & PC player)

misc\2000s\Kelly Rowland - Work (Freemasons Arabic Mix).mp3
misc\2000s\Keri Hilson - Knock You Down ft Kanye West and Ne-Yo.mp3
misc\2000s\Kid Cudi vs Crookers - Day n Nite.mp3
misc\2000s\Korn - Inja.mp3
[...]
misc\2000s\Ladyhawke - My Delirium.mp3

At very best, only for the very best, let's say if someone is skilled enough to produce the full mp3/mp4 TAG reading and formating as following

#EXTM3U
#EXTINF:227,A-Ha - Take On Me
misc\80s\A-Ha - Take On Me.mp3
#EXTINF:257,ACDC - Back in Black
misc\80s\ACDC - Back in Black.mp3
#EXTINF:311,Aerosmith & Run Dmc - Walk This Way
misc\80s\Aerosmith & Run Dmc - Walk This Way.mp3
[...]
#EXTINF:289,Alannah Myles - Black Velvet
misc\80s\Alannah Myles - Black Velvet.mp3

and thanks!!

for the raw m3u, beers are on me :-)

for the extended full m3u : beers and i'll cook the duck magret ;-)


回答1:


@ECHO OFF
SETLOCAL

:: make a tempfile
:maketemp
SET "tempfile=%temp%\%random%"
IF EXIST "%tempfile%*" (GOTO maketemp) ELSE (ECHO.>"%tempfile%a")



:: Need the start of the tree to scan
SET "rootdir=c:\sourcedir"
>"%tempfile%b" (ECHO(%rootdir%&ECHO(*)
FOR /f "delims=:" %%a IN ('FINDSTR /o /L "*" "%tempfile%b"') DO SET /a len=%%a-1

SET "destfile=%~1"

:loop
SHIFT
SET nextdir=%~1
IF NOT DEFINED nextdir GOTO process
PUSHD "%rootdir%\%~1"
FOR /f "delims=" %%a IN ('dir /b /s /a-d *.mp3 *.mp4 *.ogg') DO >>"%tempfile%a" ECHO(%%~na:%%a
POPD 
GOTO loop

:process
>%destfile% ECHO(#EXTM3U
(
FOR /f "tokens=1*delims=:" %%a IN ('SORT "%tempfile%a"') DO (
 ECHO(#EXTINF:???,%%a
 SET "location=%%b"
 SETLOCAL enabledelayedexpansion
 ECHO(!location:~%len%!
 endlocal
)
)>>%destfile%
DEL "%tempfile%*"
GOTO :EOF

I used c:\sourcedir as my starting directory. No parameter-validity checks are performed. You'd need to CALL create_m3u.bat from create_all_playlist.bat because the target executble is a batch file.

As for the missing data - hardest part is extracting information....


Edited to allow ! in filenames and remove leading \ from path


Revision: To permit absolute path

@ECHO Off
SETLOCAL

:: make a tempfile
:maketemp
SET "tempfile=%temp%\%random%"
IF EXIST "%tempfile%*" (GOTO maketemp) ELSE (ECHO.>"%tempfile%a")



:: Need the start of the tree to scan
SET "rootdir=c:\sourcedir"
>"%tempfile%b" (ECHO(%rootdir%&ECHO(*)
FOR /f "delims=:" %%a IN ('FINDSTR /o /L "*" "%tempfile%b"') DO SET /a len=%%a-1

SET "destfile=%~1"

:loop
SHIFT
SET nextdir=%~1
IF NOT DEFINED nextdir GOTO process
IF %nextdir:~0,1%==\ (
 PUSHD "%~1"
 FOR /f "delims=" %%a IN ('dir /b /s /a-d *.mp3 *.mp4 *.ogg') DO >>"%tempfile%a" ECHO(%%~na:2:%%a
) else (
 PUSHD "%rootdir%\%~1"
 FOR /f "delims=" %%a IN ('dir /b /s /a-d *.mp3 *.mp4 *.ogg') DO >>"%tempfile%a" ECHO(%%~na:%len%:%%a
)
POPD 
GOTO loop

:process
>%destfile% ECHO(#EXTM3U
(
FOR /f "tokens=1,2*delims=:" %%a IN ('SORT "%tempfile%a"') DO (
 ECHO(#EXTINF:???,%%a
 SET "location=%%c"
 SETLOCAL enabledelayedexpansion
  ECHO(!location:~%%b!
 endlocal
)
)>>%destfile%

del "%tempfile%*"

GOTO :EOF

Noting that "paths containing spaces must be in quotes"



来源:https://stackoverflow.com/questions/22401214/windows-batch-bat-automatic-m3u-playlist-creation-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!