Reputation: 11
I am working on a audioplayer that uses qr-codes as input. it works until it actually needs to play the audio.wav. Then i get the error "UNknown Wave Format".
import cv2
from pytube import YouTube
import pygame
PURPLE = '255, 0, 188'
PATH = 'A:\\VS\\programms\\ai-help'
MUSIC_PATH = 'A:\\VS\\programms\\ai-help\\audio.wav'
link = ''
pygame.mixer.pre_init(44100, 16, 2, 4096) #frequency, size, channels, buffersize
pygame.init()
cap = cv2.VideoCapture(2)
detector = cv2.QRCodeDetector()
def download():
try:
yt = YouTube(link)
except:
print('Connection error')
try:
mp3_stream = yt.streams.filter(only_audio=True).first()
mp3_stream.download(output_path=PATH,filename='audio.wav')
print('download complete')
except:
print('error downloading audiofile')
def play():
pygame.mixer.music.load(MUSIC_PATH)
pygame.mixer.music.play(MUSIC_PATH)
print('now playing')
I already tried using mp3 and changing the sample_frequency and the bitrate, but i cant get it to work.
Upvotes: 0
Views: 88
Reputation: 11
thanks for all the support guys. I realy apprechiate it. As the version with ffmpeg didnt worked i now used movviepy to convert the mp4 into a mp3 and now it works. For anyone asking the code is only two lines long...
FILECONVERT = AudioFileClip(mp4_file)
FILECONVERT.write_audiofile(mp3_file)
with the import
from moviepy.editor import *
Upvotes: 0
Reputation: 101132
The file you download is not a wav
or mp3
file. Since you download it from youtube, I guess it's either an mp4a
or opus
file.
Since pygame does not support these codecs, you'll have to first convert the file to a supported format.
Here's a simple, working example using ffmpeg and ffmpeg-python:
from pytube import YouTube
import ffmpeg
import pygame
video_url = "https://www.youtube.com/watch?v=6QbWfgq3PvQ"
audio_file = "input.mp4"
mp3_file = "output.mp3"
yt = YouTube(video_url)
yt.streams.filter(only_audio=True).first().download(filename=audio_file)
ffmpeg.input(audio_file).output(mp3_file).run()
pygame.mixer.init()
pygame.mixer.music.load(mp3_file)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
continue
Upvotes: 1