Esempio n. 1
0
    def backup(self, dir, file) -> Tuple[subprocess.Popen, str]:
        relative_file = file.replace(dir, '')
        if relative_file.startswith('/'):
            relative_file = relative_file[1::]
        target_dir = os.path.dirname(relative_file)

        size = os.path.getsize(file)
        if size >= self.max_size:
            logger.debug('{} will be split'.format(file))
            split_out_dir = os.path.join(self.working_dir,
                                         os.path.basename(file) + '_split')
            name = os.path.join(split_out_dir, os.path.basename(file) + '.')
            create_dirs(name)
            split_args = [
                self.split_exe, '-d', '-a', '2', '-b', self.split_size, file,
                name
            ]
            logger.debug(split_args)
            ret = subprocess.run(split_args).returncode
            if ret != 0:
                logger.error('Error splitting, code={}, file={}'.format(
                    ret, file))
                return None
            return self.backup_file(split_out_dir, target_dir), split_out_dir
        else:
            return self.backup_file(file, target_dir), None
 def _copy(self, src, dst, overwrite=False):
     if self.dry_run:
         print('Copy: {}=>{}'.format(src, dst))
     else:
         if not overwrite and check_exists(dst, log=self.dry_run):
             return False
         create_dirs(dst)
         shutil.copy(src, dst)
         return True
Esempio n. 3
0
    def process(self, root_dir, input_file, output_file, temp_file, status,
                convert_config):
        if not status.convert and os.path.exists(output_file):
            logger.info(
                'Output exists, will not convert: input={}, output={}'.format(
                    input_file, output_file))
        create_dirs(temp_file)
        create_dirs(output_file)
        # Start backup
        backup_popen, cleanup_dir = None, None
        error = False
        if not status.backup:
            if self.backup_enabled:
                logger.debug('No backup for {}'.format(input_file))
                backup_popen, cleanup_dir = self.backup(root_dir, input_file)
            else:
                logger.debug(
                    'No backup for {}, but backups are disabled'.format(
                        input_file))
        if not status.convert and not os.path.exists(output_file):
            # Start convert
            logger.debug('Not converted: {}'.format(input_file))
            if os.path.exists(temp_file):
                os.remove(temp_file)
            result = convert_with_config(input_file,
                                         temp_file,
                                         convert_config,
                                         print_output=False)
            if result == 0:
                logger.debug('Conversion successful for {}'.format(input_file))
                shutil.copyfile(temp_file, output_file)
                status.convert = True
            else:
                logger.error('Error converting: code={}, file={}'.format(
                    result, input_file))
                error = True
            if os.path.exists(temp_file):
                os.remove(temp_file)
        if backup_popen:
            logger.debug('Waiting for backup...')
            ret_code = backup_popen.wait()

            if ret_code == 0:
                logger.debug('Backup succeeded for {}'.format(input_file))
                status.backup = True
                if cleanup_dir and os.path.exists(cleanup_dir):
                    shutil.rmtree(cleanup_dir, ignore_errors=True)
            else:
                logger.error('Error backing up: code={}, file={}'.format(
                    ret_code, input_file))
                error = True
        return not error
def cut(input, output, start=None, end=None):
    if check_exists(output):
        return -1
    create_dirs(output)
    args = [ffmpeg(), '-i', input]
    args.extend(['-c:v', 'copy'])
    args.extend(['-c:a', 'copy'])
    args.extend(['-c:s', 'copy'])
    args.extend(['-map', '0'])
    if start:
        args.extend(['-ss', str(start)])
    if end:
        args.extend(['-to', str(end)])
    args.append(output)
    return execute(args)
def combine(video,
            srt,
            output,
            lang=None,
            overwrite=False,
            convert=False,
            crf=DEFAULT_CRF,
            preset=DEFAULT_PRESET,
            skip_eia_608=True):
    if not overwrite and check_exists(output):
        return -1

    if srt.endswith('.ttml') or srt.endswith('.xml'):
        logger.debug('Converting ttml/xml to srt')
        name, _ = os.path.splitext(srt)
        srt_out = name + '.srt'
        convert_to_srt(srt, srt_out)
        srt = srt_out
    create_dirs(output)
    args = [ffmpeg(), '-i', video]
    if overwrite:
        args.append('-y')
    args.extend(['-i', srt])
    args.extend(['-map', '0:v', '-map', '0:a'])
    if skip_eia_608:
        metadata = extract_metadata(video)
        for i in (s.index for s in metadata.subtitle_streams
                  if s.codec != 'eia_608'):
            args.extend(['-map', '0:{}'.format(i)])
    else:
        args.extend(['-map', '0:s?'])
    args.extend(['-map', '1:0'])
    if convert:
        args.extend(['-c:v', 'libx264', '-crf', str(crf), '-preset', preset])
        args.extend(['-c:a', 'aac'])
    else:
        args.extend(['-c:v', 'copy'])
        args.extend(['-c:a', 'copy'])
    args.extend(['-c:s', 'copy'])
    # -metadata:s:s:0 language=eng
    if lang:
        args.extend(['-metadata:s:s:0', 'language=' + lang])
    args.append(output)
    return execute(args)
def main(input_dir, output_dir, config):
    files = list(get_input_output(input_dir, output_dir))
    logger.info('{} files to process'.format(len(files)))
    did_process = True
    while did_process:
        did_process = False
        for input_file, output_file in files:
            if not os.path.exists(output_file):
                try:
                    logger.info('Starting convert of {} -> {}'.format(
                        input_file, output_file))
                    create_dirs(output_file)
                    ret = convert_with_config(input_file, output_file, config)
                    if ret == 0:
                        did_process = True
                    else:
                        logger.error(
                            'Nonzero return code from ffmpeg: {}'.format(ret))

                except Exception as e:
                    logger.exception('Exception during convert')