Properly update existing episodes (bug 211)
[gpodder.git] / src / gpodder / libconverter.py
blob83961e83429f6c04110159eea4e08480c2bc328a
1 # -*- coding: utf-8 -*-
3 # gPodder - A media aggregator and podcast client
4 # Copyright (c) 2005-2008 Thomas Perl and the gPodder Team
6 # gPodder is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # gPodder is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 # libconverter.py -- [any]-to-mp3 conversion library
23 # thomas perl <thp@perli.net> 20070318
28 import re
29 import popen2
30 import os
31 import os.path
32 import types
34 from gpodder import util
35 from gpodder.liblogger import log
37 class FileConverter:
38 percentage_match = re.compile('(\d+)%')
40 def __init__( self, decoder_command, decoder_arguments):
41 self.encoder_command = 'lame --nohist /dev/stdin "%s"'
42 self.decoder_command = ' '.join( ( decoder_command, decoder_arguments ))
44 def convert( self, input_filename, output_filename, callback = None):
45 input_command = self.decoder_command % input_filename
46 output_command = self.encoder_command % output_filename
48 command = '%s | %s' % ( input_command, output_command )
50 process = popen2.Popen4( command)
51 stdout = process.fromchild
52 s = stdout.read( 80)
53 while s:
54 if callback:
55 for result in self.percentage_match.finditer(s):
56 try:
57 callback(result.group(1).strip())
58 except:
59 log('Cannot call callback for status percentage.', sender=self)
60 s = stdout.read( 80)
62 return process.wait() == 0
64 class ConverterCollection( types.DictType):
65 def add_converter( self, extension, command, arguments):
66 if util.find_command(command) is not None:
67 log( 'Found "%s", will try to convert ".%s" files.' % ( command, extension ), sender = self)
68 self[extension.lower()] = FileConverter( command, arguments)
69 else:
70 log( 'Could not find "%s", ".%s" files cannot be converted.' % ( command, extension ), sender = self)
72 def has_converter(self, extension):
73 if util.find_command('lame') is not None:
74 extension = extension.lower()
75 if extension[0] == '.':
76 extension = extension[1:]
77 return self.has_key(extension)
78 else:
79 log('Please install the "lame" package to convert files.', sender=self)
80 return False
82 def convert( self, input_filename, output_filename = None, callback = None):
83 extension = os.path.splitext( input_filename)[1][1:]
84 if extension.lower() not in self:
85 return None
87 if not output_filename:
88 output_filename = os.path.splitext( input_filename)[0]+'.mp3'
90 if not self[extension.lower()].convert( input_filename, output_filename, callback):
91 return None
93 return output_filename
96 converters = ConverterCollection()
98 # Add known converter applications
99 converters.add_converter( 'ogg', 'oggdec', '--quiet --output=/dev/stdout "%s"')