File size: 1,919 Bytes
b574243 |
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 |
import os
import logging
import subprocess
import time
def download_audio(youtube_url):
"""Downloads audio from the given YouTube URL and saves it to audio_files directory."""
# Create audio_files directory if it doesn't exist
if not os.path.exists('audio_files'):
os.makedirs('audio_files')
# Use yt-dlp to download audio
command = [
'yt-dlp',
'-x', # Extract audio
'--audio-format', 'wav', # Convert to WAV format
'-o', 'audio_files/%(title)s.%(ext)s', # Output format
youtube_url
]
result = subprocess.run(command, capture_output=True, text=True)
if result.returncode != 0:
logging.error(f'Error downloading audio: {result.stderr}')
raise Exception('Failed to download audio')
# Wait a moment for the file to be created
time.sleep(1)
def get_audio_filename():
"""Returns the latest downloaded audio filename from audio_files directory."""
audio_files = os.listdir('audio_files')
if audio_files:
# Sort by modification time, return the latest .wav file
audio_files.sort(key=lambda x: os.path.getmtime(os.path.join('audio_files', x)))
for file in audio_files:
if file.endswith('.wav'):
return os.path.join('audio_files', file)
return None
if __name__ == "__main__":
youtube_url = input("Enter the YouTube URL: ")
try:
download_audio(youtube_url)
# Get the latest audio file (optional step, can be used later for transcription)
audio_file = get_audio_filename()
if audio_file:
print(f"Audio file downloaded: {audio_file}")
else:
logging.error('No audio file found after download.')
raise Exception('No audio file found.')
except Exception as e:
logging.error(f'An error occurred: {e}')
|