Exemple #1
0
def correlate(clip_filename, audio, output_filename):
    match = re.search("^\w+\.(mp3|wav|flac|ogg)$", clip_filename, re.IGNORECASE)
    save_video = True
    if match:
        low_quality_sound = AudioFileClip(clip_filename)
        save_video = False
    else:  # The file seems to be a video
        video_clip = VideoFileClip(clip_filename)
        low_quality_sound = video_clip.audio
    high_quality_sound = AudioFileClip(audio)
    audio_fps = max(low_quality_sound.fps, high_quality_sound.fps)
    lqsa = low_quality_sound.to_soundarray(nbytes=4, buffersize=1000, fps=audio_fps)
    hqsa = high_quality_sound.to_soundarray(nbytes=4, buffersize=1000, fps=audio_fps)
    sample_len = 10000
    sample_start = max(0, np.argmax(hqsa[:, 1]) - sample_len // 2)
    sample = hqsa[sample_start:sample_start+sample_len]
    correlation = np.correlate(lqsa[:, 1], sample[:, 1])
    offset = np.argmax(correlation) - sample_start
    good_sound = AudioArrayClip(mix(offset, lqsa, hqsa), fps=audio_fps)
    if save_video:
        video_clip.audio = good_sound
        video_clip.write_videofile(output_filename,
                                   codec='mpeg4',
                                   bitrate='4000000',
                                   audio_codec='pcm_s32le',
                                   audio_fps=audio_fps,
                                   #audio_bitrate='500000',
                                   preset='superslow',
                                   threads=4)
    else:
        good_sound.write_audiofile(output_filename,
                                   codec='pcm_s32le')
Exemple #2
0
def test_audioclip_io():
    # Generate a random audio clip of 4.989 seconds at 44100 Hz,
    # and save it to a file.
    input_array = np.random.random((220000, 2)) * 1.98 - 0.99
    clip = AudioArrayClip(input_array, fps=44100)
    clip.write_audiofile(os.path.join(TMP_DIR, "random.wav"))
    # Load the clip.
    # The loaded clip will be slightly longer because the duration is rounded
    # up to 4.99 seconds.
    # Verify that the extra frames are all zero, and the remainder is identical
    # to the original signal.
    clip = AudioFileClip(os.path.join(TMP_DIR, "random.wav"))
    output_array = clip.to_soundarray()
    np.testing.assert_array_almost_equal(output_array[:len(input_array)],
                                         input_array,
                                         decimal=4)
    assert (output_array[len(input_array):] == 0).all()
 def file_save_as_button_clicked(self, widget):
     filename = FileOp.choose_file(self, "save_as", file_types="audio", filename=self.filename)
     if filename:
         clip = AudioArrayClip(self.audio_samples.samples.T, fps=self.audio_samples.sample_rate)
         clip.write_audiofile(filename)
         self.set_filename(filename)