Пример #1
0
    def _convert_episode(self, episode):
        old_filename = episode.local_filename(create=False)
        filename, ext = os.path.splitext(old_filename)
        new_filename = filename + ".mp4"

        if open(old_filename, "rb").read(3) != "FLV":
            logger.debug("Not a FLV file. Ignoring.")
            return

        if ext.lower() == ".mp4":
            # Move file out of place for conversion
            tmp_filename = filename + ".flv"
            os.rename(old_filename, tmp_filename)
            old_filename = tmp_filename

        cmd = ["ffmpeg", "-i", old_filename, "-vcodec", "copy", "-acodec", "copy", new_filename]

        ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = ffmpeg.communicate()

        if ffmpeg.returncode == 0:
            util.rename_episode_file(episode, new_filename)
            os.remove(old_filename)

            logger.info("FLV conversion successful.")
            gpodder.user_extensions.on_notification_show(_("File converted"), episode.title)
        else:
            logger.warn("Error converting file: %s / %s", stdout, stderr)
            gpodder.user_extensions.on_notification_show(_("Conversion failed"), episode.title)
Пример #2
0
    def _convert_episode(self, episode):
        if not self._check_source(episode):
            return

        new_extension = self._get_new_extension()
        old_filename = episode.local_filename(create=False)
        filename, old_extension = os.path.splitext(old_filename)
        new_filename = filename + new_extension
        
        cmd = [self.command] + \
            [param % {'old_file': old_filename, 'new_file': new_filename}
                for param in self.command_param]
        ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                stderr=subprocess.PIPE)
        stdout, stderr = ffmpeg.communicate()

        if ffmpeg.returncode == 0:            
            util.rename_episode_file(episode, new_filename)
            os.remove(old_filename)
            
            logger.info('Converted video file to %(format)s.' % {'format': self.config.output_format})
            gpodder.user_extensions.on_notification_show(_('File converted'), episode.title)
        else:
            logger.warn('Error converting video file: %s / %s', stdout, stderr)
            gpodder.user_extensions.on_notification_show(_('Conversion failed'), episode.title)
Пример #3
0
    def _convert_episode(self, episode):
        if episode.mime_type not in self.MIME_TYPES:
            return

        if self.config.use_ogg:
            extension = '.ogg'
        else:
            extension = '.mp3'

        old_filename = episode.local_filename(create=False)
        filename, old_extension = os.path.splitext(old_filename)
        new_filename = filename + extension

        cmd = ['ffmpeg', '-i', old_filename, '-sameq', new_filename]
        ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                stderr=subprocess.PIPE)
        stdout, stderr = ffmpeg.communicate()

        if ffmpeg.returncode == 0:
            util.rename_episode_file(episode, new_filename)
            os.remove(old_filename)
            
            logger.info('Converted M4A file.')
            gpodder.user_extensions.on_notification_show(_('File converted'), episode.title)
        else:
            logger.warn('Error converting file: %s / %s', stdout, stderr)
            gpodder.user_extensions.on_notification_show(_('Conversion failed'), episode.title)
Пример #4
0
    def _convert_episode(self, episode):
        if not self._check_source(episode):
            return

        new_extension = self._get_new_extension()
        old_filename = episode.local_filename(create=False)
        filename, old_extension = os.path.splitext(old_filename)
        new_filename = filename + new_extension

        cmd = [self.command] + \
            [param % {'old_file': old_filename, 'new_file': new_filename}
                for param in self.command_param]
        ffmpeg = subprocess.Popen(cmd,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE)
        stdout, stderr = ffmpeg.communicate()

        if ffmpeg.returncode == 0:
            util.rename_episode_file(episode, new_filename)
            os.remove(old_filename)

            logger.info('Converted video file to %(format)s.' %
                        {'format': self.config.output_format})
            gpodder.user_extensions.on_notification_show(
                _('File converted'), episode.title)
        else:
            logger.warn('Error converting video file: %s / %s', stdout, stderr)
            gpodder.user_extensions.on_notification_show(
                _('Conversion failed'), episode.title)
Пример #5
0
    def _convert_episode(self, episode):
        old_filename = episode.local_filename(create=False)
        filename, ext = os.path.splitext(old_filename)
        new_filename = filename + '.mp4'

        if open(old_filename, 'rb').read(3) != 'FLV':
            logger.debug('Not a FLV file. Ignoring.')
            return

        if ext.lower() == '.mp4':
            # Move file out of place for conversion
            tmp_filename = filename + '.flv'
            os.rename(old_filename, tmp_filename)
            old_filename = tmp_filename

        cmd = ['ffmpeg',
                '-i', old_filename,
                '-vcodec', 'copy',
                '-acodec', 'copy',
                new_filename]

        ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                stderr=subprocess.PIPE)
        stdout, stderr = ffmpeg.communicate()

        if ffmpeg.returncode == 0:
            logger.info('FLV conversion successful.')
            util.rename_episode_file(episode, new_filename)
            os.remove(old_filename)
        else:
            logger.warn('Error converting file: %s / %s', stdout, stderr)
