| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2007 Parisson SARL |
|---|
| 4 | # Copyright (c) 2006-2007 Guillaume Pellerin <pellerin@parisson.com> |
|---|
| 5 | # All rights reserved. |
|---|
| 6 | # |
|---|
| 7 | # This software is licensed as described in the file COPYING, which |
|---|
| 8 | # you should have received as part of this distribution. The terms |
|---|
| 9 | # are also available at http://svn.parisson.org/telemeta/TelemetaLicense. |
|---|
| 10 | # |
|---|
| 11 | # Author: Guillaume Pellerin <pellerin@parisson.com> |
|---|
| 12 | |
|---|
| 13 | import os |
|---|
| 14 | import string |
|---|
| 15 | import subprocess |
|---|
| 16 | |
|---|
| 17 | from telemeta.export.core import * |
|---|
| 18 | from telemeta.export.api import IExporter |
|---|
| 19 | from mutagen.flac import FLAC |
|---|
| 20 | |
|---|
| 21 | class FlacExporter(ExporterCore): |
|---|
| 22 | """Defines methods to export to OGG Vorbis""" |
|---|
| 23 | |
|---|
| 24 | implements(IExporter) |
|---|
| 25 | |
|---|
| 26 | def __init__(self): |
|---|
| 27 | self.item_id = '' |
|---|
| 28 | self.source = '' |
|---|
| 29 | self.metadata = {} |
|---|
| 30 | self.options = {} |
|---|
| 31 | self.description = '' |
|---|
| 32 | self.dest = '' |
|---|
| 33 | self.quality_default = '5' |
|---|
| 34 | self.info = [] |
|---|
| 35 | self.buffer_size = 0xFFFF |
|---|
| 36 | |
|---|
| 37 | def get_format(self): |
|---|
| 38 | return 'FLAC' |
|---|
| 39 | |
|---|
| 40 | def get_file_extension(self): |
|---|
| 41 | return 'flac' |
|---|
| 42 | |
|---|
| 43 | def get_mime_type(self): |
|---|
| 44 | return 'application/flac' |
|---|
| 45 | |
|---|
| 46 | def get_description(self): |
|---|
| 47 | return 'FIXME' |
|---|
| 48 | |
|---|
| 49 | def get_file_info(self): |
|---|
| 50 | try: |
|---|
| 51 | file1, file2 = os.popen4('metaflac --list "'+self.dest+'"') |
|---|
| 52 | info = [] |
|---|
| 53 | for line in file2.readlines(): |
|---|
| 54 | info.append(clean_word(line[:-1])) |
|---|
| 55 | self.info = info |
|---|
| 56 | return self.info |
|---|
| 57 | except IOError: |
|---|
| 58 | return 'Exporter error [1]: file does not exist.' |
|---|
| 59 | |
|---|
| 60 | def set_cache_dir(self,path): |
|---|
| 61 | """Set the directory where cached files should be stored. Does nothing |
|---|
| 62 | if the exporter doesn't support caching. |
|---|
| 63 | |
|---|
| 64 | The driver shouldn't assume that this method will always get called. A |
|---|
| 65 | temporary directory should be used if that's not the case. |
|---|
| 66 | """ |
|---|
| 67 | self.cache_dir = path |
|---|
| 68 | |
|---|
| 69 | def decode(self): |
|---|
| 70 | try: |
|---|
| 71 | file_name, ext = get_file_name(self.source) |
|---|
| 72 | dest = self.cache_dir+os.sep+file_name+'.wav' |
|---|
| 73 | os.system('flac -d -o "'+dest+'" "'+self.source+'"') |
|---|
| 74 | self.source = dest |
|---|
| 75 | return dest |
|---|
| 76 | except IOError: |
|---|
| 77 | return 'ExporterError [2]: decoder not compatible.' |
|---|
| 78 | |
|---|
| 79 | def write_tags(self): |
|---|
| 80 | media = FLAC(self.dest) |
|---|
| 81 | for tag in self.metadata.keys(): |
|---|
| 82 | if tag == 'COMMENT': |
|---|
| 83 | media['DESCRIPTION'] = str(self.metadata[tag]) |
|---|
| 84 | else: |
|---|
| 85 | media[tag] = str(self.metadata[tag]) |
|---|
| 86 | media.save() |
|---|
| 87 | |
|---|
| 88 | def get_args(self,options=None): |
|---|
| 89 | """Get process options and return arguments for the encoder""" |
|---|
| 90 | args = '' |
|---|
| 91 | if not options is None: |
|---|
| 92 | self.options = options |
|---|
| 93 | if not ('verbose' in self.options and self.options['verbose'] != '0'): |
|---|
| 94 | args = args + ' -s ' |
|---|
| 95 | |
|---|
| 96 | if 'flac_quality' in self.options: |
|---|
| 97 | args = args+' -f -'+self.options['flac_quality'] |
|---|
| 98 | else: |
|---|
| 99 | args = args+' -f -'+self.quality_default |
|---|
| 100 | else: |
|---|
| 101 | args = args+' -s -f -'+self.quality_default |
|---|
| 102 | return args |
|---|
| 103 | |
|---|
| 104 | def process(self, item_id, source, metadata, options=None): |
|---|
| 105 | self.item_id = item_id |
|---|
| 106 | self.source = source |
|---|
| 107 | self.metadata = metadata |
|---|
| 108 | #self.options = {} |
|---|
| 109 | self.args = self.get_args(options) |
|---|
| 110 | self.ext = self.get_file_extension() |
|---|
| 111 | self.command = 'sox "'+self.source+'" -q -w -r 44100 -t wav -c2 - '+ \ |
|---|
| 112 | '| flac '+self.args+' -c -' |
|---|
| 113 | |
|---|
| 114 | # Pre-proccessing |
|---|
| 115 | try: |
|---|
| 116 | self.dest = self.pre_process(self.item_id, |
|---|
| 117 | self.source, |
|---|
| 118 | self.metadata, |
|---|
| 119 | self.ext, |
|---|
| 120 | self.cache_dir, |
|---|
| 121 | self.options) |
|---|
| 122 | except: |
|---|
| 123 | yield 'ExporterError [3]: pre_process' |
|---|
| 124 | |
|---|
| 125 | # Processing (streaming + cache writing) |
|---|
| 126 | try: |
|---|
| 127 | stream = self.core_process(self.command,self.buffer_size,self.dest) |
|---|
| 128 | for chunk in stream: |
|---|
| 129 | yield chunk |
|---|
| 130 | except: |
|---|
| 131 | yield 'ExporterError: core_process' |
|---|
| 132 | |
|---|
| 133 | # Post-proccessing |
|---|
| 134 | try: |
|---|
| 135 | self.write_tags() |
|---|
| 136 | self.post_process(self.item_id, |
|---|
| 137 | self.source, |
|---|
| 138 | self.metadata, |
|---|
| 139 | self.ext, |
|---|
| 140 | self.cache_dir, |
|---|
| 141 | self.options) |
|---|
| 142 | except: |
|---|
| 143 | yield 'ExporterError: post_process' |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | # Encoding |
|---|
| 147 | #os.system('flac '+args+' -o "'+self.dest+'" "'+ \ |
|---|
| 148 | # self.source+'" > /dev/null') |
|---|