コード例 #1
0
def cmd_match(segment_kwargs, directory=None, match_all=False, fail_if_cut=False):
    if 'youtube' in segment_kwargs:
        video = ytdl_video(segment_kwargs['youtube'])
    elif 'direct' in segment_kwargs:
        video = Clip(segment_kwargs['direct'], ar=1000)

    template = video.slice(300, 300)[0]

    candidates = sorted(
        [s
         for s in streams.segments
         if can_match(s, segment_kwargs, directory, match_all)],
        key=lambda s: abs(int(s.abs_end - s.abs_start) - video.duration)
    )

    original = None
    matching_stream = None
    video_offset = None

    for segment in candidates:
        path = original_video(segment, directory)
        print(f'Checking segment {segment.hash} (path: {path})',
              file=sys.stderr)

        original = Clip(path, ar=1000)

        try:
            offset, score = find_offset(
                template, original,
                start=int(segment.abs_start),
                end=int(segment.abs_end) - video.duration + 10 * 60,
                min_score=10)
        except Exception:
            continue

        offset -= 300

        if score > 0:
            matching_stream = segment.stream
            video_offset = Timecode(round(offset))
            break

    if matching_stream is None:
        print('Error: Video does not match any streams', file=sys.stderr)
        sys.exit(2)

    segment_kwargs['offset'] = video_offset

    if fail_if_cut:
        print('Checking for cuts...')
        diff = check_cuts(original, video, offset=video_offset.value)
        if diff > 1:
            print(f'Error: The video is {int(diff)} seconds shorter '
                   'than the original.', file=sys.stderr)
            sys.exit(3)

    segment = cmd_add(matching_stream, segment_kwargs)
    return matching_stream, segment
コード例 #2
0
def ytdl_video(video_id):
    try:
        print(f'Preparing template...', file=sys.stderr)
        youtube_source = ytdl_best_source(video_id)
        video = Clip(youtube_source, ar=1000)
    except:
        print(f'Falling back to bestaudio...', file=sys.stderr)
        youtube_source = ytdl_best_source(video_id, 'bestaudio')
        video = Clip(youtube_source, ar=1000)
    return video
コード例 #3
0
def cmd_cuts(segment, segment_kwargs, directory=None):
    if 'youtube' in segment_kwargs:
        video = ytdl_video(segment_kwargs['youtube'])
    elif 'direct' in segment_kwargs:
        video = Clip(segment_kwargs['direct'], ar=1000)

    original = Clip(original_video(segment, directory), ar=1000)
    diff = check_cuts(original, video, offset=int(segment.offset))

    if diff <= 1:
        print('The video is the same as the original')
    else:
        print(f'The video is {int(diff)} seconds shorter than the original')
        sys.exit(1)
コード例 #4
0
def ytdl_video(video_id):
    try:
        print('Retrieving video from YouTube...', file=sys.stderr)
        youtube_source = ytdl_best_source(video_id)
        video = Clip(youtube_source, ar=1000)
        video.slice(0, 1)
    except Exception:
        print('Falling back to bestaudio...', file=sys.stderr)
        youtube_source = ytdl_best_source(video_id, 'bestaudio')
        video = Clip(youtube_source, ar=1000)
        video.slice(0, 1)
    return video
