Add FLAC support in a script

This is the place for queries that don't fit in any of the other categories.

Add FLAC support in a script

Postby andoru » Thu Jan 16, 2014 3:48 pm

Hello, a fellow Python noob here. A friend of mine made a command prompt script to display/index all the wav files in one folder and calculate their duration by adding the minutes and seconds from each track.
Here's the script:
Code: Select all
# A programme to create an index in hours, minutes, and seconds if all .wav files in the current directory are joined together in order. Also displays track's length in seconds if -s is used on command line.

import sys
import os
import struct

debug = False

NL = "\n"
TB = "\t"

displayTrackSecs = False
displayTimeList = False
removeZZZinList = False
removeZZZ = False

channels = 2
samplerate = 48000
samplesize = 2
bytesPerSecond = channels*samplerate*samplesize
sizeThusFar = 0

timeList = []

def Debug(inTxt):
 if debug: print ("--Debug: " + str(inTxt))

curDirContents = os.listdir(".")
# A header tuple contains: filename, format, #channels, samples per second, bytes per second, bytes per sample, bytes of PCM data.
headerData = []

files = []

if sys.argv.__contains__("-z"):
 removeZZZinList = True
if sys.argv.__contains__("-Z"):
 removeZZZ = True
if sys.argv.__contains__("-s"):
 displayTrackSecs = True

if sys.argv.__contains__("-t"):
 displayTimeList = True

for i in curDirContents:
 if i.endswith(".wav"):

for i in files:
 theFile = open(i, 'rb')
 rawData =
 udTmp = struct.unpack('4sI4s4sIHHIIHH4sI', rawData)
 relevantData = [udTmp[5], udTmp[6], udTmp[7], udTmp[8], udTmp[10]/8, udTmp[12]]
 headerData.append([i] + relevantData)

channels, samplerate, samplesize, bytesPerSecond = headerData[0][2], headerData[0][3], headerData[0][5], headerData[0][4]

for i in headerData:
 if i[1] != 1:
   quit(i[0] + " is not a canonical WAV/PCM file.")
 if channels != i[2]:
   quit(i[0] + " has a different number of channels.")
 if samplerate != i[3]:
   quit(i[0] + " has a different sample rate.")
 if samplesize != i[5]:
   quit(i[0] + " has a different sample size.")

for i in headerData:
 outString = ""
 curTime = sizeThusFar/bytesPerSecond
 hours = curTime/3600
 minutes = (curTime % 3600) /60
 seconds = curTime % 60
 nameTimeSet = [i[0], ((sizeThusFar + i[6])/bytesPerSecond) - curTime]
 # if hours != 0:
 if displayTrackSecs:
   outString += TB + str(nameTimeSet[1])
 if displayTimeList:
   if removeZZZinList:
     if nameTimeSet[0].__contains__("zzz"):
       timeList[timeList.__len__()-1] += nameTimeSet[1]
 outString += i[0] + str(hours) + ":" "%02d:" % minutes + "%02d " % seconds

 if removeZZZ:
   if outString.__contains__("zzz"):
     outString = ""
 outString = outString.replace(".wav", " - ")   
 if outString != "":
   print outString
 sizeThusFar += i[6]
print "Total seconds: " + str(sizeThusFar/bytesPerSecond)

if displayTimeList:
  print timeList

It works well so far, but the only problem is that I usually need it to index my FLAC files, and each time I need to do that, I have to transcode all the FLAC files to WAV leading to a lot of wasteful HDD read/write cycles. So Is it possible to add to this script FLAC support? I know there's a tagging library for mp3, FLAC, ogg and other formats available for Python. If any of you could help me this I would really appreciate it!
Thanks in advance.
Last edited by stranac on Thu Jan 16, 2014 4:26 pm, edited 1 time in total.
Reason: First post lock.
Posts: 3
Joined: Tue Dec 31, 2013 8:49 pm

Re: Add FLAC support in a script

Postby andoru » Sun May 18, 2014 3:39 am

*bump* Anyone?
Posts: 3
Joined: Tue Dec 31, 2013 8:49 pm

Re: Add FLAC support in a script

Postby micseydel » Sun May 18, 2014 5:07 am

Have you tried any of these?
Due to the reasons discussed here we will be moving to on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Add FLAC support in a script

Postby andoru » Fri May 30, 2014 8:36 pm

Thanks for the reply, but as I mentioned in the first post, I don't know much about Python, so don't exactly know how to use those modules... :oops:
Posts: 3
Joined: Tue Dec 31, 2013 8:49 pm

Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 9 guests