Permalink
Browse files

add a new hook to convert a m4a file to mp3

  • Loading branch information...
1 parent 4f32636 commit 5e7d3ae6209a15ccf05cb11885461b6470d0696b Bernd Schlapsi committed Nov 23, 2011
Showing with 458 additions and 53 deletions.
  1. +66 −0 m4a_converter.py
  2. +348 −0 tests/config/LogbuchNetzpolitik.rss
  3. +5 −0 tests/config/data.py
  4. +37 −0 tests/m4a_converter_test.py
  5. +0 −51 tests/mp3split_test.py
  6. +2 −2 tests/runtests.py
View
@@ -0,0 +1,66 @@
+# -*- coding: utf-8 -*-
+# Convertes m4a audio files to mp3
+# This requires ffmpeg to be installed. Also works as a context
+# menu item for already-downloaded files.
+#
+# (c) 2011-11-23 Bernd Schlapsi <brot@gmx.info>
+# Released under the same license terms as gPodder itself.
+
+import gpodder
+
+import os
+import shlex
+import subprocess
+
+import logging
+logger = logging.getLogger(__name__)
+
+
+FFMPEG_CMD = 'ffmpeg -i "%(infile)s" -sameq "%(outfile)s"'
+MIME_TYPES = ['audio/x-m4a', 'audio/mp4']
+EXTENSION = '.mp3'
+
+
+class gPodderHooks(object):
+ def __init__(self, test=False):
+ self.test = test
+
+ def on_episode_downloaded(self, episode):
+ self._convert_episode(episode)
+
+ def _convert_episode(self, episode):
+ filename = episode.local_filename(create=False)
+ dirname = os.path.dirname(filename)
+ basename, ext = os.path.splitext(os.path.basename(filename))
+ new_filename = basename + EXTENSION
+
+ if ext != '.m4a':
+ return
+
+ target = os.path.join(dirname, new_filename)
+ cmd = FFMPEG_CMD % {
+ 'infile': filename,
+ 'outfile': target
+ }
+
+ # Prior to Python 2.7.3, this module (shlex) did not support Unicode input.
+ if isinstance(cmd, unicode):
+ cmd = cmd.encode('ascii', 'ignore')
+
+ ffmpeg = subprocess.Popen(shlex.split(cmd),
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE
+ )
+ stdout, stderr = ffmpeg.communicate()
+
+ if ffmpeg.returncode == 0:
+ logger.info('m4a -> mp3 conversion successful.')
+ if not self.test:
+ os.remove(filename)
+ episode.download_filename = new_filename
+ episode.save()
+ else:
+ logger.info('Error converting file. FFMPEG installed?')
+ try:
+ os.remove(target)
+ except OSError:
+ pass
Oops, something went wrong.

0 comments on commit 5e7d3ae

Please sign in to comment.