Пример #6
0
    def _convert_episode(self, episode):
        if not self._check_source(episode):
            return

        new_extension = self._get_new_extension()
        old_filename = episode.local_filename(create=False)
        filename, old_extension = os.path.splitext(old_filename)
        new_filename = filename + new_extension

        cmd_param = self.CMD[self.command_without_ext][new_extension]
        cmd = [self.command] + \
            [param % {'old_file': old_filename, 'new_file': new_filename}
                for param in cmd_param]

        if gpodder.ui.win32:
            ffmpeg = util.Popen(cmd)
            ffmpeg.wait()
            stdout, stderr = ("<unavailable>",) * 2
        else:
            ffmpeg = util.Popen(cmd, stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE)
            stdout, stderr = ffmpeg.communicate()

        if ffmpeg.returncode == 0:
            util.rename_episode_file(episode, new_filename)
            os.remove(old_filename)

            logger.info('Converted audio file to %(format)s.' % {'format': new_extension})
            gpodder.user_extensions.on_notification_show(_('File converted'), episode.title)
        else:
            logger.warn('Error converting audio file: %s / %s', stdout, stderr)
            gpodder.user_extensions.on_notification_show(_('Conversion failed'), episode.title)
Пример #7
0
    def _convert_episode(self, episode):
        if episode.mime_type not in self.MIME_TYPES:
            return

        if self.config.use_ogg:
            extension = '.ogg'
        else:
            extension = '.mp3'

        old_filename = episode.local_filename(create=False)
        filename, old_extension = os.path.splitext(old_filename)
        new_filename = filename + extension

        cmd = ['ffmpeg', '-i', old_filename, '-sameq', new_filename]
        ffmpeg = subprocess.Popen(cmd,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE)
        stdout, stderr = ffmpeg.communicate()

        if ffmpeg.returncode == 0:
            util.rename_episode_file(episode, new_filename)
            os.remove(old_filename)

            logger.info('Converted M4A file.')
            gpodder.user_extensions.on_notification_show(
                _('File converted'), episode.title)
        else:
            logger.warn('Error converting file: %s / %s', stdout, stderr)
            gpodder.user_extensions.on_notification_show(
                _('Conversion failed'), episode.title)
    def _convert_episode(self, episode):
        if not self._check_source(episode):
            return

        old_filename = episode.local_filename(create=False)
        filename, old_extension = os.path.splitext(old_filename)
        new_filename = filename + '.fast' + old_extension

        cmd_param = self.CMD[self.command_without_ext][old_extension]
        if self.config.mono:
            cmd_param.extend(self.CMD[self.command_without_ext][old_extension+'-mono'])
        cmd = ['nice', self.command] \
              + [param % {'old_file': old_filename,
                          'new_file': new_filename,
                          'speed': self.config.speed}
                 for param in cmd_param]

        sox = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
        stdout, stderr = sox.communicate()

        if sox.returncode == 0:
            util.rename_episode_file(episode, new_filename)
            os.remove(old_filename)

            logger.info('File re-encoded at %(speed).2fx speed.' % {'speed': self.config.speed})
            gpodder.user_extensions.on_notification_show(_('File converted'), episode.title)
        else:
            logger.warn('sox failed: %s / %s', stdout, stderr)
            gpodder.user_extensions.on_notification_show(_('Speed-up failed'), episode.title)
Пример #9
0
    def _convert_episode(self, episode):
        if not self._check_source(episode):
            return

        new_extension = self._get_new_extension()
        old_filename = episode.local_filename(create=False)
        filename, old_extension = os.path.splitext(old_filename)
        new_filename = filename + new_extension

        cmd_param = self.CMD[self.command_without_ext][new_extension]
        cmd = [self.command] + \
            [param % {'old_file': old_filename, 'new_file': new_filename}
                for param in cmd_param]

        if gpodder.ui.win32:
            ffmpeg = util.Popen(cmd)
            ffmpeg.wait()
            stdout, stderr = ("<unavailable>",) * 2
        else:
            ffmpeg = util.Popen(cmd, stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE)
            stdout, stderr = ffmpeg.communicate()

        if ffmpeg.returncode == 0:
            util.rename_episode_file(episode, new_filename)
            os.remove(old_filename)

            logger.info('Converted audio file to %(format)s.' % {'format': new_extension})
            gpodder.user_extensions.on_notification_show(_('File converted'), episode.title)
        else:
            logger.warn('Error converting audio file: %s / %s', stdout, stderr)
            gpodder.user_extensions.on_notification_show(_('Conversion failed'), episode.title)
