-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_from_video.py
32 lines (27 loc) · 1.06 KB
/
audio_from_video.py
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
from pydub import AudioSegment
import subprocess
def convert_video_to_wav_ffmpeg(video_file, output_wav):
try:
# Use ffmpeg to extract audio from video
temp_audio_file = "temp_audio.mp3"
subprocess.run([
"ffmpeg",
"-i", video_file,
"-q:a", "0",
"-map", "a",
temp_audio_file
], check=True)
# Convert the extracted audio to WAV using pydub
audio = AudioSegment.from_file(temp_audio_file, format="mp3")
audio.export(output_wav, format="wav")
print(f"Audio has been successfully saved as: {output_wav}")
except subprocess.CalledProcessError as e:
print(f"An error occurred during ffmpeg processing: {e}")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
# File paths
video_file = "video.f251.webm" # Replace with your video file name
output_wav = "output_audio.wav" # Desired output file name
# Convert the video to WAV
convert_video_to_wav_ffmpeg(video_file, output_wav)