32 lines
825 B
Python
32 lines
825 B
Python
import tempfile
|
|
from pathlib import Path
|
|
|
|
import moviepy.editor as mp
|
|
import numpy as np
|
|
import scipy.io.wavfile as wav
|
|
|
|
|
|
def extract_audio_from_video(video_path: str, filename: str):
|
|
tempdir = tempfile.gettempdir()
|
|
|
|
dest_location = f"{tempdir}/{filename}.wav"
|
|
if Path(dest_location).is_file():
|
|
return dest_location, True
|
|
|
|
vid = mp.VideoFileClip(video_path)
|
|
vid.audio.write_audiofile(dest_location, logger=None)
|
|
vid.close()
|
|
return dest_location, False
|
|
|
|
|
|
def process_audio(source_audio_path):
|
|
rate, data_raw = wav.read(source_audio_path)
|
|
data_raw = data_raw.astype(np.int32)
|
|
mono = (data_raw[:, 0] + data_raw[:, 1]) / 2
|
|
duration = len(mono) / rate
|
|
return mono, duration, rate
|
|
|
|
|
|
def resample(data: np.ndarray, factor: int) -> np.ndarray:
|
|
return data[::factor].copy()
|