I am trying to play a file, but I do not know the command. Here is the image as to what my project looks like, and I am wondering if there is a way I can play the audio.
My suggestion is pygame. Install via:
pip3 install pygame
Download a .wav file to your .py file direction. Define a push button and connect to this function:
def somesound(self):
pygame.mixer.init()
pygame.mixer.music.load('somesound.wav') # Pygame allows *.wav
pygame.mixer.music.play(1) # For 1 second
Related
I am trying to use python to convert files from mp4 to mp3. After some research most places recommend moviepy. I used the command pip install moviepy and it seemed to go off without a hitch. I go to VS Code and enter what a youtube video told me to enter (I know its not recommended to do that, I just wanted to see if it would work). This is what I have
`
#This code should convert a .mp4 file into a .mp3 file
#This imports the moviepy package
from moviepy.editor import *
#here are the names of my files (I have subbed out actual files names)
mp4_file = "file_name.mp4"
mp3_file = "file_name.mp3
#Here is the the audio being stripped from the .mp4 file
video_clip = VideoFileClip(mp4_file)
audio_clip = video_clip.audio
#this is writing the audio to a .mp3 file at the path that is specified.
audio_clip.write_audiofile(mp3_file)
#this closes the conversion code
audio_clip.close()
VideoClip.close()
`
After running the code I get this error:
RuntimeError: No ffmpeg exe could be found. Install ffmpeg on your system, or set the IMAGEIO_FFMPEG_EXE environment variable.
There is a bunch of gibberish above it but that is the final line that gets spit out.
After looking up what the issue is I tried to input:
`
from moviepy.config import change_settings
change_settings({"FFMPEG_BINARY": "/usr/bin/ffmpeg"})
`
And it also did not work. I have tried searching for where ffmpeg is and it is not in /usr/bin/ffmepg or /usr/local/bin/ffmpeg like most sources I have looked at tell me it should be.
I have tried installing ffmpeg on its own by doing pip install ffmpeg and 'brew install ffmpeg'. Both of those go off without a hitch as well but the error still pops.
I am using a macbook air m1 and I have I think everything I need installed already so I am so lost on what is causing this error to pop.
Can someone please help?
I have tried installing ffmpeg on its own as well as searching for the file directly.
I should expect to get the .py file to run fine.
I instead get the error seen above:
RuntimeError: No ffmpeg exe could be found. Install ffmpeg on your system, or set the IMAGEIO_FFMPEG_EXE environment variable.
try to use this code which is i got
import moviepy.editor
import os
os.environ["IMAGEIO_FFMPEG_EXE"] = "/usr/bin/ffmpeg"
mp4_file = "file_name.mp4"
mp3_file = "file_name.mp3"
# Replace the parameter with the location of the video
video = moviepy.editor.VideoFileClip("mp4_file")
audio = video.audio
# Replace the parameter with the location along with filename
audio.write_audiofile("mp3_file")
if it is still not find the ffmpeg then find its actual path and note that
I seem to be having difficulty with executing a pygame program on boot up. I have a raspberry pi 3 model B running the latest versions of the required libraries. I am simply trying to play a wav file through pygame when a button is pressed. I substituted an “led.on” script for the pygame to ensure my program was being started on boot-up and it ran flawlessly. However, when I try to use pygame on boot up it will not play the audio file. Although pygame will play the file in the terminal, it will not play on startup.
I am using crontab to run the program from boot. Using the following line:
#reboot python /home/pi/test.wav &
And the python code:
from gpiozero import Button
import pygame.mixer
pygame.mixer.init()
song=pygame.mixer.music.load(‘/home/pi/test.wav’)
btn=Button(27)
while True:
btn.when_pressed=(pygame.mixer.music.play)
Any help would be greatly appreciated, thank you!
This should solve your question
pygame.mixer.music.play()
play should be called with () as it's a fn
use
pygame.mixer.music.play(-1)
for looping the music
I made this code to open the pi camera and to close it. When i run the code it makes .h264 videos but i can't play these on windows. Heres the code
import picamera
from time import *
from subprocess import call
with picamera.PiCamera() as camera:
camera.start_recording("beepvid.h264")
sleep(5)
camera.stop_recording()
Another simpler way is to simply make it record to beepvid.mp4
This is really a question for the rasberry pi stack but its ok!
If you type in cd (where ever the file is stored) and type ‘omxplayer (yourvid).h264, it will show you the video. The best thing for you to do is to use a command called ‘MP4Box’.
You can do this with the CMD but ill tell you how to do it in Python. Below your current code add the following.
command = "MP4Box -add beepvid.h264 beepvid.mp4"
call([command], shell=True)
print("vid conv")
This will convert the ‘beepvid.h264’ to mp4 and you can install VLC player if you want to play it. The way to install VLC player is the following: sudo apt-get install vlc. I suggest you type in ‘ sudo apt-get update’ and ‘sudo apt-get upgrade’ before hand
I hope this has answered your question.
I have written a very simple script for my raspberry pi that loads an uncompressed WAV and plays it - however when I run the script as root (to be able to use GPIO and ServoBlaster), there is no sound output.
I have set the default audio device to a USB sound card, and this works - I have tested this using aplay fx.wav.
Running the pygame script without sudo, the sound plays fine.
What is going on here?
The issue was the sudo command changing the directory in which the script was being run - so running python with sudo -s or simply using an absolute path for the sound fixed it.
This question already has answers here:
how to play wav file in python?
(5 answers)
How can I play an mp3 with pygame?
(7 answers)
Closed 1 year ago.
import pygame
pygame.mixer.init()
pygame.mixer.music.load("only one.mp3")
pygame.mixer.music.play(0)
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
When I run the code, there's no sound and the program ends in like a second. Initially I didn't have the while loop until I saw the suggestions in the answers to similar questions. The program does enter the while loop on my friend's windows system, but not on my mac, and it doesn't have any sound either even on my friend's windows system. Does anybody know how to solve it?
Works well on Ubuntu 10.04 and Pygame 1.9.1.
Some things you can try:
initialize whole pygame pygame.init()
i_4_got's suggestion (create a display) pygame.display.set_mode((200,100))
put a pause (tick) between play an get_busy
poll events inside the loop pygame.event.get()
Example:
import pygame
pygame.init()
pygame.display.set_mode((200,100))
pygame.mixer.music.load("only one.mp3")
pygame.mixer.music.play(0)
clock = pygame.time.Clock()
clock.tick(10)
while pygame.mixer.music.get_busy():
pygame.event.poll()
clock.tick(10)
import winsound
winsound.PlaySound(filename [as string with path if necessary], flag [integer z.B. 1 for asynchronous] )
This works fine with Windows 8 and Python 2.7.6 and *.wav files
The different flags you can check if you type winsound. and look for the autofill list.
The flags are like SND_FILENAME or similar.
If you type them into the editor like: *winsound.SND_FILENAME
you get the integer to apply in the PLaySound command from above
Have fun
Pygame sound not working fix
INFO
Try reinstalling pygame or upgrading it
I had version 1.9.1 which I upgraded to 1.9.3 by the following command
pip install pygame --upgrade
Note:
If you install it with easy_install it doesn't work . Not sure but
when I tried with easy_install it doesn't work
The code is the same as yours but i just replaced it with my music file to check it.
You can see it in the image :
In this image
.wav files seem to work well
I checked it with .mp3 files and found that it does'nt work.
You can convert .mp3 to .wav from : Converter
Or use PyMedia
If you do not want to convert it using any website.
You can also convert it with pydub.
You can download pydub from this
link
Or type this command in the command line:
pip install pydub
Usually, Pygame will not play mp3 files. You could test to see if .wav and .ogg files will play first, to make sure your code is correct (based on what you pasted, it seems to be right). I suggest converting your mp3 sounds to ogg for Pygame.
You have to add this to the end, in order for pygame to be happy:
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
Also, import sys.
it would be easier if u changed the mp3 to ogg format ,try this.. its simple
from pygame import *
pygame.init()
pygame.mixer.music.load("only one.ogg")
pygame.mixer.music.play(-1)
pygame.mixer.music.set_volume(0.3)
vote the answer if its working, if not tell me whats wrong