Wanted: Command line m3u creator

Our "pub" where you can post about things completely Off Topic or about non-silent PC issues.

Moderators: NeilBlanchard, Ralf Hutter, sthayashi, Lawrence Lee

Post Reply
Vicotnik
*Lifetime Patron*
Posts: 1831
Joined: Thu Feb 13, 2003 6:53 am
Location: Sweden

Wanted: Command line m3u creator

Post by Vicotnik » Thu Oct 29, 2009 2:24 pm

I'm looking for a Win32 program (or a script) that creates an extended m3u file. It must be command line and work with both mp3 and flac.

For example lets say I have a few files:
c:\music_album\1.flac
c:\music_album\2.flac
c:\music_album\1.mp3
c:\music_album\2.mp3

Executing c:\music_album\createM3u.exe should result in the file
c:\music_album\music_album.m3u
with this content:

#EXTM3U
#EXTINF:123,1
1.flac
#EXTINF:123,1
1.mp3
#EXTINF:123,2
2.flac
#EXTINF:123,2
2.mp3

(Assuming all files are 123 seconds long).

Anyone knows of such a program?

lm
Friend of SPCR
Posts: 1251
Joined: Wed Dec 17, 2003 6:14 am
Location: Finland

Post by lm » Thu Oct 29, 2009 3:24 pm

Should be easily doable, e.g. in python (assuming you have command line programs to tell the length in seconds for each music file type, otherwise it becomes more work).

Code: Select all

import sys,os

def duration(f):
	return 1 # TODO output length in seconds if f is a song, else None

dir = sys.argv[1]
os.chdir(dir)
n = os.getcwd().split('/')[-1]
playlist = n + ".m3u"
m3u = open(playlist,'w')
m3u.write("#EXTM3U\n")
for f in os.listdir(dir):
	l = duration(f)
	if l:
		m3u.write("#EXTINF:"+str(l)+','+f.strip('.')[0]+'\n'+f+'\n')
	elif f != playlist:
		print "found unknown file:", f
Here's a start, but I can't be bothered to implement the length() function that finds out the correct length in seconds (and figures out if a file is a song or not).

Note to other programmers: far from ready, so rather than complaining about any bugs, just complete it yourself.

Vicotnik
*Lifetime Patron*
Posts: 1831
Joined: Thu Feb 13, 2003 6:53 am
Location: Sweden

Post by Vicotnik » Thu Oct 29, 2009 4:26 pm

Thanks lm. But I cannot seem to find a command line program that returns the length of a flac.
I'm testing a bit with Clamp right now. It's a command line interface for Winamp, but it seems a bit buggy.

ascl
Posts: 279
Joined: Tue May 05, 2009 1:15 am
Location: Sydney, Australia

Post by ascl » Thu Oct 29, 2009 5:16 pm

Head to here: http://sourceforge.net/projects/flac/files/flac-win/

And grab the latest flac package. Inside you will find metaflac.exe, using it you can retrieve the sample_rate and total samples in a file. samples / sample_rate will give the length.


There may be other methods, but this is one I found using the standard flac tools.

Vicotnik
*Lifetime Patron*
Posts: 1831
Joined: Thu Feb 13, 2003 6:53 am
Location: Sweden

Post by Vicotnik » Thu Oct 29, 2009 8:16 pm

Thank you ascl. I think I finally got a batch script together that does what I need it to do.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

if not exist *.flac if not exist *.mp3 goto nofiles

for %%x in (".") do set dir=%%~nx
echo #EXTM3U > "%dir%".m3u

for %%x in (*.flac) do (
   for /f "tokens=*" %%a in ('metaflac.exe --show-total-samples "%%x"') do set samples=%%a
   for /f "tokens=*" %%a in ('metaflac.exe --show-sample-rate "%%x"') do set rate=%%a
   set /a seconds=!samples!/!rate!
   echo #EXTINF:!seconds!,%%~nx >> "%dir%".m3u
   echo %%x >> "%dir%".m3u
)

for %%x in (*.mp3) do (
   for /f "tokens=*" %%a in ('mp3info.exe -p %%S "%%x"') do set seconds=%%a
   echo #EXTINF:!seconds!,%%~nx >> "%dir%".m3u
   echo %%x >> "%dir%".m3u
)

echo %dir%.m3u created
goto EOF

:nofiles
echo Cannot find any flac/mp3 files
I'm using metaflac and mp3info to get the length of the files. Another productive night here at work. :P

edit: Made a few slight changes to the script.
Last edited by Vicotnik on Sun Nov 01, 2009 7:14 am, edited 1 time in total.

ascl
Posts: 279
Joined: Tue May 05, 2009 1:15 am
Location: Sydney, Australia

Post by ascl » Thu Oct 29, 2009 8:42 pm

Excellent, thanks for posting. One small potential problem is:

Code: Select all

set /a seconds=!samples!/!rate! 
I think this doesn't round properly, it just truncates to an 'int', which may mean your song length isn't quite right (ie samples = 3, rate = 2, length will equal 1).

Not sure how much it matters (if at all) in an m3u file.

nick705
Posts: 1162
Joined: Tue Mar 23, 2004 3:26 pm
Location: UK

Post by nick705 » Fri Oct 30, 2009 8:07 am

Vicotnik wrote:Thank you ascl. I think I finally got a batch script together that does what I need it to do.
Sorry I didn't see this thread a bit earlier, I might have saved you a bit of bother: http://www.synthetic-soul.co.uk/tag/

tag --playlist *.flac should give you what you want. :)

Post Reply