コード例 #5
0
def cmd_match(segment_kwargs,
              directory=None,
              match_all=False,
              fail_if_cut=False):
    if 'youtube' in segment_kwargs:
        dupes = [
            s for s in streams.segments
            if s.youtube == segment_kwargs['youtube']
        ]

        if len(dupes) != 0:
            print(
                f'Error: Video is already assigned to segment {dupes[0].hash}',
                file=sys.stderr)
            sys.exit(2)

        video = ytdl_video(segment_kwargs['youtube'])
    elif 'direct' in segment_kwargs:
        video = Clip(segment_kwargs['direct'], ar=1000)
    else:
        print('Error: Video source is not supported')
        sys.exit(2)

    segment_kwargs['duration'] = video.duration

    candidates = sorted(match_candidates(segment_kwargs, directory, match_all),
                        key=lambda s: abs(int(s[1].duration) - video.duration))

    if len(candidates) == 0:
        print('Error: No candidates found', file=sys.stderr)
        sys.exit(2)

    print('Possible candidates:')
    for segment, t_range, s_range in candidates:
        print(f'  {segment.twitch} - {t_range} - {s_range}')

    print('Preparing template...', file=sys.stderr)
    template = video.slice(MATCH_OFFSET, MATCH_CHUNK)[0]

    original = None
    matching_segment = None
    video_offset = None

    for segment, _, s_range in candidates:
        path = original_video(segment, directory)
        print(f'Checking segment {segment.hash} {s_range} (path: {path})',
              file=sys.stderr)

        original = Clip(path, ar=1000)

        try:
            offset, score = find_offset(template,
                                        original,
                                        start=int(s_range.start),
                                        end=int(s_range.end),
                                        min_score=50)
        except Exception:
            continue

        offset -= MATCH_OFFSET

        if score == 0:
            continue

        video_offset = Timecode(round(offset))
        print(f'Match found: segment {segment.hash}, offset {video_offset}',
              file=sys.stderr)

        if segment.stream.type == StreamType.JOINED and not fail_if_cut:
            print('Matching stream is joined, forcing --fail-if-cut')
            fail_if_cut = True

        if fail_if_cut:
            print('Checking for cuts...')
            diff = check_cuts(original, video, offset=int(video_offset))
            if diff > 1:
                print(
                    f'Error: The video is {int(diff)} seconds shorter '
                    'than the original.',
                    file=sys.stderr)
                continue

        matching_segment = segment
        break

    if matching_segment is None:
        print('Error: Video does not match any streams', file=sys.stderr)
        sys.exit(2)

    segment_kwargs['offset'] = video_offset

    if matching_segment.note:
        segment_kwargs['note'] = matching_segment.note

    if video_offset == 0 and fail_if_cut:
        if len(matching_segment.cuts) > 0:
            segment_kwargs['cuts'] = matching_segment._cuts

        if matching_segment.stream.type == StreamType.JOINED:
            segment_kwargs['offsets'] = matching_segment.offsets

        if matching_segment.offset(0) != 0:
            segment_kwargs['offset'] = matching_segment.offset(0)

    segment = cmd_add(matching_segment.stream, segment_kwargs)
    return matching_segment.stream, segment
コード例 #6
0
ファイル: cli.py プロジェクト: ZeeWanderer/BlackSilverUfa
def cmd_match(segment_kwargs,
              directory=None,
              match_all=False,
              fail_if_cut=False):
    if 'youtube' in segment_kwargs:
        dupes = [
            s for s in streams.segments
            if s.youtube == segment_kwargs['youtube']
        ]

        if len(dupes) != 0:
            print(
                f'Error: Video is already assigned to segment {dupes[0].hash}',
                file=sys.stderr)
            sys.exit(2)

        video = ytdl_video(segment_kwargs['youtube'])
    elif 'direct' in segment_kwargs:
        video = Clip(segment_kwargs['direct'], ar=1000)

    segment_kwargs['duration'] = video.duration

    candidates = sorted(match_candidates(segment_kwargs, directory, match_all),
                        key=lambda s: abs(int(s[1].duration) - video.duration))

    if len(candidates) == 0:
        print('Error: No candidates found', file=sys.stderr)
        sys.exit(2)

    print(f'Preparing template...', file=sys.stderr)
    template = video.slice(300, 300)[0]

    original = None
    matching_stream = None
    video_offset = None

    for segment, t_range in candidates:
        path = original_video(segment, directory)
        print(f'Checking segment {segment.hash} {t_range} (path: {path})',
              file=sys.stderr)

        original = Clip(path, ar=1000)
        start = t_range.value
        end = t_range.value + max(t_range.duration - video.duration, 0) + 600

        try:
            offset, score = find_offset(template,
                                        original,
                                        start=start,
                                        end=end,
                                        min_score=10)
        except Exception:
            continue

        offset -= 300

        if score > 0:
            matching_stream = segment.stream
            video_offset = Timecode(round(offset))
            print(
                f'Match found: segment {segment.hash}, offset {video_offset}',
                file=sys.stderr)
            break

    if matching_stream is None:
        print('Error: Video does not match any streams', file=sys.stderr)
        sys.exit(2)

    segment_kwargs['offset'] = video_offset

    if fail_if_cut:
        print('Checking for cuts...')
        diff = check_cuts(original, video, offset=video_offset.value)
        if diff > 1:
            print(
                f'Error: The video is {int(diff)} seconds shorter '
                'than the original.',
                file=sys.stderr)
            sys.exit(3)

    segment = cmd_add(matching_stream, segment_kwargs)
    return matching_stream, segment