Esempio n. 1
0
def test_audioclip(util, mono_wave):
    filename = os.path.join(util.TMP_DIR, "audioclip.mp3")
    audio = AudioClip(mono_wave(440), duration=2, fps=22050)
    audio.write_audiofile(filename, bitrate="16", logger=None)

    assert os.path.exists(filename)

    AudioFileClip(filename)
Esempio n. 2
0
def test_audioclip():
    make_frame = lambda t: [sin(440 * 2 * pi * t)]
    audio = AudioClip(make_frame, duration=2, fps=22050)
    audio.write_audiofile(os.path.join(TMP_DIR, "audioclip.mp3"), bitrate="16")

    assert os.path.exists(os.path.join(TMP_DIR, "audioclip.mp3"))

    clip = AudioFileClip(os.path.join(TMP_DIR, "audioclip.mp3"))

    # TODO Write better tests; find out why the following fail
    # assert clip.duration == 2
    # assert clip.fps == 22050
    # assert clip.reader.bitrate == 16
    close_all_clips(locals())
Esempio n. 3
0
def pitch_shift(audio: AudioClip, steps: int) -> AudioClip:
    """Shifts an audio clip by a given amount of tone steps.

    Args:
        audio (AudioClip): Input audio
        steps (int): Amount of steps to shift

    Returns:
        AudioClip: Pitch shifted audio
    """
    with tempfile.TemporaryDirectory() as tmpdir:
        tmp_file = os.path.join(tmpdir, "tmp.wav")
        pitched_file = os.path.join(tmpdir, "pitched.wav")
        audio.write_audiofile(tmp_file)
        y, sr = librosa.core.load(tmp_file, audio.fps)
        y_shifted = librosa.effects.pitch_shift(y, sr, n_steps=steps)
        librosa.output.write_wav(pitched_file, y_shifted, sr)
        return AudioFileClip(pitched_file)
Esempio n. 4
0
def test_audioclip():
    make_frame = lambda t: [sin(440 * 2 * pi * t)]
    clip = AudioClip(make_frame, duration=2, fps=22050)
    clip.write_audiofile(os.path.join(TMP_DIR, "audioclip.mp3"))
Esempio n. 5
0
def test_audioclip():
    make_frame = lambda t: [sin(440 * 2 * pi * t)]
    clip = AudioClip(make_frame, duration=2, fps=22050)
    clip.write_audiofile(os.path.join(TMP_DIR, "audioclip.mp3"))
Esempio n. 6
0
def test_ffmpeg_parse_infos_multiple_audio_streams():
    """Check that ``ffmpeg_parse_infos`` can parse multiple audio streams."""
    # Create two mono audio files
    clip_440_filepath = os.path.join(
        TMP_DIR, "ffmpeg_parse_infos_multiple_streams_440.mp3"
    )
    clip_880_filepath = os.path.join(
        TMP_DIR, "ffmpeg_parse_infos_multiple_streams_880.mp3"
    )
    multiple_streams_filepath = os.path.join(
        TMP_DIR, "ffmpeg_parse_infos_multiple_streams.mp4"
    )

    make_frame_440 = lambda t: np.array(
        [
            np.sin(440 * 2 * np.pi * t),
        ]
    )
    make_frame_880 = lambda t: np.array(
        [
            np.sin(880 * 2 * np.pi * t),
        ]
    )

    clip_440 = AudioClip(make_frame_440, fps=22050, duration=0.01)
    clip_880 = AudioClip(make_frame_880, fps=22050, duration=0.01)
    clip_440.write_audiofile(clip_440_filepath)
    clip_880.write_audiofile(clip_880_filepath)

    # create a MP4 file with multiple streams
    cmd = [
        FFMPEG_BINARY,
        "-y",
        "-i",
        clip_440_filepath,
        "-i",
        clip_880_filepath,
        "-map",
        "0:a:0",
        "-map",
        "0:a:0",
        multiple_streams_filepath,
    ]
    with open(os.devnull, "w") as stderr:
        subprocess.check_call(cmd, stderr=stderr)

    # check that `ffmpeg_parse_infos` can parse all the streams data
    d = ffmpeg_parse_infos(multiple_streams_filepath)

    # number of inputs and streams
    assert len(d["inputs"]) == 1
    assert len(d["inputs"][0]["streams"]) == 2

    default_stream = d["inputs"][0]["streams"][0]
    ignored_stream = d["inputs"][0]["streams"][1]

    # default, only the first
    assert default_stream["default"]
    assert not ignored_stream["default"]

    # streams and inputs numbers
    assert default_stream["stream_number"] == 0
    assert ignored_stream["stream_number"] == 1
    assert default_stream["input_number"] == 0
    assert ignored_stream["input_number"] == 0

    # stream type
    assert default_stream["stream_type"] == "audio"
    assert ignored_stream["stream_type"] == "audio"

    # cleanup
    for filepath in [clip_440_filepath, clip_880_filepath, multiple_streams_filepath]:
        os.remove(filepath)
    close_all_clips(locals())