Esempio n. 1
0
    def __init__(self, name, encoderArguments, destdir=None, ffmpegbin=None):
        """
        Args:
        name: Name of this quality (Used in path names)
        encoderArguments: :class:`configobj.ConfigObj` with valid ffmpeg options. This will be passed to a new :class:`FFMpeg` object.
        destdir: The location where files will be saved.
        ffmpegbin: Path to ffmpeg binary.
        """
        super(HTTPLiveVariantStream, self).__init__(name=name)

        self.name = name
        """Name of this variant"""

        self.encoder = None
        """The :class:`FFMpeg` object used for encoding"""

        self.segmenter = None
        """The :class:`HTTPLiveSegmenter` object used for segmenting"""

        self.destinationDirectory = None
        self.setDestdir(destdir)
        # Set up the encoder
        if ffmpegbin:
            self.encoder = FFMpeg(ffmpegbin=ffmpegbin,
                                  encoderArguments=encoderArguments)
        else:
            self.encoder = FFMpeg(encoderArguments=encoderArguments)

        self.segmenter = HTTPLiveSegmenter(name=self.name + "_segmenter",
                                           destdir=self.destinationDirectory)

        # Hook everything up
        self.addOutlet(self.encoder)
        self.encoder.addOutlet(self.segmenter)
Esempio n. 2
0
    def __init__(self, ffmpeg_path=None, ffprobe_path=None):
        """
        Initialize a new Converter object.
        """

        self.ffmpeg = FFMpeg(ffmpeg_path=ffmpeg_path,
                             ffprobe_path=ffprobe_path)
        self.video_codecs = {}
        self.audio_codecs = {}
        self.subtitle_codecs = {}
        self.formats = {}

        for cls in audio_codec_list:
            name = cls.codec_name
            self.audio_codecs[name] = cls

        for cls in video_codec_list:
            name = cls.codec_name
            self.video_codecs[name] = cls

        for cls in subtitle_codec_list:
            name = cls.codec_name
            self.subtitle_codecs[name] = cls

        for cls in format_list:
            name = cls.format_name
            self.formats[name] = cls
Esempio n. 3
0
    def __init__(self,name,encoderArguments,destdir=None,ffmpegbin=None):
        """
        Args:
        name: Name of this quality (Used in path names)
        encoderArguments: :class:`configobj.ConfigObj` with valid ffmpeg options. This will be passed to a new :class:`FFMpeg` object.
        destdir: The location where files will be saved.
        ffmpegbin: Path to ffmpeg binary.
        """
        super(HTTPLiveVariantStream,self).__init__(name=name)

        self.name = name
        """Name of this variant"""

        self.encoder = None
        """The :class:`FFMpeg` object used for encoding"""

        self.segmenter = None
        """The :class:`HTTPLiveSegmenter` object used for segmenting"""

        self.destinationDirectory = None
        self.setDestdir(destdir)
        # Set up the encoder
        if ffmpegbin:
            self.encoder = FFMpeg(name=name, ffmpegbin=ffmpegbin,encoderArguments=encoderArguments)
        else:
            self.encoder = FFMpeg(name=name, encoderArguments=encoderArguments)

        self.segmenter = HTTPLiveSegmenter(name=self.name+"_segmenter",destdir=self.destinationDirectory)

        # Hook everything up
        self.addOutlet(self.encoder)
        self.encoder.addOutlet(self.segmenter)
Esempio n. 4
0
    def test_ffmpeg_live_output(self):
        f = FFMpeg()
        p = Popen('ffmpeg', stderr=PIPE, stdin=PIPE, stdout=PIPE)
        generator = f._ffmpeg_live_output(p)
        for line in generator:
            first_line = line
            break

        self.assertIn('ffmpeg version 3.2', first_line)
Esempio n. 5
0
 def test_get_media_type(self):
     # instantiate the class
     f = FFMpeg()
     self.assertEqual(f.get_media_type('test.mp4'), {
         'audio': True,
         'video': True
     })
     self.assertEqual(f.get_media_type('test.aac'), {'audio': True})
     self.assertEqual(f.get_media_type('noaudio.mp4'), {'video': True})
Esempio n. 6
0
 def start(self, request):
     super().start()
     inst = FFMpeg(self, request.user_agent, codes_folder, avc_table,
                   hevc_table)
     inst.process(request)
     #TODO: Should the media itself keep track of the instances it has started?
     #Probably
     #Later
     #This would require us to remove this start and ask the MediaDirectory class to start a file
     return inst
Esempio n. 7
0
    def test_add_audio_to_video(self):
        # test if method can add an audio stream
        # to a video file which does not have one
        f = FFMpeg(verbose=False)
        v = VideoFFMpeg(verbose=False)
        v.add_audio_to_video('noaudio.mp4', 'gotaudio.mp4', 'test.aac')
        # get the second stream
        stream = f.probe('gotaudio.mp4').streams[1]

        self.assertEqual(stream.codec.codec_type, 'audio')

        # unlink the output video file
        if os.path.exists('gotaudio.mp4'):
            os.unlink('gotaudio.mp4')
Esempio n. 8
0
 def test_parse_codec_line(self):
     f = FFMpeg()
     features, codec_name, desc = ['DES...', 'xsub', 'XSUB']
     codec = f._parse_codec_line(features, codec_name, desc)
     _codec = {
         'xsub': {
             'encoding': True,
             'codec_type': 'subtitle',
             'description': 'XSUB',
             'decoding': True
         }
     }
     self.assertIsInstance(codec, dict)
     self.assertDictEqual(codec, _codec)
    def __init__(self, ffmpeg_path=None, ffprobe_path=None):
        """
        Initialize a new Converter object.
        """

        self.ffmpeg = FFMpeg(ffmpeg_path=ffmpeg_path,
            ffprobe_path=ffprobe_path)
        self.video_codecs = {}
        self.audio_codecs = {}
        self.subtitle_codecs = {}
        self.formats = {}

        for cls in audio_codec_list:
            name = cls.codec_name
            self.audio_codecs[name] = cls

        for cls in video_codec_list:
            name = cls.codec_name
            self.video_codecs[name] = cls
            
        for cls in subtitle_codec_list:
            name = cls.codec_name
            self.subtitle_codecs[name] = cls

        for cls in format_list:
            name = cls.format_name
            self.formats[name] = cls
Esempio n. 10
0
 def test_list_codecs(self):
     f = FFMpeg()
     codecs = f.list_codecs()
     codec = codecs[0]
     _codec = {
         '012v': {
             'codec_type': 'video',
             'decoding': True,
             'description': 'Uncompressed 4:2:2 10-bit',
             'intraframe_only': True
         }
     }
     self.assertIsInstance(codecs, list)
     self.assertIsInstance(codec, dict)
     self.assertEqual(414, len(codecs))
     self.assertDictEqual(codec, _codec)
Esempio n. 11
0
    def test_ffmpeg_probe(self):
        # instantiate the FFMpeg object
        f = FFMpeg()
        self.assertEqual(f.probe('nonexistent'), None)
        self.assertEqual(f.probe(''), None)
        info = f.probe('test.mp4')
        format_info = info.format_info
        streams = info.streams
        self.assertEqual(len(streams), 2)
        self.assertEqual(format_info.get('nb_streams'), '2')
        self.assertEqual(format_info.get('nb_programs'), '0')
        self.assertEqual(format_info.get('bit_rate'), '1452696')
        self.assertAlmostEqual(float(format_info.get('duration')),
                               293.63,
                               places=0)

        # get the video stream and create tests for it
        v_stream = streams[0]
        self.assertEqual(v_stream.height, '720')
        self.assertEqual(v_stream.width, '1280')
        self.assertEqual(v_stream.codec.codec_type, 'video')
        self.assertEqual(v_stream.codec.codec_name, 'h264')
        self.assertEqual(v_stream.codec.codec_tag_string, 'avc1')
        self.assertEqual(v_stream.codec.codec_tag, '0x31637661')

        # get the audio stream and create tests for it
        a_stream = streams[1]
        self.assertEqual(a_stream.height, None)
        self.assertEqual(a_stream.width, None)
        self.assertAlmostEqual(float(a_stream.duration), 293.63, places=0)
        self.assertEqual(a_stream.codec.codec_long_name,
                         'AAC (Advanced Audio Coding)')
        self.assertEqual(a_stream.codec.codec_name, 'aac')
        self.assertEqual(a_stream.codec.codec_type, 'audio')
        self.assertEqual(a_stream.codec.codec_tag_string, 'mp4a')
        self.assertEqual(a_stream.codec.codec_tag, '0x6134706d')