Пример #10
0
    def _convert_episode(self, episode):
        old_filename = episode.local_filename(create=False)

        if not self._check_mp4(episode):
            return

        if self.config.use_ogg:
            extension = '.ogg'
        else:
            extension = '.mp3'

        filename, old_extension = os.path.splitext(old_filename)
        new_filename = filename + extension

        cmd = [self.command] + \
            [param % {'old_file': old_filename, 'new_file': new_filename}
                for param in self.command_param]
        ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                stderr=subprocess.PIPE)
        stdout, stderr = ffmpeg.communicate()

        if ffmpeg.returncode == 0:
            util.rename_episode_file(episode, new_filename)
            os.remove(old_filename)
            
            logger.info('Converted M4A file.')
            gpodder.user_extensions.on_notification_show(_('File converted'), episode.title)
        else:
            logger.warn('Error converting file: %s / %s', stdout, stderr)
            gpodder.user_extensions.on_notification_show(_('Conversion failed'), episode.title)
Пример #11
0
    def on_episode_downloaded(self, episode):
        current_filename = episode.local_filename(False)
        converted_filename = self._convert_mp4(episode, current_filename)

        if converted_filename is not None:
            util.rename_episode_file(episode, converted_filename)
            os.remove(current_filename)
            logger.info('Conversion for %s was successfully' % current_filename)
            gpodder.user_extensions.on_notification_show(_('File converted'), episode.title)
Пример #12
0
    def on_episode_downloaded(self, episode):
        current_filename = episode.local_filename(create=False)

        new_filename = self.make_filename(current_filename, episode.title, episode.sortdate + '-')

        if new_filename != current_filename:
            logger.info('Renaming: %s -> %s', current_filename, new_filename)
            os.rename(current_filename, new_filename)
            util.rename_episode_file(episode, new_filename)
Пример #13
0
    def on_episode_downloaded(self, episode):
        current_filename = episode.local_filename(False)
        converted_filename = self._convert_mp4(episode, current_filename)

        if converted_filename is not None:
            util.rename_episode_file(episode, converted_filename)
            os.remove(current_filename)
            logger.info('Conversion for %s was successfully' % current_filename)
            gpodder.user_extensions.on_notification_show(_('File converted'), episode.title)
Пример #14
0
    def on_episode_downloaded(self, episode):
        current_filename = episode.local_filename(create=False)

        new_filename = self.make_filename(current_filename, episode.title)

        if new_filename != current_filename:
            logger.info('Renaming: %s -> %s', current_filename, new_filename)
            os.rename(current_filename, new_filename)
            util.rename_episode_file(episode, new_filename)
Пример #15
0
    def convert_episode(self, episode):
        if episode.mime_type not in self.MIME_TYPES:
            return

        old_filename = episode.local_filename(create=False)
        filename, old_extension = os.path.splitext(old_filename)
        new_filename = filename + self.TARGET_EXT

        cmd = [self.command] + \
            [param % {'old_file': old_filename, 'new_file': new_filename}
                for param in self.command_param]
        ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                stderr=subprocess.PIPE)
        stdout, stderr = ffmpeg.communicate()

        if ffmpeg.returncode == 0:            
            util.rename_episode_file(episode, new_filename)
            os.remove(old_filename)
            
            logger.info('Converted OGG file to MP3.')
            gpodder.user_extensions.on_notification_show(_('File converted from ogg to mp3'), episode.title)
        else:
            logger.warn('Error converting file from ogg to mp3: %s / %s', stdout, stderr)
            gpodder.user_extensions.on_notification_show(_('Conversion failed from ogg to mp3'), episode.title)
Пример #16
0
    def _convert_episode(self, episode):
        old_filename = episode.local_filename(create=False)
        filename, ext = os.path.splitext(old_filename)
        new_filename = filename + '.mp4'

        if open(old_filename, 'rb').read(3) != 'FLV':
            logger.debug('Not a FLV file. Ignoring.')
            return

        if ext.lower() == '.mp4':
            # Move file out of place for conversion
            tmp_filename = filename + '.flv'
            os.rename(old_filename, tmp_filename)
            old_filename = tmp_filename

        cmd = [
            'ffmpeg', '-i', old_filename, '-vcodec', 'copy', '-acodec', 'copy',
            new_filename
        ]

        ffmpeg = subprocess.Popen(cmd,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE)
        stdout, stderr = ffmpeg.communicate()

        if ffmpeg.returncode == 0:
            util.rename_episode_file(episode, new_filename)
            os.remove(old_filename)

            logger.info('FLV conversion successful.')
            gpodder.user_extensions.on_notification_show(
                _('File converted'), episode.title)
        else:
            logger.warn('Error converting file: %s / %s', stdout, stderr)
            gpodder.user_extensions.on_notification_show(
                _('Conversion failed'), episode.title)
