<commit_before>from moviepy.editor import *
from annotations import get_annotation_duration, get_annotations_added_duration, get_subtitle, get_marker


def bake_annotations(video_file, end_point, annotations):
    clip = VideoFileClip(video_file)
    annotated_video = generate_annotation_markings(clip, annotations)
    final_video = generate_video_pauses(annotated_video, annotations)
    audio = clip.audio.set_duration(clip.audio.duration + get_annotations_added_duration(annotations))
    final_video_audio = generate_pause_audio(audio, annotations)
    final_video.set_audio(final_video_audio)
    final_video.write_videofile(end_point)


def generate_annotation_markings(video_clip, annotations):
    composite_clips = [video_clip]
    one_frame_time = 1 / video_clip.fps

    for annotation in annotations:
        txt_clip = get_subtitle(annotation, one_frame_time)
        marker = get_marker(annotation, one_frame_time, video_clip)
        composite_clips.append(txt_clip)
        composite_clips.append(marker)

    return CompositeVideoClip(composite_clips)


def generate_video_pauses(video_clip, annotations):
    """Takes in a regular video clip, and bakes in annotation pauses"""
    for annotation in reversed(annotations):
        pause_time = get_annotation_duration(annotation)
        current_annotation_time = annotation["time"] / 1000.0
        video_clip = video_clip.fx(vfx.freeze, t=current_annotation_time, freeze_duration=pause_time)

    return video_clip


def generate_pause_audio(original_audio, annotations):
    audio_clip = original_audio
    last_pause_time = 0
    audio_parts = []

    for annotation in annotations:
        pause_time = get_annotation_duration(annotation)
        current_annotation_time = annotation["time"] / 1000.0

        audio_part = audio_clip.subclip(last_pause_time, current_annotation_time)
        audio_part = audio_part.set_start(last_pause_time)
        audio_part = audio_part.set_end(current_annotation_time)
        audio_parts.append(audio_part)

        last_pause_time = current_annotation_time + pause_time

    comp = CompositeAudioClip(audio_parts)

    return comp

<commit_msg>Change audio-codec for quicktime-friendly video<commit_after>from moviepy.editor import *
from annotations import get_annotation_duration, get_annotations_added_duration, get_subtitle, get_marker


def bake_annotations(video_file, end_point, annotations):
    clip = VideoFileClip(video_file)
    annotated_video = generate_annotation_markings(clip, annotations)
    final_video = generate_video_pauses(annotated_video, annotations)
    audio = clip.audio.set_duration(clip.audio.duration + get_annotations_added_duration(annotations))
    final_video_audio = generate_pause_audio(audio, annotations)
    final_video.set_audio(final_video_audio)
    final_video.write_videofile(end_point, codec='libx264', audio_codec='aac', temp_audiofile='temp-audio.m4a')


def generate_annotation_markings(video_clip, annotations):
    composite_clips = [video_clip]
    one_frame_time = 1 / video_clip.fps

    for annotation in annotations:
        txt_clip = get_subtitle(annotation, one_frame_time)
        marker = get_marker(annotation, one_frame_time, video_clip)
        composite_clips.append(txt_clip)
        composite_clips.append(marker)

    return CompositeVideoClip(composite_clips)


def generate_video_pauses(video_clip, annotations):
    """Takes in a regular video clip, and bakes in annotation pauses"""
    for annotation in reversed(annotations):
        pause_time = get_annotation_duration(annotation)
        current_annotation_time = annotation["time"] / 1000.0
        video_clip = video_clip.fx(vfx.freeze, t=current_annotation_time, freeze_duration=pause_time)

    return video_clip


def generate_pause_audio(original_audio, annotations):
    audio_clip = original_audio
    last_pause_time = 0
    audio_parts = []

    for annotation in annotations:
        pause_time = get_annotation_duration(annotation)
        current_annotation_time = annotation["time"] / 1000.0

        audio_part = audio_clip.subclip(last_pause_time, current_annotation_time)
        audio_part = audio_part.set_start(last_pause_time)
        audio_part = audio_part.set_end(current_annotation_time)
        audio_parts.append(audio_part)

        last_pause_time = current_annotation_time + pause_time

    comp = CompositeAudioClip(audio_parts)

    return comp


{'fraction_non_alphanumeric': 0.06966749604161954, 'fraction_numerical': 0.006333408731056322, 'mean_word_length': 4.83377308707124, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '712657', 'n_tokens_mistral': 1359, 'n_tokens_neox': 1278, 'n_words': 235}