Esempio n. 12
0
    def test_get_extension(self):
        f = FFMpeg()
        audio_extension = f._get_extension('test.aac', 'audio')
        mp3_extension = f._get_extension('test.mp3', 'audio')
        video_extension = f._get_extension('test.mp4', 'video')
        avi_extension = f._get_extension('test.avi', 'video')

        self.assertEqual(audio_extension, 'aac')
        self.assertEqual(mp3_extension, 'mp3')
        self.assertEqual(video_extension, 'mp4')
        self.assertEqual(avi_extension, 'avi')
Esempio n. 13
0
    def run(self):
        inputfile = self.inputEdit.displayText()
        for index, segment in enumerate(self.segments):
            timestamps = {}
            filenames = {}

            filenames['input'] = inputfile
            filenames['output'] = segment['name'].displayText()

            try:
                timestamps['start'] = utils.parse_time(
                    segment['start'].displayText())
                timestamps['end'] = utils.parse_time(
                    segment['end'].displayText())
            except ValueError:
                QMessageBox.about(self, 'Error',
                                  'Segment %d is invalid.' % (index + 1))
                return

            FFMpeg(filenames, timestamps, path=self.ffmpeg_path).run()
Esempio n. 14
0
class Converter(object):
    """
    Converter class, encapsulates formats and codecs.

    >>> c = Converter()
    """
    def __init__(self, ffmpeg_path=None, ffprobe_path=None):
        """
        Initialize a new Converter object.
        """

        self.ffmpeg = FFMpeg(ffmpeg_path=ffmpeg_path,
                             ffprobe_path=ffprobe_path)
        self.video_codecs = {}
        self.audio_codecs = {}
        self.formats = {}

        for cls in audio_codec_list:
            name = cls.codec_name
            self.audio_codecs[name] = cls

        for cls in video_codec_list:
            name = cls.codec_name
            self.video_codecs[name] = cls

        for cls in format_list:
            name = cls.format_name
            self.formats[name] = cls

    def parse_options(self, opt, twopass=None):
        """
        Parse format/codec options and prepare raw ffmpeg option list.
        """
        format_options = None
        audio_options = []
        video_options = []

        if not isinstance(opt, dict):
            raise ConverterError('Invalid output specification')

        if 'format' not in opt:
            raise ConverterError('Format not specified')

        f = opt['format']
        if f not in self.formats:
            raise ConverterError('Requested unknown format: ' + str(f))

        format_options = self.formats[f]().parse_options(opt)
        if format_options is None:
            raise ConverterError('Unknown container format error')

        if 'audio' not in opt and 'video' not in opt:
            raise ConverterError('Neither audio nor video streams requested')

        if 'audio' not in opt or twopass == 1:
            opt['audio'] = {'codec': None}

        if 'video' not in opt:
            opt['video'] = {'codec': None}

        if 'audio' in opt:
            x = opt['audio']

            if not isinstance(x, dict) or 'codec' not in x:
                raise ConverterError('Invalid audio codec specification')

            c = x['codec']
            if c not in self.audio_codecs:
                raise ConverterError(
                    'Requested unknown audio codec %s. Audio Supported %s' %
                    (str(c), unicode(self.audio_codecs)))

            audio_options = self.audio_codecs[c]().parse_options(x)
            if audio_options is None:
                raise ConverterError('Unknown audio codec error')

        if 'video' in opt:
            x = opt['video']
            if not isinstance(x, dict) or 'codec' not in x:
                raise ConverterError('Invalid video codec specification')

            c = x['codec']
            if c not in self.video_codecs:
                raise ConverterError(
                    'Requested unknown video codec  %s. Video Supported %s' %
                    (str(c), unicode(self.video_codecs)))

            video_options = self.video_codecs[c]().parse_options(x)
            if video_options is None:
                raise ConverterError('Unknown video codec error')

        optlist = audio_options + video_options + format_options

        if twopass == 1:
            optlist.extend(['-pass', '1'])
        elif twopass == 2:
            optlist.extend(['-pass', '2'])

        return optlist

    def convert(self, infile, outfile, options, twopass=False):
        """
        Convert media file (infile) according to specified options, and
        save it to outfile. For two-pass encoding, specify the pass (1 or 2)
        in the twopass parameter.

        Options should be passed as a dictionary. The keys are:
            * format (mandatory, string) - container format; see
              formats.BaseFormat for list of supported formats
            * audio (optional, dict) - audio codec and options; see
              avcodecs.AudioCodec for list of supported options
            * video (optional, dict) - video codec and options; see
              avcodecs.VideoCodec for list of supported options

        Multiple audio/video streams are not supported. The output has to
        have at least an audio or a video stream (or both).

        Convert returns a generator that needs to be iterated to drive the
        conversion process. The generator will periodically yield timecode
        of currently processed part of the file (ie. at which second in the
        content is the conversion process currently).

        >>> conv = c.convert('test1.ogg', '/tmp/output.mkv', {
        ...    'format': 'mkv',
        ...    'audio': { 'codec': 'aac' },
        ...    'video': { 'codec': 'h264' }
        ... })

        >>> for timecode in conv:
        ...   pass # can be used to inform the user about the progress
        """

        if not isinstance(options, dict):
            raise ConverterError('Invalid options')

        if not os.path.exists(infile):
            raise ConverterError("Source file doesn't exist: " + infile)

        info = self.ffmpeg.probe(infile)
        if info is None:
            raise ConverterError("Can't get information about source file")

        if not info.video and not info.audio:
            raise ConverterError('Source file has no audio or video streams')

        if info.video and 'video' in options:
            v = options['video']
            v['src_width'] = info.video.video_width
            v['src_height'] = info.video.video_height

        if info.format.duration < 0.01:
            raise ConverterError('Zero-length media')

        if twopass:
            optlist1 = self.parse_options(options, 1)
            for timecode in self.ffmpeg.convert(infile, outfile, optlist1):
                yield int((50.0 * timecode) / info.format.duration)

            optlist2 = self.parse_options(options, 2)
            for timecode in self.ffmpeg.convert(infile, outfile, optlist2):
                yield int(50.0 + (50.0 * timecode) / info.format.duration)
        else:
            optlist = self.parse_options(options, twopass)
            for timecode in self.ffmpeg.convert(infile, outfile, optlist):
                yield int((100.0 * timecode) / info.format.duration)

    def probe(self, fname):
        """
        Examine the media file. See the documentation of
        converter.FFMpeg.probe() for details.
        """
        return self.ffmpeg.probe(fname)

    def thumbnail(self, fname, time, outfile, size=None):
        """
        Create a thumbnail of the media file. See the documentation of
        converter.FFMpeg.thumbnail() for details.
        """
        return self.ffmpeg.thumbnail(fname, time, outfile, size)
Esempio n. 15
0
 def test_ffmpeg_binary_paths(self):
     f = FFMpeg()
     self.assertIsNotNone(f.ffmpeg_path)
     self.assertIsNotNone(f.ffprobe_path)
