File size: 1,900 Bytes
96d549d 2b16bc4 96d549d 2b16bc4 96d549d 2b16bc4 96d549d 2b16bc4 96d549d 2b16bc4 96d549d 2b16bc4 96d549d 2b16bc4 96d549d 2b16bc4 96d549d 2b16bc4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
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()
|