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))
def _execute_ffmpeg(input_file: str, frames: int, start: int = 0):
    args = [
        ffmpeg(), '-ss',
        str(start), '-i', input_file, '-filter:v', 'idet', '-frames:v',
        str(frames), '-an', '-f', 'rawvideo', '-y', '/dev/null'
    ]
    ret, output = execute_with_output(args, print_output=False)
    if ret != 0:
        raise Exception('Non-zero ffmpeg return code: {}. Output={}'.format(
            ret, output))
    return _parse_output(output)
def _temp_convert(input, output, print_output=False):
    # ffmpeg -i input1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts
    print('Converting {}'.format(input))
    args = [
        ffmpeg(), '-loglevel', 'fatal', '-i', input, '-c:v', 'copy', '-c:a',
        'copy', '-c:s', 'copy', '-bsf:v', 'h264_mp4toannexb', '-f', 'mpegts',
        output
    ]
    ret, r = execute_with_output(args, print_output=print_output)
    if ret != 0:
        raise Exception('Error during ffmpeg: {}'.format(r))
def _concat(files, output, print_output=False):
    # ffmpeg -i "concat:intermediate1.ts|intermediate2.ts" -c copy -bsf:a aac_adtstoasc output.mp4
    concat_str = 'concat:'
    for f in files:
        concat_str += f + '|'
    concat_str = concat_str[0:-1]
    print(concat_str)
    args = [
        ffmpeg(), '-loglevel', 'fatal', '-y', '-i', concat_str, '-c:v', 'copy',
        '-c:a', 'copy', '-c:s', 'copy', '-bsf:a', 'aac_adtstoasc', output
    ]
    ret, r = execute_with_output(args, print_output=print_output)
    if ret != 0:
        raise Exception('Error during ffmpeg: {}'.format(r))
def invoke_filebot(file_path: str, type: str):
    # java -jar FileBot.jar -rename /Volumes/Media/temp/QI_XL_Series_O_Episode_1_Ologies_720p.mp4.mp4 --db TheTVDB -non-strict --action test --format '{n}/{s}/{e}/{t}' --output '/'
    args = [
        java(), '-jar',
        filebot_jar(), '-rename', file_path, '--db',
        'TheTVDB' if type == 'tv' else 'TheMovieDB', '-non-strict', '--action',
        'test', '--format', "{n}/{s}/{e}/{t}/{ext}", '--output', '/'
    ]

    ret_code, filebot_output = execute_with_output(args,
                                                   print_output=False,
                                                   use_nice=False)
    if ret_code != 0:
        raise Exception('Non-zero filebot return code ({}): {}'.format(
            ret_code, filebot_output))

    results = parse_filebot_text(filebot_output)
    result = results.get(file_path, None)
    if result:
        return result
    else:
        return None, None, None, None
Esempio n. 6
0
    def convert(self, in_file, out_file, commercials, metadata):
        invert = invert_commercial(commercials)
        temp_files = []
        wo_ext = os.path.basename(in_file).replace('.wtv', '')
        try:
            args = [ffmpeg(), '-i', in_file]
            for i in invert:
                temp_file = os.path.join(
                    self.temp_dir, wo_ext + '.' + str(len(temp_files)) + '.ts')
                temp_files.append(temp_file)
                if os.path.isfile(temp_file):
                    os.remove(temp_file)
                args.extend(self.cut_args(i, temp_file))

            ret, output = execute_with_output(args)
            if ret != 0:
                logger.error('Nonzero return code from ffmpeg: {}'.format(ret))
                return False
            else:
                input = 'concat:{}'.format('|'.join(temp_files))
                ret = convert_with_config(input,
                                          out_file,
                                          config=self.convert_config,
                                          print_output=False,
                                          overwrite=True,
                                          metadata=metadata)
                if ret != 0:
                    logger.error(
                        'Nonzero return code from ffmpeg: {}'.format(ret))
                # Cleanup temp files
                if not self.debug:
                    for f in temp_files:
                        os.remove(f)
        except Exception as e:
            logger.exception('Exception')
            return False
        return True
Esempio n. 7
0
 def extract_subtitles(self, wtv_file, out_srt):
     execute_with_output([self.ccextractor_exe, wtv_file, '-o', out_srt])
def execute(args, print_output=True):
    ret, r = execute_with_output(args, print_output)
    return ret