Esempio n. 16
0
class Converter(object):
    """
    Converter class, encapsulates formats and codecs.

    >>> c = Converter()
    """

    def __init__(self, ffmpeg_path=None, ffprobe_path=None):
        """Initialize a new Converter object."""
        self.ffmpeg = FFMpeg(ffmpeg_path=ffmpeg_path, ffprobe_path=ffprobe_path)
        self.video_codecs = {}
        self.audio_codecs = {}
        self.subtitle_codecs = {}
        self.formats = {}

        for cls in codec_lists["audio"]:
            name = cls.codec_name
            self.audio_codecs[name] = cls

        for cls in codec_lists["video"]:
            name = cls.codec_name
            self.video_codecs[name] = cls

        for cls in codec_lists["subtitle"]:
            name = cls.codec_name
            self.subtitle_codecs[name] = cls

        for cls in format_list:
            name = cls.format_name
            self.formats[name] = cls

    def parse_options(self, opt, twopass=None):
        """Parse format/codec options and prepare raw ffmpeg option list."""
        if not isinstance(opt, dict):
            raise ConverterError("Invalid output specification")

        if "format" not in opt:
            raise ConverterError("Format not specified")

        f = opt["format"]
        if f not in self.formats:
            raise ConverterError("Requested unknown format: " + str(f))

        format_options = self.formats[f]().parse_options(opt)
        if format_options is None:
            raise ConverterError("Unknown container format error")

        if "audio" not in opt and "video" not in opt:
            raise ConverterError("Neither audio nor video streams requested")

        # audio options
        if "audio" not in opt or twopass == 1:
            opt_audio = {"codec": None}
        else:
            opt_audio = opt["audio"]
            if not isinstance(opt_audio, dict) or "codec" not in opt_audio:
                raise ConverterError("Invalid audio codec specification")

        c = opt_audio["codec"]
        if c not in self.audio_codecs:
            raise ConverterError("Requested unknown audio codec " + str(c))

        audio_options = self.audio_codecs[c]().parse_options(opt_audio)
        if audio_options is None:
            raise ConverterError("Unknown audio codec error")

        # video options
        if "video" not in opt:
            opt_video = {"codec": None}
        else:
            opt_video = opt["video"]
            if not isinstance(opt_video, dict) or "codec" not in opt_video:
                raise ConverterError("Invalid video codec specification")

        c = opt_video["codec"]
        if c not in self.video_codecs:
            raise ConverterError("Requested unknown video codec " + str(c))

        video_options = self.video_codecs[c]().parse_options(opt_video)
        if video_options is None:
            raise ConverterError("Unknown video codec error")

        if "subtitle" not in opt:
            opt_subtitle = {"codec": None}
        else:
            opt_subtitle = opt["subtitle"]
            if not isinstance(opt_subtitle, dict) or "codec" not in opt_subtitle:
                raise ConverterError("Invalid subtitle codec specification")

        c = opt_subtitle["codec"]
        if c not in self.subtitle_codecs:
            raise ConverterError("Requested unknown subtitle codec " + str(c))

        subtitle_options = self.subtitle_codecs[c]().parse_options(opt_subtitle)
        if subtitle_options is None:
            raise ConverterError("Unknown subtitle codec error")

        if "map" in opt:
            m = opt["map"]
            if not isinstance(m, int):
                raise ConverterError("map needs to be int")
            else:
                format_options.extend(["-map", str(m)])

        # aggregate all options
        optlist = audio_options + video_options + subtitle_options + format_options

        if twopass == 1:
            optlist.extend(["-pass", "1"])
        elif twopass == 2:
            optlist.extend(["-pass", "2"])

        return optlist

    def convert(self, infile, outfile, options, twopass=False, timeout=10):
        """
        Convert media file (infile) according to specified options, and save it to outfile. For two-pass encoding, specify the pass (1 or 2) in the twopass parameter.

        Options should be passed as a dictionary. The keys are:
            * format (mandatory, string) - container format; see
              formats.BaseFormat for list of supported formats
            * audio (optional, dict) - audio codec and options; see
              codecs.audio.AudioCodec for list of supported options
            * video (optional, dict) - video codec and options; see
              codecs.video.VideoCodec for list of supported options
            * map (optional, int) - can be used to map all content of stream 0

        Multiple audio/video streams are not supported. The output has to
        have at least an audio or a video stream (or both).

        Convert returns a generator that needs to be iterated to drive the
        conversion process. The generator will periodically yield timecode
        of currently processed part of the file (ie. at which second in the
        content is the conversion process currently).

        The optional timeout argument specifies how long should the operation
        be blocked in case ffmpeg gets stuck and doesn't report back. This
        doesn't limit the total conversion time, just the amount of time
        Converter will wait for each update from ffmpeg. As it's usually
        less than a second, the default of 10 is a reasonable default. To
        disable the timeout, set it to None. You may need to do this if
        using Converter in a threading environment, since the way the
        timeout is handled (using signals) has special restriction when
        using threads.

        >>> conv = Converter().convert('test1.ogg', '/tmp/output.mkv', {
        ...    'format': 'mkv',
        ...    'audio': { 'codec': 'aac' },
        ...    'video': { 'codec': 'h264' }
        ... })

        >>> for timecode in conv:
        ...   pass # can be used to inform the user about the progress
        """
        if not isinstance(options, dict):
            raise ConverterError("Invalid options")

        if not os.path.exists(infile):
            raise ConverterError("Source file doesn't exist: " + infile)

        info = self.ffmpeg.probe(infile)
        if info is None:
            raise ConverterError("Can't get information about source file")

        if not info.video and not info.audio:
            raise ConverterError("Source file has no audio or video streams")

        if info.video and "video" in options:
            options = options.copy()
            v = options["video"] = options["video"].copy()
            v["src_width"] = info.video.video_width
            v["src_height"] = info.video.video_height

        if info.format.duration < 0.01:
            raise ConverterError("Zero-length media")

        if twopass:
            optlist1 = self.parse_options(options, 1)
            for timecode in self.ffmpeg.convert(infile, outfile, optlist1, timeout=timeout):
                yield float(timecode) / info.format.duration

            optlist2 = self.parse_options(options, 2)
            for timecode in self.ffmpeg.convert(infile, outfile, optlist2, timeout=timeout):
                yield 0.5 + float(timecode) / info.format.duration
        else:
            optlist = self.parse_options(options, twopass)
            for timecode in self.ffmpeg.convert(infile, outfile, optlist, timeout=timeout):
                yield float(timecode) / info.format.duration

    def segment(self, infile, working_directory, output_file, output_directory, timeout=10):
        if not os.path.exists(infile):
            raise ConverterError("Source file doesn't exist: " + infile)

        info = self.ffmpeg.probe(infile)
        if info is None:
            raise ConverterError("Can't get information about source file")

        if not info.video and not info.audio:
            raise ConverterError("Source file has no audio or video streams")

        try:
            os.makedirs(os.path.join(working_directory, output_directory))
        except Exception as e:
            if e.errno != errno.EEXIST:
                raise e
        current_directory = os.getcwd()
        os.chdir(working_directory)
        optlist = [
            "-flags",
            "-global_header",
            "-f",
            "segment",
            "-segment_time",
            "1",
            "-segment_list",
            output_file,
            "-segment_list_type",
            "m3u8",
            "-segment_format",
            "mpegts",
            "-segment_list_entry_prefix",
            "%s/" % output_directory,
            "-map",
            "0",
            "-map",
            "-0:d",
            "-bsf",
            "h264_mp4toannexb",
            "-vcodec",
            "copy",
            "-acodec",
            "copy",
        ]
        outfile = "%s/media%%05d.ts" % output_directory
        for timecode in self.ffmpeg.convert(infile, outfile, optlist, timeout=timeout):
            yield int((100.0 * timecode) / info.format.duration)
        os.chdir(current_directory)

    def probe(self, fname, posters_as_video=True):
        """
        Examine the media file.

        See the documentation of converter.FFMpeg.probe() for details.

        :param posters_as_video: Take poster images (mainly for audio files) as
            A video stream, defaults to True
        """
        return self.ffmpeg.probe(fname, posters_as_video)

    def thumbnail(self, fname, time, outfile, size=None, quality=FFMpeg.DEFAULT_JPEG_QUALITY):
        """
        Create a thumbnail of the media file.

        See the documentation of converter.FFMpeg.thumbnail() for details.
        """
        return self.ffmpeg.thumbnail(fname, time, outfile, size, quality)

    def thumbnails(self, fname, option_list):
        """
        Create one or more thumbnail of the media file.

        See the documentation of converter.FFMpeg.thumbnails() for details.
        """
        return self.ffmpeg.thumbnails(fname, option_list)
