faster-whisper-readme / downloader_manager.py
ManBib's picture
fixed processing through link or audio bytes
2b16bc4
raw
history blame
1.9 kB
import logging
import tempfile
from io import BytesIO
import requests
from moviepy.editor import VideoFileClip
from tqdm import tqdm
def extract_audio(video_path):
try:
with VideoFileClip(video_path) as video:
audio = video.audio
audio_path = video_path.replace('.mp4', '.mp3')
audio.write_audiofile(audio_path, codec='mp3')
return audio_path
except Exception as e:
logging.error(f"Failed to extract audio: {e}")
return None
def download_mp4_and_extract_audio(link: str):
"""Download an MP4 file from a given link and return the path to the video and audio files."""
logging.info("Starting the download of the MP4 file...")
video_content = BytesIO()
try:
r = requests.get(link, stream=True)
r.raise_for_status()
total_size = int(r.headers.get('content-length', 0))
with tqdm(total=total_size, unit='B', unit_scale=True, desc="Downloading...") as bar:
for data in r.iter_content(chunk_size=1024):
bar.update(len(data))
video_content.write(data)
video_content.seek(0)
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_video_file:
temp_video_file.write(video_content.getvalue())
temp_video_file_path = temp_video_file.name
logging.info("Starting audio extraction in a separate process...")
audio_path = extract_audio(temp_video_file_path)
logging.info("Download and audio extraction completed")
return temp_video_file_path, audio_path
except requests.exceptions.RequestException as e:
logging.error(f"Request Error: {e}")
return None, None
except Exception as e:
logging.error(f"Failed to download MP4 and extract audio: {e}")
return None, None
finally:
video_content.close()