Exemplo n.º 1
0
def _copy(obj_id, args):
    # TODO: Whoops, now used this twice (search.py too)
    path = built_ins['db']['recordings']
    rec_db = TinyDB(path)
    shows = Query()
    # shortcut for later
    shows_qry = shows.data
    # TODO: deal with pieces of the same recording (TMSID?) marked "finished"
    #      ie, 2 portions (non-full) of the an episode
    #        + just skip them (do this!)
    #        + differentiate on recorded at the same time
    #           - look at recording/show data to see what it *should* be?
    #        - overwrite previous portions
    obj = rec_db.get((shows_qry.object_id == int(obj_id))
                     & (shows_qry.video_details.state == 'finished'))
    if obj is None:
        print(f'ERROR: Unable to find recording with object_id == "{obj_id}", '
              f'skipping...')
        return

    rec = Recording(obj['data'])

    watch = rec.watch()
    if watch.error is not None:
        print(rec.get_description())
        print("ERROR: Recording no longer exists, skipping!")
        return
    out_file = rec.get_out_path('mp4')
    # TODO: this could make weird dirs?
    os.makedirs(os.path.dirname(out_file), exist_ok=True)

    # Display what we're working on
    if built_ins['log_level'] <= logging.INFO:
        rec.print()
        watch.dump_info()
    else:
        print(rec.get_description())
        print(" " * 2 + f"writing to: {out_file}")

    if not args.clobber and os.path.exists(out_file):
        print("File exists, skipping")
        return

    total_duration = float(
        ffmpeg.probe(watch.playlist_url)['format']['duration'])

    if built_ins['dry_run']:
        # maybe do a dry run writing to a temp path and deleting so the time
        # is roughly the same?
        print("DRY RUN: The recording wasn't saved.")
    else:
        with show_progress(total_duration) as socket_filename:
            try:
                copier = (
                    ffmpeg
                    # this is a m3u8 playlist
                    .input(watch.playlist_url)
                    .output(out_file, codec='copy',
                            preset='ultrafast', loglevel='info')
                    .overwrite_output()
                    .global_args(
                        '-progress', 'unix://{}'.format(socket_filename)
                    )
                )

                copier.run(capture_stdout=True, capture_stderr=True)
            except KeyboardInterrupt:
                os.remove(out_file)
                raise KeyboardInterrupt
            except ffmpeg.Error as e:
                logger.error(e)