class Converter(object):
    """
    Converter class, encapsulates formats and codecs.

    >>> c = Converter()
    """

    def __init__(self, ffmpeg_path=None, ffprobe_path=None):
        """
        Initialize a new Converter object.
        """

        self.ffmpeg = FFMpeg(ffmpeg_path=ffmpeg_path,
            ffprobe_path=ffprobe_path)
        self.video_codecs = {}
        self.audio_codecs = {}
        self.subtitle_codecs = {}
        self.formats = {}

        for cls in audio_codec_list:
            name = cls.codec_name
            self.audio_codecs[name] = cls

        for cls in video_codec_list:
            name = cls.codec_name
            self.video_codecs[name] = cls
            
        for cls in subtitle_codec_list:
            name = cls.codec_name
            self.subtitle_codecs[name] = cls

        for cls in format_list:
            name = cls.format_name
            self.formats[name] = cls

    def parse_options(self, opt, twopass=None):
        """
        Parse format/codec options and prepare raw ffmpeg option list.
        """
        format_options = None
        audio_options = []
        video_options = []
        subtitle_options = []

        if not isinstance(opt, dict):
            raise ConverterError('Invalid output specification')

        if 'format' not in opt:
            raise ConverterError('Format not specified')

        f = opt['format']
        if f not in self.formats:
            raise ConverterError('Requested unknown format: ' + str(f))

        format_options = self.formats[f]().parse_options(opt)
        if format_options is None:
            raise ConverterError('Unknown container format error')

        if 'audio' not in opt and 'video' not in opt and 'subtitle' not in opt:
            raise ConverterError('Neither audio nor video nor subtitle streams requested')

        if 'audio' in opt:
            y = opt['audio']

            # Creates the new nested dictionary to preserve backwards compatability
            try:
                first = y.values()[0]
                if not isinstance(first, dict) and first is not None:
                    y = {0: y}
            except IndexError:
                pass

            for n in y:
                x = y[n]

                if not isinstance(x, dict) or 'codec' not in x:
                    raise ConverterError('Invalid audio codec specification')

                if 'map' not in x:
                    raise ConverterError('Must specify a map value')

                c = x['codec']
                if c not in self.audio_codecs:
                    raise ConverterError('Requested unknown audio codec ' + str(c))

                audio_options.extend(self.audio_codecs[c]().parse_options(x, n))
                if audio_options is None:
                    raise ConverterError('Unknown audio codec error')

        if 'subtitle' in opt:
            y = opt['subtitle']

            # Creates the new nested dictionary to preserve backwards compatability
            try:
                first = y.values()[0]
                if not isinstance(first, dict) and first is not None:
                    y = {0: y}
            except IndexError:
                pass

            for n in y:
                x = y[n]
                if not isinstance(x, dict) or 'codec' not in x:
                    raise ConverterError('Invalid subtitle codec specification')

                if 'map' not in x:
                    raise ConverterError('Must specify a map value')

                if 'path' in x and 'source' not in x:
                    raise ConverterError('Cannot specify subtitle path without FFMPEG source number')

                if 'source' in x and 'path' not in x:
                    raise ConverterError('Cannot specify alternate input source without a path')

                c = x['codec']
                if c not in self.subtitle_codecs:
                    raise ConverterError('Requested unknown audio codec ' + str(c))

                subtitle_options.extend(self.subtitle_codecs[c]().parse_options(x, n))
                if audio_options is None:
                    raise ConverterError('Unknown audio codec error')

        if 'video' in opt:
            x = opt['video']
            if not isinstance(x, dict) or 'codec' not in x:
                raise ConverterError('Invalid video codec specification')

            if 'map' not in x:
                raise ConverterError('Must specify a map value')

            c = x['codec']
            if c not in self.video_codecs:
                raise ConverterError('Requested unknown video codec ' + str(c))

            video_options = self.video_codecs[c]().parse_options(x)
            if video_options is None:
                raise ConverterError('Unknown video codec error')

        optlist = video_options + audio_options + subtitle_options + format_options

        if twopass == 1:
            optlist.extend(['-pass', '1'])
        elif twopass == 2:
            optlist.extend(['-pass', '2'])

        return optlist

    def convert(self, infile, outfile, options, twopass=False, timeout=10):
        """
        Convert media file (infile) according to specified options, and
        save it to outfile. For two-pass encoding, specify the pass (1 or 2)
        in the twopass parameter.

        Options should be passed as a dictionary. The keys are:
            * format (mandatory, string) - container format; see
              formats.BaseFormat for list of supported formats
            * audio (optional, dict) - audio codec and options; see
              avcodecs.AudioCodec for list of supported options
            * video (optional, dict) - video codec and options; see
              avcodecs.VideoCodec for list of supported options

        Multiple audio/video streams are not supported. The output has to
        have at least an audio or a video stream (or both).

        Convert returns a generator that needs to be iterated to drive the
        conversion process. The generator will periodically yield timecode
        of currently processed part of the file (ie. at which second in the
        content is the conversion process currently).

        The optional timeout argument specifies how long should the operation
        be blocked in case ffmpeg gets stuck and doesn't report back. This
        doesn't limit the total conversion time, just the amount of time
        Converter will wait for each update from ffmpeg. As it's usually
        less than a second, the default of 10 is a reasonable default. To
        disable the timeout, set it to None. You may need to do this if
        using Converter in a threading environment, since the way the
        timeout is handled (using signals) has special restriction when
        using threads.

        >>> conv = c.convert('test1.ogg', '/tmp/output.mkv', {
        ...    'format': 'mkv',
        ...    'audio': { 'codec': 'aac' },
        ...    'video': { 'codec': 'h264' }
        ... })

        >>> for timecode in conv:
        ...   pass # can be used to inform the user about the progress
        """

        if not isinstance(options, dict):
            raise ConverterError('Invalid options')

        if not os.path.exists(infile):
            raise ConverterError("Source file doesn't exist: " + infile)

        info = self.ffmpeg.probe(infile)
        if info is None:
            raise ConverterError("Can't get information about source file")

        if not info.video and not info.audio:
            raise ConverterError('Source file has no audio or video streams')

        if info.video and 'video' in options:
            v = options['video']
            v['src_width'] = info.video.video_width
            v['src_height'] = info.video.video_height

        if info.format.duration < 0.01:
            raise ConverterError('Zero-length media')

        if twopass:
            optlist1 = self.parse_options(options, 1)
            for timecode in self.ffmpeg.convert(infile, outfile, optlist,
                    timeout=timeout):
                yield int((50.0 * timecode) / info.format.duration)

            optlist2 = self.parse_options(options, 2)
            for timecode in self.ffmpeg.convert(infile, outfile, optlist2,
                    timeout=timeout):
                yield int(50.0 + (50.0 * timecode) / info.format.duration)
        else:
            optlist = self.parse_options(options, twopass)
            for timecode in self.ffmpeg.convert(infile, outfile, optlist,
                    timeout=timeout):
                yield int((100.0 * timecode) / info.format.duration)

    def probe(self, fname):
        """
        Examine the media file. See the documentation of
        converter.FFMpeg.probe() for details.
        """
        return self.ffmpeg.probe(fname)

    def thumbnail(self, fname, time, outfile, size=None):
        """
        Create a thumbnail of the media file. See the documentation of
        converter.FFMpeg.thumbnail() for details.
        """
        return self.ffmpeg.thumbnail(fname, time, outfile, size)
