I've got this Python program and would like to know how I can play a video with the original audio.
I've tried other things about this.
Like pyglet(just a quick sample, no video):
import pyglet
import ffmpeg
player = Player()
source = load("Video Recording.m4a")
player.queue(source)
And stuff on StackOverflow two.
But it's all not working.
Are any of these libraries outdated?
Or any solutions?
Related
quick question.
I'm running pygame under linux just to play some audio files.
I've got some .wav files and I'm having problems playing them back at the right speed.
import pygame.mixer, sys, time
#plays too fast
pygame.mixer.init(44100)
pygame.mixer.music.load(sys.argv[1])
pygame.mixer.music.play()
time.sleep(5)
pygame.mixer.quit()
#plays too slow
pygame.mixer.init(22100)
pygame.mixer.music.load(sys.argv[1])
pygame.mixer.music.play()
time.sleep(5)
pygame.mixer.quit()
I've ggogle code searched some stuff but everybody seems to be fine calling the init function with its default parameters. Can others try running this script and seeing if they get the same behavior or not? Does anybody know how to speed it up? Or adjust the speed for each file?
Thanks.
I had some mp3 audio tracks playing back slowed down. I updated the mixer frequency to be based on the mp3 sample rate using mutagen like so:
import pygame, mutagen.mp3
song_file = "your_music.mp3"
mp3 = mutagen.mp3.MP3(song_file)
pygame.mixer.init(frequency=mp3.info.sample_rate)
pygame.mixer.music.load(song_file)
pygame.mixer.music.play()
And it fixed the problem.
To improve Chris H answer. Here is a example of how to use the wave library.
import wave
import pygame
file_path = '/path/to/sound.wav'
file_wav = wave.open(file_path)
frequency = file_wav.getframerate()
pygame.mixer.init(frequency=frequency)
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
Remember that if you want to change frequency or any other parameter used in pygame.mixer.init you must call pygame.mixer.quit first. Pygame documentation
Open your audio file in a free audio tool like Audacity. It will tell you the sampling rate of your media. It will also allow you to convert to a different sampling rate so all your sounds can be the same.
I figured it out...
There is a wave module http://docs.python.org/library/wave.html and it can read the sample rate for wav files.
If you're using Ogg Vorbis (.ogg) encoding, the same problem of stuttering audio happens. You'll have to read the frequency of what you're trying to play before initializing the mixer object.
Here's how to play .ogg audio with appropriate frequency using pygame.
from pyogg import VorbisFile
from pygame import mixer
# path to your audio
path = "./file.ogg"
# an object representing the audio, see https://github.com/Zuzu-Typ/PyOgg
sound = VorbisFile(path)
# pull the frequency out of the Vorbis abstraction
frequency = sound.frequency
# initialize the mixer
mixer.init(frequency=frequency)
# add the audio to the mixer's music channel
mixer.music.load(path)
# mixer.music.set_volume(1.0)
# mixer.music.fadeout(15)
# play
mixer.music.play()
I'm trying to set audio clip on image clip in moviepy. I read this page, then I wrote code like this.
from moviepy.editor import *
clip = ImageClip('img/output_1.png')
audio_clip = AudioFileClip('audio/output_1.mp3')
clip = clip.set_duration(audio_clip.duration)
clip = clip.set_audio(audio_clip)
Audio and duration are returns value.
>>> clip.audio
<moviepy.audio.io.AudioFileClip.AudioFileClip object at 0x1241dbd30>
>>> clip.duration
4.18
However when I write as file.
clip.write_videofile('mov/sample.mp4',fps=4)
I can't hear the sound. Are there any mistake on my code? I don't know how should I change my code... If you have any idea please help me!
My environment is
MacOS 11.3.1
python 3.9.5
moviepy 1.0.3
I had run same code on google colab, I got same problem. The video has 4.18 seconds, however it's no sound...
I downloaded audio file from ondoku3 and youtube as mp3
I found QuickTime can't play sound. When I play VLC, I can hear the sound.
And also when I merge audio and video with ffmpeg. I got same problem. So I think this problem from ffmpge convert problem or QuickTime problem.
I've been working on this for a while but everything I find seems to be a dead end so I could use some help troubleshooting.
import pygame
file = open("C:\\Users\\MyName\\Music\\oggTest.ogg")
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(file)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
This code is supposed to grab an audio file and play it using the mixer from pygame. Very simple, and I've seen other questions here asking about how to do it but mine's still not working for some reason. Right now, I have no errors when I'm running the code. The only problem is that nothing happens. No audio gets played. I have a hunch that it has something to do with my IDE (pycharm), but I don't know for sure if that's the case. Also, the filepath part is a bit confusing to me. I don't see any reason why it wouldn't work but perhaps I'm missing something obvious. Thanks in advance for helping out.
As per the advice of a YT video, I converted the mp3 file I originally had to an ogg file. Not sure if that was necessary, and honestly I'd prefer not to have to do it in the future.
Okay, I found the answer after a bit more research. Taking out the pygame.init() worked. I guess it didn't like doing both pygame.init() and pygame.mixer.init() for some reason.
I am trying to play an audio file of a female, speaking something in hindi using pygame library in python. When i manually click on audio file and listen to it, it is a female voice, but when but I play it via below script, I get a male voice. I guess it is converting female frequency into male. Why is that and how to avoid it?
Note: I am using Raspbian on Raspberry Pi.
This is the link to the audio file: https://drive.google.com/open?id=18pLBoCMxWZzB-RO3qqVmi0zREgJckb3M
My coding:
import pygame.time
from pygame.mixer import *
pre_init()
init()
filename = 'speech.wav'
music.load(filename)
music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
import pygame.time
from pygame.mixer import *
pre_init()
init(frequency=32000)
filename = 'speech.wav'
music.load(filename)
music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
pygame.mixer.init() takes a frequency parameter. And since the player defaults to 22050, the playback will be in slowm-otion - making it sound different. You can circumvent this by changing the speed manually, or you can probably get the actual speed from music.load(), the meta-data should be in the file.
Otherwise, just do ffmpeg -i speech.wav and you'll see the correct frequency at the bottom.
To change the frequency of audio-files to match the same frequencies, you could use ffmpeg to re-encode the files. Now, I'm no magician with ffmpeg - but the just goes something along the lines of:
ffmpeg -i speech.wav -af asetrate="32000*1.38125,atempo=1/1.38125" output.wav
Or use Audacity or something others recommend: https://superuser.com/questions/292833/how-to-change-audio-frequency
A second option to change frequency on the player instead, is to call pygame.mixer.quit() after each media file and re-initiate it with a new frequency matching your new file. Or lastly, read the docs and see if it's possible to change frequency playback on a already initialized instance of the mixer. This is beyond my knowledge tho. I just know what your original root problem is :)
im currently having trouble with the python 3rd party library pygame.
Other posts on stackoverflow didn't help me so i opened a new post.
I want to load a *.mp3 file into the program and play it. My code so far (copied from other posts):
import pygame,time,sys
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
print ("Mixer settings", pygame.mixer.get_init())
print ("Mixer channels", pygame.mixer.get_num_channels())
pygame.mixer.music.set_volume(1.0)
pygame.mixer.music.load("test.mp3")
pygame.mixer.music.play()
clock = pygame.time.Clock()
while pygame.mixer.music.get_busy():
clock.tick(30)
Here is a link to a screenshot from the Windows Sound panel: http://i.imgur.com/fUvJXof.png
I see that there is some output, but i cant hear anything on my headphones.
I hope some has an idea what the problem is.
Thanks
Check your hardware/audio drivers. It doesn't sound like (pardon the pun!) an issue with python or pygame. Does your audio work outside python? Also, if you've got other programs open, they can sometimes take control of your audio drivers and not allow other programs (in this case python) to output.