Пример #17
0
    def _convert_episode(self, episode):
        if not self._check_source(episode):
            return

        new_extension = self._get_new_extension()
        old_filename = episode.local_filename(create=False)
        filename, old_extension = os.path.splitext(old_filename)
        new_filename = filename + new_extension

        cmd_param = self.CMD[self.command_without_ext][new_extension]
        cmd = [self.command] + [param % {"old_file": old_filename, "new_file": new_filename} for param in cmd_param]

        ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = ffmpeg.communicate()

        if ffmpeg.returncode == 0:
            util.rename_episode_file(episode, new_filename)
            os.remove(old_filename)

            logger.info("Converted audio file to %(format)s." % {"format": new_extension})
            gpodder.user_extensions.on_notification_show(_("File converted"), episode.title)
        else:
            logger.warn("Error converting audio file: %s / %s", stdout, stderr)
            gpodder.user_extensions.on_notification_show(_("Conversion failed"), episode.title)
Пример #18
0
class gPodderExtension:
    MIME_TYPES = ('audio/x-m4a', 'audio/mp4', 'audio/mp4a-latm', 'audio/mpeg', 'audio/ogg', 'audio/opus')
    EXT = ('.m4a', '.ogg', '.opus', '.mp3')
    CMD = {'avconv': {'.mp3': ['-n', '-i', '%(old_file)s', '-q:a', '2', '-id3v2_version', '3', '-write_id3v1', '1', '%(new_file)s'],
                      '.ogg': ['-n', '-i', '%(old_file)s', '-q:a', '2', '%(new_file)s'],
                      '.opus': ['-n', '-i', '%(old_file)s', '-b:a', '64k', '%(new_file)s']
                      },
           'ffmpeg': {'.mp3': ['-n', '-i', '%(old_file)s', '-q:a', '2', '-id3v2_version', '3', '-write_id3v1', '1', '%(new_file)s'],
                      '.ogg': ['-n', '-i', '%(old_file)s', '-q:a', '2', '%(new_file)s'],
                      '.opus': ['-n', '-i', '%(old_file)s', '-b:a', '64k', '%(new_file)s']
                      }
           }

    def __init__(self, container):
        self.container = container
        self.config = self.container.config

        # Dependency checks
        self.command = self.container.require_any_command(['avconv', 'ffmpeg'])

        # extract command without extension (.exe on Windows) from command-string
        self.command_without_ext = os.path.basename(os.path.splitext(self.command)[0])

    def on_episode_downloaded(self, episode):
        self._convert_episode(episode)

    def _get_new_extension(self):
        if self.config.use_ogg:
            extension = '.ogg'
        elif self.config.use_opus:
            extension = '.opus'
        else:
            extension = '.mp3'
        return extension

    def _check_source(self, episode):
        if episode.extension() == self._get_new_extension():
            return False

        if episode.mime_type in self.MIME_TYPES:
            return True

        # Also check file extension (bug 1770)
        if episode.extension() in self.EXT:
            return True

        return False

    def on_episodes_context_menu(self, episodes):
        if not self.config.context_menu:
            return None

        if not all(e.was_downloaded(and_exists=True) for e in episodes):
            return None

        if not any(self._check_source(episode) for episode in episodes):
            return None

        menu_item = _('Convert to %(format)s') % {'format': self._target_format()}

        return [(menu_item, self._convert_episodes)]

    def _target_format(self):
        if self.config.use_ogg:
            target_format = 'OGG'
        elif self.config.use_opus:
            target_format = 'OPUS'
        else:
            target_format = 'MP3'
        return target_format

    def _convert_episode(self, episode):
        if not self._check_source(episode):
            return

        new_extension = self._get_new_extension()
        old_filename = episode.local_filename(create=False)
        filename, old_extension = os.path.splitext(old_filename)
        new_filename = filename + new_extension

        cmd_param = self.CMD[self.command_without_ext][new_extension]
        cmd = [self.command] + \
            [param % {'old_file': old_filename, 'new_file': new_filename}
                for param in cmd_param]

            ffmpeg = util.Popen(cmd, stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE)
            stdout, stderr = ffmpeg.communicate()

        if ffmpeg.returncode == 0:
            util.rename_episode_file(episode, new_filename)
            os.remove(old_filename)

            logger.info('Converted audio file to %(format)s.' % {'format': new_extension})
            gpodder.user_extensions.on_notification_show(_('File converted'), episode.title)
        else:
            logger.warn('Error converting audio file: %s / %s', stdout, stderr)
            gpodder.user_extensions.on_notification_show(_('Conversion failed'), episode.title)