Esempio n. 18
0
class Converter(object):
    """
    Converter class, encapsulates formats and codecs.

    >>> c = Converter()
    """
    def __init__(self, ffmpeg_path=None, ffprobe_path=None):
        """
        Initialize a new Converter object.
        """

        self.ffmpeg = FFMpeg(ffmpeg_path=ffmpeg_path,
                             ffprobe_path=ffprobe_path)
        self.video_codecs = {}
        self.audio_codecs = {}
        self.subtitle_codecs = {}
        self.formats = {}

        for cls in audio_codec_list:
            name = cls.codec_name
            self.audio_codecs[name] = cls

        for cls in video_codec_list:
            name = cls.codec_name
            self.video_codecs[name] = cls

        for cls in subtitle_codec_list:
            name = cls.codec_name
            self.subtitle_codecs[name] = cls

        for cls in format_list:
            name = cls.format_name
            self.formats[name] = cls

    def parse_options(self, opt, twopass=None):
        """
        Parse format/codec options and prepare raw ffmpeg option list.
        """
        if not isinstance(opt, dict):
            raise ConverterError('Invalid output specification')

        if 'format' not in opt:
            raise ConverterError('Format not specified')

        f = opt['format']
        if f not in self.formats:
            raise ConverterError('Requested unknown format: ' + str(f))

        format_options = self.formats[f]().parse_options(opt)
        if format_options is None:
            raise ConverterError('Unknown container format error')

        if 'audio' not in opt and 'video' not in opt:
            raise ConverterError('Neither audio nor video streams requested')

        # audio options
        if 'audio' not in opt or twopass == 1:
            opt_audio = {'codec': None}
        else:
            opt_audio = opt['audio']
            if not isinstance(opt_audio, dict) or 'codec' not in opt_audio:
                raise ConverterError('Invalid audio codec specification')

        c = opt_audio['codec']
        if c not in self.audio_codecs:
            raise ConverterError('Requested unknown audio codec ' + str(c))

        audio_options = self.audio_codecs[c]().parse_options(opt_audio)
        if audio_options is None:
            raise ConverterError('Unknown audio codec error')

        # video options
        if 'video' not in opt:
            opt_video = {'codec': None}
        else:
            opt_video = opt['video']
            if not isinstance(opt_video, dict) or 'codec' not in opt_video:
                raise ConverterError('Invalid video codec specification')

        c = opt_video['codec']
        if c not in self.video_codecs:
            raise ConverterError('Requested unknown video codec ' + str(c))

        video_options = self.video_codecs[c]().parse_options(opt_video)
        if video_options is None:
            raise ConverterError('Unknown video codec error')

        if 'subtitle' not in opt:
            opt_subtitle = {'codec': None}
        else:
            opt_subtitle = opt['subtitle']
            if not isinstance(opt_subtitle,
                              dict) or 'codec' not in opt_subtitle:
                raise ConverterError('Invalid subtitle codec specification')

        c = opt_subtitle['codec']
        if c not in self.subtitle_codecs:
            raise ConverterError('Requested unknown subtitle codec ' + str(c))

        subtitle_options = self.subtitle_codecs[c]().parse_options(
            opt_subtitle)
        if subtitle_options is None:
            raise ConverterError('Unknown subtitle codec error')

        if 'map' in opt:
            m = opt['map']
            if not type(m) == int:
                raise ConverterError('map needs to be int')
            else:
                format_options.extend(['-map', str(m)])

        # aggregate all options
        optlist = audio_options + video_options + subtitle_options + format_options

        if twopass == 1:
            optlist.extend(['-pass', '1'])
        elif twopass == 2:
            optlist.extend(['-pass', '2'])

        return optlist

    def convert(self, infile, outfile, options, twopass=False, timeout=10):
        """
        Convert media file (infile) according to specified options, and
        save it to outfile. For two-pass encoding, specify the pass (1 or 2)
        in the twopass parameter.

        Options should be passed as a dictionary. The keys are:
            * format (mandatory, string) - container format; see
              formats.BaseFormat for list of supported formats
            * audio (optional, dict) - audio codec and options; see
              avcodecs.AudioCodec for list of supported options
            * video (optional, dict) - video codec and options; see
              avcodecs.VideoCodec for list of supported options
            * map (optional, int) - can be used to map all content of stream 0

        Multiple audio/video streams are not supported. The output has to
        have at least an audio or a video stream (or both).

        Convert returns a generator that needs to be iterated to drive the
        conversion process. The generator will periodically yield timecode
        of currently processed part of the file (ie. at which second in the
        content is the conversion process currently).

        The optional timeout argument specifies how long should the operation
        be blocked in case ffmpeg gets stuck and doesn't report back. This
        doesn't limit the total conversion time, just the amount of time
        Converter will wait for each update from ffmpeg. As it's usually
        less than a second, the default of 10 is a reasonable default. To
        disable the timeout, set it to None. You may need to do this if
        using Converter in a threading environment, since the way the
        timeout is handled (using signals) has special restriction when
        using threads.

        >>> conv = Converter().convert('test1.ogg', '/tmp/output.mkv', {
        ...    'format': 'mkv',
        ...    'audio': { 'codec': 'aac' },
        ...    'video': { 'codec': 'h264' }
        ... })

        >>> for timecode in conv:
        ...   pass # can be used to inform the user about the progress
        """

        if not isinstance(options, dict):
            raise ConverterError('Invalid options')

        if not os.path.exists(infile):
            raise ConverterError("Source file doesn't exist: " + infile)

        info = self.ffmpeg.probe(infile)
        if info is None:
            raise ConverterError("Can't get information about source file")

        if not info.video and not info.audio:
            raise ConverterError('Source file has no audio or video streams')

        if info.video and 'video' in options:
            options = options.copy()
            v = options['video'] = options['video'].copy()
            v['src_width'] = info.video.video_width
            v['src_height'] = info.video.video_height

        if info.format.duration < 0.01:
            raise ConverterError('Zero-length media')

        if twopass:
            optlist1 = self.parse_options(options, 1)
            for timecode in self.ffmpeg.convert(infile,
                                                outfile,
                                                optlist1,
                                                timeout=timeout):
                yield int((50.0 * timecode) / info.format.duration)

            optlist2 = self.parse_options(options, 2)
            for timecode in self.ffmpeg.convert(infile,
                                                outfile,
                                                optlist2,
                                                timeout=timeout):
                yield int(50.0 + (50.0 * timecode) / info.format.duration)
        else:
            optlist = self.parse_options(options, twopass)
            for timecode in self.ffmpeg.convert(infile,
                                                outfile,
                                                optlist,
                                                timeout=timeout):
                yield int((100.0 * timecode) / info.format.duration)

    def probe(self, fname, posters_as_video=True):
        """
        Examine the media file. See the documentation of
        converter.FFMpeg.probe() for details.

        :param posters_as_video: Take poster images (mainly for audio files) as
            A video stream, defaults to True
        """
        return self.ffmpeg.probe(fname, posters_as_video)

    def thumbnail(self,
                  fname,
                  time,
                  outfile,
                  size=None,
                  quality=FFMpeg.DEFAULT_JPEG_QUALITY):
        """
        Create a thumbnail of the media file. See the documentation of
        converter.FFMpeg.thumbnail() for details.
        """
        return self.ffmpeg.thumbnail(fname, time, outfile, size, quality)

    def thumbnails(self, fname, option_list):
        """
        Create one or more thumbnail of the media file. See the documentation
        of converter.FFMpeg.thumbnails() for details.
        """
        return self.ffmpeg.thumbnails(fname, option_list)
