How to use the pycaption.detect_format function in pycaption

To help you get started, we’ve selected a few pycaption examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github keredson / gnomecast / gnomecast.py View on Github external
return
    fn = os.path.abspath(fn)
    ext = fn.split('.')[-1]
    display_name = os.path.basename(fn)
    if ext=='vtt':
      with open(fn) as f:
        self.subtitles = f.read()
    else:
      with open(fn,'rb') as f:
        caps = f.read()
        try: caps = caps.decode()
        except UnicodeDecodeError: caps = caps.decode('latin-1')
      if caps.startswith('\ufeff'): # BOM
        caps = caps[1:]
      converter = pycaption.CaptionConverter()
      converter.read(caps, pycaption.detect_format(caps)())
      self.subtitles = converter.write(pycaption.WebVTTWriter())
    pos = len(self.subtitle_store)
    stream = StreamMetadata(None, None, title=display_name)
    stream._subtitles = self.subtitles
    self.subtitle_store.append([display_name, stream, None])
    self.subtitle_combo.set_active(pos)
github keredson / gnomecast / gnomecast.py View on Github external
cmd = ['ffmpeg', '-y', '-i', self.fn, '-vn', '-an',]
    files = []
    for stream in self.subtitles:
      srt_fn = tempfile.mkstemp(suffix='.srt', prefix='gnomecast_subtitles_')[1]
      files.append(srt_fn)
      cmd += ['-map', stream.index, '-codec', 'srt', srt_fn]
      
    print(cmd)
    try:
      output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
      for stream, srt_fn in zip(self.subtitles, files):
        with open(srt_fn) as f:
          caps = f.read()
        #print('caps', caps)
        converter = pycaption.CaptionConverter()
        converter.read(caps, pycaption.detect_format(caps)())
        stream._subtitles = converter.write(pycaption.WebVTTWriter())
        os.remove(srt_fn)
    except subprocess.CalledProcessError as e:
      print('ERROR processing subtitles:', e)
      self.subtitles = []
github appsembler / xblock-video / video_xblock / mixins.py View on Github external
def convert_caps_to_vtt(caps):
        """
        Utility method to convert any supported transcripts into WebVTT format.

        Supported input formats: DFXP/TTML - SAMI - SCC - SRT - WebVTT.

        Arguments:
            caps (unicode): Raw transcripts.
        Returns:
            unicode: Transcripts converted into WebVTT format.
        """
        if caps:
            reader = detect_format(caps)
            if reader:
                return WebVTTWriter().write(reader().read(caps))
        return u''
github raydouglass / media_management_scripts / media_management_scripts / convert.py View on Github external
def convert_subtitles_to_srt(i: str, o: str):
    ext = os.path.splitext(i)[1]
    if ext == '.srt':
        import shutil
        shutil.copy(i, o)
    elif ext in ('.ttml', '.xml', '.dfxp', '.tt'):
        # TTML
        from media_management_scripts.support.ttml2srt import convert_to_srt
        convert_to_srt(i, o)
    else:
        # VTT, SCC, etc

        from pycaption import detect_format, SRTWriter
        subtitle_str = _read_file(i)
        reader = detect_format(subtitle_str)
        if reader:
            subtitle_str = SRTWriter().write(reader().read(subtitle_str))
            with open(o, 'w') as file:
                file.write(subtitle_str)
        else:
            # Attempt to use FFMPEG
            from media_management_scripts.support.executables import ffmpeg
            from media_management_scripts.support.executables import execute_with_output
            args = [ffmpeg(), '-loglevel', 'fatal', '-y', '-i', i, '-c:s', 'srt', o]
            ret, output = execute_with_output(args)
            if ret != 0:
                raise Exception('Exception during subtitle conversion: {}'.format(output))