コード例 #1
0
    def create_silent_video(self, youtube=None, preview=False):
        log("Creating silent video", prnt=True)

        base_path = str(self.podcast.id) + '/' + str(self.episode.id)
        video_path = base_path + '.mp4'

        with NamedTemporaryFile(suffix='.mp4') as tf:
            with NamedTemporaryFile(suffix='.mp3') as _audio:
                self.videosyncepisode.update_sync_status({
                    'event':
                    'DOWNLOAD_AUDIO',
                    'timestamp':
                    arrow.utcnow().isoformat()
                })
                r = requests.get(self.audio_url, stream=True)
                for chunk in r.iter_content(chunk_size=4096):
                    _audio.write(chunk)
                _audio.seek(0)
                song = AudioSegment.from_file(File(_audio), format='mp3')

                sound_length = math.ceil(len(song) / 1000)

                self.videosyncepisode.update_sync_status({
                    'event':
                    'CREATE_VIDEO',
                    'timestamp':
                    arrow.utcnow().isoformat()
                })
                clip = ImageClip(self.videosyncepisode.artwork_path,
                                 duration=sound_length)
                clip.write_videofile(tf.name, fps=1, audio=_audio.name)

                self.videosyncepisode.update_sync_status({
                    'event':
                    'UPLOAD_VIDEO',
                    'timestamp':
                    arrow.utcnow().isoformat()
                })
                video_id = self.upload_video(youtube, tf.name)

                return video_id

        return None
コード例 #2
0
ファイル: editor.py プロジェクト: leonid-mamaev/video_editor
 def image_to_video(self, image_path: str, destination: str,
                    duration: float) -> None:
     video = ImageClip(image_path)
     video = video.set_duration(duration)
     video.write_videofile(destination, fps=24)
コード例 #3
0
def generate_video_for_id(id):
    et_dataset_location = f'{reflacx_dataset_location}/main_data/'
    mimic_dataset_location = mimiccxr_images_location
    table_et_pt1 = pd.read_csv(f'{et_dataset_location}/{id}/fixations.csv')
    table_text = pd.read_csv(
        f'{et_dataset_location}/{id}/timestamps_transcription.csv')
    main_table = pd.read_csv(
        f'{et_dataset_location}/metadata_phase_{id[1]}.csv')
    image_filepath = main_table[main_table['id'] == id]['image'].values
    assert (len(image_filepath) == 1)
    max_time_fixation = max(table_et_pt1['timestamp_end_fixation'].values)
    max_time_text = max(table_text['timestamp_end_word'].values)
    dicom_array, _, _ = open_dicom(
        f'{mimic_dataset_location}/{image_filepath[0]}')
    dicom_array = dicom_array * 255
    from skimage.transform import resize
    dicom_array = resize(dicom_array,
                         (int(dicom_array.shape[0] * scale_video),
                          int(dicom_array.shape[1] * scale_video)),
                         anti_aliasing=True)
    dicom_array = dicom_array.astype(np.uint8)

    #generat ea clip with the original dicom as every frame
    my_clip = ImageClip(np.stack(
        (dicom_array, ) * 3,
        axis=-1)).set_duration(max([max_time_fixation,
                                    max_time_text])).set_fps(fps)

    #modify every frame of the video according to the fixations and transcription tables
    my_clip = fl(
        my_clip,
        lambda get_frame, t: scroll(get_frame, t, table_et_pt1, table_text))

    #generate the audio from the timestamped transcription
    full_audio = AudioSegment.empty()
    previous_end = 0
    for _, row in table_text.iterrows():

        # if start and end of the word are at the same time, it was not captured by the original transcription, so we do not use it in the audio, only in subtitle
        if row['timestamp_start_word'] == row['timestamp_end_word']:
            continue

        print(row['word'])

        # text to speech
        tts = SaveTTSFile('./create_video_temp.wav')
        tts.start(
            row['word'].replace('.', 'period').replace(',', 'comma').replace(
                '/', 'slash'), row['timestamp_start_word'],
            row['timestamp_end_word'])
        for i in range(10):
            if not os.path.exists('./create_video_temp.wav'):
                time.sleep(1)
            else:
                break
            if i > 10:
                assert (False)
        del (tts)

        # add silence between words if they did not end/start at the same time
        if row['timestamp_start_word'] > previous_end:
            full_audio += AudioSegment.silent(
                duration=(row['timestamp_start_word'] - previous_end) * 1000)
        print(full_audio.duration_seconds)
        print(row['timestamp_start_word'])
        assert (abs(full_audio.duration_seconds - row['timestamp_start_word'])
                < 0.005)

        #change the duration of the word sound to the duration it took for the radiologist to say it
        word_audio = AudioSegment.from_file('./create_video_temp.wav',
                                            format="wav")
        word_audio = stretch_audio(
            word_audio, './create_video_temp.wav',
            word_audio.duration_seconds /
            (row['timestamp_end_word'] - row['timestamp_start_word']))

        os.remove('./create_video_temp.wav')

        full_audio += word_audio
        assert (abs(full_audio.duration_seconds - row['timestamp_end_word']) <
                0.005)
        previous_end = row['timestamp_end_word']
    full_audio.export("./create_video_temp.wav", format="wav")
    audio_background = mpe.AudioFileClip('./create_video_temp.wav')
    my_clip = my_clip.set_audio(audio_background)
    my_clip.write_videofile(f"movie_{id}.mp4",
                            audio_codec='aac',
                            codec="libx264",
                            temp_audiofile='temp-audio.m4a',
                            remove_temp=True,
                            fps=30,
                            bitrate="5000k")
    os.remove('./create_video_temp.wav')