Esempio n. 19
0
class Converter(object):
    """
    Converter class, encapsulates formats and codecs.

    >>> c = Converter()
    """
    def __init__(self, ffmpeg_path=None, ffprobe_path=None):
        """
        Initialize a new Converter object.
        """

        self.ffmpeg = FFMpeg(ffmpeg_path=ffmpeg_path,
                             ffprobe_path=ffprobe_path)
        self.video_codecs = {}
        self.audio_codecs = {}
        self.subtitle_codecs = {}
        self.formats = {}

        for cls in audio_codec_list:
            name = cls.codec_name
            self.audio_codecs[name] = cls

        for cls in video_codec_list:
            name = cls.codec_name
            self.video_codecs[name] = cls

        for cls in subtitle_codec_list:
            name = cls.codec_name
            self.subtitle_codecs[name] = cls

        for cls in format_list:
            name = cls.format_name
            self.formats[name] = cls

    def parse_options(self, opt, twopass=None):
        """
        Parse format/codec options and prepare raw ffmpeg option list.
        """
        format_options = None
        audio_options = []
        video_options = []
        subtitle_options = []

        if not isinstance(opt, dict):
            raise ConverterError('Invalid output specification')

        if 'format' not in opt:
            raise ConverterError('Format not specified')

        f = opt['format']
        if f not in self.formats:
            raise ConverterError('Requested unknown format: ' + str(f))

        format_options = self.formats[f]().parse_options(opt)
        if format_options is None:
            raise ConverterError('Unknown container format error')

        if 'audio' not in opt and 'video' not in opt:
            raise ConverterError('Neither audio nor video streams requested')

        if 'audio' not in opt or twopass == 1:
            opt['audio'] = {0: {'codec': None}}

        if 'video' not in opt:
            opt['video'] = {'codec': None}

        if 'audio' in opt:
            y = opt['audio']

            # Creates the new nested dictionary to preserve backwards compatability
            try:
                first = y.values()[0]
                if not isinstance(first, dict) and first is not None:
                    y = {0: y}
            except IndexError:
                pass

            for n in y:
                x = y[n]

                if not isinstance(x, dict) or 'codec' not in x:
                    raise ConverterError('Invalid audio codec specification')

                if 'map' not in x:
                    raise ConverterError('Must specify a map value')

                c = x['codec']
                if c not in self.audio_codecs:
                    raise ConverterError('Requested unknown audio codec ' +
                                         str(c))

                audio_options.extend(self.audio_codecs[c]().parse_options(
                    x, n))
                if audio_options is None:
                    raise ConverterError('Unknown audio codec error')

        if 'subtitle' in opt:
            y = opt['subtitle']

            # Creates the new nested dictionary to preserve backwards compatability
            try:
                first = y.values()[0]
                if not isinstance(first, dict) and first is not None:
                    y = {0: y}
            except IndexError:
                pass

            for n in y:
                x = y[n]
                if not isinstance(x, dict) or 'codec' not in x:
                    raise ConverterError(
                        'Invalid subtitle codec specification')

                if 'map' not in x:
                    raise ConverterError('Must specify a map value')

                if 'path' in x and 'source' not in x:
                    raise ConverterError(
                        'Cannot specify subtitle path without FFMPEG source number'
                    )

                if 'source' in x and 'path' not in x:
                    raise ConverterError(
                        'Cannot specify alternate input source without a path')

                c = x['codec']
                if c not in self.subtitle_codecs:
                    raise ConverterError('Requested unknown audio codec ' +
                                         str(c))

                subtitle_options.extend(
                    self.subtitle_codecs[c]().parse_options(x, n))
                if audio_options is None:
                    raise ConverterError('Unknown audio codec error')

        if 'video' in opt:
            x = opt['video']
            if not isinstance(x, dict) or 'codec' not in x:
                raise ConverterError('Invalid video codec specification')

            if 'map' not in x:
                raise ConverterError('Must specify a map value')

            c = x['codec']
            if c not in self.video_codecs:
                raise ConverterError('Requested unknown video codec ' + str(c))

            video_options = self.video_codecs[c]().parse_options(x)
            if video_options is None:
                raise ConverterError('Unknown video codec error')

        optlist = video_options + audio_options + subtitle_options + format_options

        if twopass == 1:
            optlist.extend(['-pass', '1'])
        elif twopass == 2:
            optlist.extend(['-pass', '2'])

        return optlist

    def convert(self, infile, outfile, options, twopass=False, timeout=10):
        """
        Convert media file (infile) according to specified options, and
        save it to outfile. For two-pass encoding, specify the pass (1 or 2)
        in the twopass parameter.

        Options should be passed as a dictionary. The keys are:
            * format (mandatory, string) - container format; see
              formats.BaseFormat for list of supported formats
            * audio (optional, dict) - audio codec and options; see
              avcodecs.AudioCodec for list of supported options
            * video (optional, dict) - video codec and options; see
              avcodecs.VideoCodec for list of supported options

        Multiple audio/video streams are not supported. The output has to
        have at least an audio or a video stream (or both).

        Convert returns a generator that needs to be iterated to drive the
        conversion process. The generator will periodically yield timecode
        of currently processed part of the file (ie. at which second in the
        content is the conversion process currently).

        The optional timeout argument specifies how long should the operation
        be blocked in case ffmpeg gets stuck and doesn't report back. This
        doesn't limit the total conversion time, just the amount of time
        Converter will wait for each update from ffmpeg. As it's usually
        less than a second, the default of 10 is a reasonable default. To
        disable the timeout, set it to None. You may need to do this if
        using Converter in a threading environment, since the way the
        timeout is handled (using signals) has special restriction when
        using threads.

        >>> conv = c.convert('test1.ogg', '/tmp/output.mkv', {
        ...    'format': 'mkv',
        ...    'audio': { 'codec': 'aac' },
        ...    'video': { 'codec': 'h264' }
        ... })

        >>> for timecode in conv:
        ...   pass # can be used to inform the user about the progress
        """

        if not isinstance(options, dict):
            raise ConverterError('Invalid options')

        if not os.path.exists(infile):
            raise ConverterError("Source file doesn't exist: " + infile)

        info = self.ffmpeg.probe(infile)
        if info is None:
            raise ConverterError("Can't get information about source file")

        if not info.video and not info.audio:
            raise ConverterError('Source file has no audio or video streams')

        if info.video and 'video' in options:
            v = options['video']
            v['src_width'] = info.video.video_width
            v['src_height'] = info.video.video_height

        if info.format.duration < 0.01:
            raise ConverterError('Zero-length media')

        if twopass:
            optlist1 = self.parse_options(options, 1)
            for timecode in self.ffmpeg.convert(infile,
                                                outfile,
                                                optlist,
                                                timeout=timeout):
                yield int((50.0 * timecode) / info.format.duration)

            optlist2 = self.parse_options(options, 2)
            for timecode in self.ffmpeg.convert(infile,
                                                outfile,
                                                optlist2,
                                                timeout=timeout):
                yield int(50.0 + (50.0 * timecode) / info.format.duration)
        else:
            optlist = self.parse_options(options, twopass)
            for timecode in self.ffmpeg.convert(infile,
                                                outfile,
                                                optlist,
                                                timeout=timeout):
                yield int((100.0 * timecode) / info.format.duration)

    def probe(self, fname):
        """
        Examine the media file. See the documentation of
        converter.FFMpeg.probe() for details.
        """
        return self.ffmpeg.probe(fname)

    def thumbnail(self, fname, time, outfile, size=None):
        """
        Create a thumbnail of the media file. See the documentation of
        converter.FFMpeg.thumbnail() for details.
        """
        return self.ffmpeg.thumbnail(fname, time, outfile, size)
