]> git.parisson.com Git - telemaster.git/commitdiff
use SoX for decoding (impossible to stream multiple flacs to Flac, grrr !)
authoryomguy <yomguy@353fd7da-fb10-4236-9bec-1a49139083f2>
Fri, 1 May 2009 15:24:28 +0000 (15:24 +0000)
committeryomguy <yomguy@353fd7da-fb10-4236-9bec-1a49139083f2>
Fri, 1 May 2009 15:24:28 +0000 (15:24 +0000)
git-svn-id: http://svn.parisson.org/svn/telemaster/trunk@6 353fd7da-fb10-4236-9bec-1a49139083f2

flac2mp3mix.py

index 69587f0ae15bb254bfacab0f22075f3be9a8e1f1..ad5a95f4f9b024e023fd3d819152a30c128f7eb2 100644 (file)
@@ -5,86 +5,109 @@ import os
 import sys
 import mutagen
 import subprocess
+import Queue
+from tools import *
 
-class Flac2Mp3Mix:
 
-    def __init__(self, argv):
-        self.root_dir = argv[1]
-        self.buffer_size = 32768
-        
-    def flac_mix_player(self, media_list):
-        args = ''
-        for media in media_list:
-            args += '"' + media + '" '
-            
-        command = 'cat ' + args + ' | flac -d -c - '
-        print command
-        stream = self.core_process_stdout(command)
-        for chunk in stream:
-            yield chunk
-
-    def core_process_stdout(self, command):
+class SubProcessPipe:
+    
+    def __init__(self, command, stdin=None):
         """Read media and stream data through a generator.
         Taken from Telemeta (see http://telemeta.org)"""
 
-        proc = subprocess.Popen(command.encode('utf-8'),
+        if not stdin:
+            stdin =  subprocess.PIPE
+    
+        self.buffer_size = 32768
+        self.proc = subprocess.Popen(command.encode('utf-8'),
                     shell = True,
                     bufsize = self.buffer_size,
-                    stdin = subprocess.PIPE,
+                    stdin = stdin,
                     stdout = subprocess.PIPE,
                     close_fds = True)
 
-        # Core processing
-        while True:
-            __chunk = proc.stdout.read(self.buffer_size)
-            status = proc.poll()
-            if status != None and status != 0:
-                raise DeeFuzzStreamError('Command failure:', command, proc)
-            if not __chunk:
-                break
-            yield __chunk
-
-    def core_process_stdin(self, command, stream_in):
-        """Read media and stream data through a generator.
-        Taken from Telemeta (see http://telemeta.org)"""
+        self.stdin = self.proc.stdin
+        self.stdout = self.proc.stdout
+       
+    #def stdout(self):
+        #while True:
+            #__chunk = self.proc.stdout.read(self.buffer_size)
+            #if not __chunk:
+                #break
+            #yield __chunk
 
-        proc = subprocess.Popen(command.encode('utf-8'),
-                    shell = True,
-                    bufsize = self.buffer_size,
-                    stdin = subprocess.PIPE,
-                    stdout = subprocess.PIPE,
-                    close_fds = True)
+    #def stdin(self, data):
+        #self.proc.stdin.write(data)
+        #self.proc.stdin.flush()
 
-        # Core processing
-        for __chunk in stream_in:
-            proc.stdin.write(__chunk)
-            proc.stdin.flush()
-            status = proc.poll()
-            if status != None and status != 0:
-                raise DeeFuzzStreamError('Command failure:', command, proc)
-    
 
+class Flac2Mp3Mix:
+
+    def __init__(self, argv):
+        self.root_dir = argv[1]
+        self.buffer_size = 32768
+
+    def mix_player(self, media_list, stdin):
+        command = 'cat '
+        for media in media_list:
+            command += '"' + media + '" '
+        print command
+        return SubProcessPipe(command, stdin)
+
+    def flac_player(self, media_list, stdin):
+        files = ''
+        for media in media_list:
+            files += '"' + media + '" '
+        command = 'sox -t flac %s -q -b 16 -r 44100 -t wav - ' % files
+        return SubProcessPipe(command, stdin)
+
+
+    def mp3_encode(self, stdin, file):
+        command = 'lame -b 256 - "' + file + '"'
+        return SubProcessPipe(command, stdin)
+        
     def get_media_lists(self):
         full_media_list = []
         for root, dirs, files in os.walk(self.root_dir):
             media_list = []
             if root:
                 for file in files:
-                    if file.split('.')[-1] == 'flac':
+                    ext = file.split('.')[-1]
+                    if ext == 'oga' or ext == 'flac':
                         media_list.append(root+os.sep+file)
                 full_media_list.append(media_list)
         return full_media_list
 
-    def main(self, file):
-        command = 'lame -b 256 - "' + file + '"'
+    def main(self):
         media_lists = self.get_media_lists()
-        print media_lists
         for media_list in media_lists:
-            raw_stream = self.flac_mix_player(media_list)
-            self.core_process_stdin(command, raw_stream)
-            
+            if media_list:
+                media_list.sort()
+                root_dir = os.sep.join(media_list[0].split(os.sep)[:-1])
+                print root_dir
+                album_name = root_dir.split(os.sep)[-2]
+                print album_name
+
+                media_0 = media_list[0]
+                flac = Flac(media_0)
+                metadata = flac.get_tags()
+                mp3_file = root_dir + os.sep + album_name + '.mp3'                
+                f = self.flac_player(media_list, None)
+                m = self.mp3_encode(f.stdout, mp3_file)
+                print 'Mixing : '
+                print media_list
+                print 'Encoding...'
+                m.proc.wait()
+                print 'Writing metadata...'
+                mp3 = Mp3(mp3_file)
+                mp3.metadata = metadata
+                mp3.write_tags()
+                print 'Done !'
+            break
+
+
 
 if __name__ == '__main__':
     f = Flac2Mp3Mix(sys.argv)
-    f.main('/tmp/test.mp3')
+    f.main()