Esempio n. 20
0
class Converter(object):
    """
    Converter class, encapsulates formats and codecs.

    >>> c = Converter()
    """

    def __init__(self, ffmpeg_path=None, ffprobe_path=None):
        """
        Initialize a new Converter object.
        """

        self.ffmpeg = FFMpeg(ffmpeg_path=ffmpeg_path,
            ffprobe_path=ffprobe_path)
        self.video_codecs = {}
        self.audio_codecs = {}
        self.formats = {}

        for cls in audio_codec_list:
            name = cls.codec_name
            self.audio_codecs[name] = cls

        for cls in video_codec_list:
            name = cls.codec_name
            self.video_codecs[name] = cls

        for cls in format_list:
            name = cls.format_name
            self.formats[name] = cls

    def parse_options(self, opt, twopass=None):
        """
        Parse format/codec options and prepare raw ffmpeg option list.
        """
        format_options = None
        audio_options = []
        video_options = []

        if not isinstance(opt, dict):
            raise ConverterError('Invalid output specification')

        if 'format' not in opt:
            raise ConverterError('Format not specified')

        f = opt['format']
        if f not in self.formats:
            raise ConverterError('Requested unknown format: ' + str(f))

        format_options = self.formats[f]().parse_options(opt)
        if format_options is None:
            raise ConverterError('Unknown container format error')

        if 'audio' not in opt and 'video' not in opt:
            raise ConverterError('Neither audio nor video streams requested')

        if 'audio' not in opt or twopass == 1:
            opt['audio'] = {'codec': None}

        if 'video' not in opt:
            opt['video'] = {'codec': None}

        if 'audio' in opt:
            x = opt['audio']

            if not isinstance(x, dict) or 'codec' not in x:
                raise ConverterError('Invalid audio codec specification')

            c = x['codec']
            if c not in self.audio_codecs:
                raise ConverterError('Requested unknown audio codec %s. Audio Supported %s'%(str(c),unicode(self.audio_codecs)))

            audio_options = self.audio_codecs[c]().parse_options(x)
            if audio_options is None:
                raise ConverterError('Unknown audio codec error')

        if 'video' in opt:
            x = opt['video']
            if not isinstance(x, dict) or 'codec' not in x:
                raise ConverterError('Invalid video codec specification')

            c = x['codec']
            if c not in self.video_codecs:
                raise ConverterError('Requested unknown video codec  %s. Video Supported %s'%(str(c),unicode(self.video_codecs)))

            video_options = self.video_codecs[c]().parse_options(x)
            if video_options is None:
                raise ConverterError('Unknown video codec error')

        optlist = audio_options + video_options + format_options

        if twopass == 1:
            optlist.extend(['-pass', '1'])
        elif twopass == 2:
            optlist.extend(['-pass', '2'])

        return optlist

    def convert(self, infile, outfile, options, twopass=False):
        """
        Convert media file (infile) according to specified options, and
        save it to outfile. For two-pass encoding, specify the pass (1 or 2)
        in the twopass parameter.

        Options should be passed as a dictionary. The keys are:
            * format (mandatory, string) - container format; see
              formats.BaseFormat for list of supported formats
            * audio (optional, dict) - audio codec and options; see
              avcodecs.AudioCodec for list of supported options
            * video (optional, dict) - video codec and options; see
              avcodecs.VideoCodec for list of supported options

        Multiple audio/video streams are not supported. The output has to
        have at least an audio or a video stream (or both).

        Convert returns a generator that needs to be iterated to drive the
        conversion process. The generator will periodically yield timecode
        of currently processed part of the file (ie. at which second in the
        content is the conversion process currently).

        >>> conv = c.convert('test1.ogg', '/tmp/output.mkv', {
        ...    'format': 'mkv',
        ...    'audio': { 'codec': 'aac' },
        ...    'video': { 'codec': 'h264' }
        ... })

        >>> for timecode in conv:
        ...   pass # can be used to inform the user about the progress
        """

        if not isinstance(options, dict):
            raise ConverterError('Invalid options')

        if not os.path.exists(infile):
            raise ConverterError("Source file doesn't exist: " + infile)

        info = self.ffmpeg.probe(infile)
        if info is None:
            raise ConverterError("Can't get information about source file")

        if not info.video and not info.audio:
            raise ConverterError('Source file has no audio or video streams')

        if info.video and 'video' in options:
            v = options['video']
            v['src_width'] = info.video.video_width
            v['src_height'] = info.video.video_height

        if info.format.duration < 0.01:
            raise ConverterError('Zero-length media')

        if twopass:
            optlist1 = self.parse_options(options, 1)
            for timecode in self.ffmpeg.convert(infile, outfile, optlist1):
                yield int((50.0 * timecode) / info.format.duration)

            optlist2 = self.parse_options(options, 2)
            for timecode in self.ffmpeg.convert(infile, outfile, optlist2):
                yield int(50.0 + (50.0 * timecode) / info.format.duration)
        else:
            optlist = self.parse_options(options, twopass)
            for timecode in self.ffmpeg.convert(infile, outfile, optlist):
                yield int((100.0 * timecode) / info.format.duration)

    def probe(self, fname):
        """
        Examine the media file. See the documentation of
        converter.FFMpeg.probe() for details.
        """
        return self.ffmpeg.probe(fname)

    def thumbnail(self, fname, time, outfile, size=None):
        """
        Create a thumbnail of the media file. See the documentation of
        converter.FFMpeg.thumbnail() for details.
        """
        return self.ffmpeg.thumbnail(fname, time, outfile, size)
Esempio n. 21
0
class HTTPLiveVariantStream(KNDistributor):
    """Encode an input mpegts stream to the desired quality and segment the stream to chunks"""
    
    def __init__(self,name,encoderArguments,destdir=None,ffmpegbin=None):
        """
        Args:
        name: Name of this quality (Used in path names)
        encoderArguments: :class:`configobj.ConfigObj` with valid ffmpeg options. This will be passed to a new :class:`FFMpeg` object.
        destdir: The location where files will be saved.
        ffmpegbin: Path to ffmpeg binary.
        """
        super(HTTPLiveVariantStream,self).__init__(name=name)

        self.name = name
        """Name of this variant"""

        self.encoder = None
        """The :class:`FFMpeg` object used for encoding"""

        self.segmenter = None
        """The :class:`HTTPLiveSegmenter` object used for segmenting"""

        self.destinationDirectory = None
        self.setDestdir(destdir)
        # Set up the encoder
        if ffmpegbin:
            self.encoder = FFMpeg(name=name, ffmpegbin=ffmpegbin,encoderArguments=encoderArguments)
        else:
            self.encoder = FFMpeg(name=name, encoderArguments=encoderArguments)

        self.segmenter = HTTPLiveSegmenter(name=self.name+"_segmenter",destdir=self.destinationDirectory)

        # Hook everything up
        self.addOutlet(self.encoder)
        self.encoder.addOutlet(self.segmenter)

    def willStart(self):
        config = self._findObjectInInletChainOfClass(knive.Knive).config
        self.segmenter.segmenterbin = config['paths']['segmenterbin']

        
    def setDestdir(self,destdir,createDir=False):
        """Set the location where files will be saved to destdir.

        Args:
            createDir: (bool) Create the directory if it doesn't exist already. Else throws an exception.
        """

        self.log.debug("Settings destinationDirectory to %s" % destdir)
        destdir = os.path.abspath(destdir)
        if os.path.exists(destdir):
            self.destinationDirectory = destdir
            self.log.debug("Will create files in '%s'" % self.destinationDirectory)
        else:
            if(createDir):
                try:
                    os.mkdir(destdir)
                    self.setDestdir(destdir)
                except:
                    raise
            else:
                raise Exception("Directory does not exist %s" % destdir)
Esempio n. 22
0
class Converter(object):
    """
    Converter class, encapsulates formats and codecs.

    >>> c = Converter()
    """

    def __init__(self, ffmpeg_path=None, ffprobe_path=None):
        """
        Initialize a new Converter object.
        """

        self.ffmpeg = FFMpeg(ffmpeg_path=ffmpeg_path,
                             ffprobe_path=ffprobe_path)
        self.video_codecs = {}
        self.audio_codecs = {}
        self.formats = {}

        for cls in audio_codec_list:
            name = cls.codec_name
            self.audio_codecs[name] = cls

        for cls in video_codec_list:
            name = cls.codec_name
            self.video_codecs[name] = cls

        for cls in format_list:
            name = cls.format_name
            self.formats[name] = cls

    def parse_options(self, opt, twopass=None):
        """
        Parse format/codec options and prepare raw ffmpeg option list.
        """
        if not isinstance(opt, dict):
            raise ConverterError('Invalid output specification')

        if 'format' not in opt:
            raise ConverterError('Format not specified')

        f = opt['format']
        if f not in self.formats:
            raise ConverterError('Requested unknown format: ' + str(f))

        format_options = self.formats[f]().parse_options(opt)
        if format_options is None:
            raise ConverterError('Unknown container format error')

        if 'audio' not in opt and 'video' not in opt:
            raise ConverterError('Neither audio nor video streams requested')

        # audio options
        if 'audio' not in opt or twopass == 1:
            opt_audio = {'codec': None}
        else:
            opt_audio = opt['audio']
            if not isinstance(opt_audio, dict) or 'codec' not in opt_audio:
                raise ConverterError('Invalid audio codec specification')

        c = opt_audio['codec']
        if c not in self.audio_codecs:
            raise ConverterError('Requested unknown audio codec ' + str(c))

        audio_options = self.audio_codecs[c]().parse_options(opt_audio)
        if audio_options is None:
            raise ConverterError('Unknown audio codec error')

        # video options
        if 'video' not in opt:
            opt_video = {'codec': None}
        else:
            opt_video = opt['video']
            if not isinstance(opt_video, dict) or 'codec' not in opt_video:
                raise ConverterError('Invalid video codec specification')

        c = opt_video['codec']
        if c not in self.video_codecs:
            raise ConverterError('Requested unknown video codec ' + str(c))

        video_options = self.video_codecs[c]().parse_options(opt_video)
        if video_options is None:
            raise ConverterError('Unknown video codec error')

        # aggregate all options
        optlist = audio_options + video_options + format_options

        if twopass == 1:
            optlist.extend(['-pass', '1'])
        elif twopass == 2:
            optlist.extend(['-pass', '2'])

        return optlist

    def convert(self, infile, outfile, options, twopass=False, timeout=10):
        """
        Convert media file (infile) according to specified options, and
        save it to outfile. For two-pass encoding, specify the pass (1 or 2)
        in the twopass parameter.

        Options should be passed as a dictionary. The keys are:
            * format (mandatory, string) - container format; see
              formats.BaseFormat for list of supported formats
            * audio (optional, dict) - audio codec and options; see
              avcodecs.AudioCodec for list of supported options
            * video (optional, dict) - video codec and options; see
              avcodecs.VideoCodec for list of supported options

        Multiple audio/video streams are not supported. The output has to
        have at least an audio or a video stream (or both).

        Convert returns a generator that needs to be iterated to drive the
        conversion process. The generator will periodically yield timecode
        of currently processed part of the file (ie. at which second in the
        content is the conversion process currently).

        The optional timeout argument specifies how long should the operation
        be blocked in case ffmpeg gets stuck and doesn't report back. This
        doesn't limit the total conversion time, just the amount of time
        Converter will wait for each update from ffmpeg. As it's usually
        less than a second, the default of 10 is a reasonable default. To
        disable the timeout, set it to None. You may need to do this if
        using Converter in a threading environment, since the way the
        timeout is handled (using signals) has special restriction when
        using threads.

        >>> conv = Converter().convert('test1.ogg', '/tmp/output.mkv', {
        ...    'format': 'mkv',
        ...    'audio': { 'codec': 'aac' },
        ...    'video': { 'codec': 'h264' }
        ... })

        >>> for timecode in conv:
        ...   pass # can be used to inform the user about the progress
        """

        if not isinstance(options, dict):
            raise ConverterError('Invalid options')

        if not os.path.exists(infile):
            raise ConverterError("Source file doesn't exist: " + infile)

        info = self.ffmpeg.probe(infile)
        if info is None:
            raise ConverterError("Can't get information about source file")

        if not info.video and not info.audio:
            raise ConverterError('Source file has no audio or video streams')

        if info.video and 'video' in options:
            options = options.copy()
            v = options['video'] = options['video'].copy()
            v['src_width'] = info.video.video_width
            v['src_height'] = info.video.video_height

        if info.format.duration < 0.01:
            raise ConverterError('Zero-length media')

        if twopass:
            optlist1 = self.parse_options(options, 1)
            for timecode in self.ffmpeg.convert(infile, outfile, optlist1,
                                                timeout=timeout):
                yield int((50.0 * timecode) / info.format.duration)

            optlist2 = self.parse_options(options, 2)
            for timecode in self.ffmpeg.convert(infile, outfile, optlist2,
                                                timeout=timeout):
                yield int(50.0 + (50.0 * timecode) / info.format.duration)
        else:
            optlist = self.parse_options(options, twopass)
            for timecode in self.ffmpeg.convert(infile, outfile, optlist,
                                                timeout=timeout):
                yield int((100.0 * timecode) / info.format.duration)

    def probe(self, fname, posters_as_video=True):
        """
        Examine the media file. See the documentation of
        converter.FFMpeg.probe() for details.

        :param posters_as_video: Take poster images (mainly for audio files) as
            A video stream, defaults to True
        """
        return self.ffmpeg.probe(fname, posters_as_video)

    def thumbnail(self, fname, time, outfile, size=None, quality=FFMpeg.DEFAULT_JPEG_QUALITY):
        """
        Create a thumbnail of the media file. See the documentation of
        converter.FFMpeg.thumbnail() for details.
        """
        return self.ffmpeg.thumbnail(fname, time, outfile, size, quality)

    def thumbnails(self, fname, option_list):
        """
        Create one or more thumbnail of the media file. See the documentation
        of converter.FFMpeg.thumbnails() for details.
        """
        return self.ffmpeg.thumbnails(fname, option_list)
Esempio n. 23
0
class HTTPLiveVariantStream(KNDistributor):
    """Encode an input mpegts stream to the desired quality and segment the stream to chunks"""
    def __init__(self, name, encoderArguments, destdir=None, ffmpegbin=None):
        """
        Args:
        name: Name of this quality (Used in path names)
        encoderArguments: :class:`configobj.ConfigObj` with valid ffmpeg options. This will be passed to a new :class:`FFMpeg` object.
        destdir: The location where files will be saved.
        ffmpegbin: Path to ffmpeg binary.
        """
        super(HTTPLiveVariantStream, self).__init__(name=name)

        self.name = name
        """Name of this variant"""

        self.encoder = None
        """The :class:`FFMpeg` object used for encoding"""

        self.segmenter = None
        """The :class:`HTTPLiveSegmenter` object used for segmenting"""

        self.destinationDirectory = None
        self.setDestdir(destdir)
        # Set up the encoder
        if ffmpegbin:
            self.encoder = FFMpeg(ffmpegbin=ffmpegbin,
                                  encoderArguments=encoderArguments)
        else:
            self.encoder = FFMpeg(encoderArguments=encoderArguments)

        self.segmenter = HTTPLiveSegmenter(name=self.name + "_segmenter",
                                           destdir=self.destinationDirectory)

        # Hook everything up
        self.addOutlet(self.encoder)
        self.encoder.addOutlet(self.segmenter)

    def willStart(self):
        config = self._findObjectInInletChainOfClass(knive.Knive).config
        self.segmenter.segmenterbin = config['paths']['segmenterbin']

    def setDestdir(self, destdir, createDir=False):
        """Set the location where files will be saved to destdir.

        Args:
            createDir: (bool) Create the directory if it doesn't exist already. Else throws an exception.
        """

        self.log.debug("Settings destinationDirectory to %s" % destdir)
        destdir = os.path.abspath(destdir)
        if os.path.exists(destdir):
            self.destinationDirectory = destdir
            self.log.debug("Will create files in '%s'" %
                           self.destinationDirectory)
        else:
            if (createDir):
                try:
                    os.mkdir(destdir)
                    self.setDestdir(destdir)
                except:
                    raise
            else:
                raise Exception("Directory does not exist %s" % destdir)