Converting my .h264 video (python) - python

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.

Related

MP3 Audio Python

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

No sound on Raspbian when using root

I'm having a really weird issue on my Raspberry. I need to play sounds with it and I need to do it inside a script which requires sudo to work. However, for some reason, when I try to play sounds with sudo, it doesn't work.
No errors, it just doesn't make any sound (I tried with several libraries, none worked). It does work if I'm using omxplayer.
If I don't use sudo, everything works fine.
Anybody had a similar issue?
Edit: I added one of the code I used (this works just fine on another Raspberry)
import pygame
import time
pygame.mixer.init()
pygame.mixer.music.stop()
pygame.mixer.music.load("music.mp3")
pygame.mixer.music.play()
print("Going forward")
while True:
time.sleep(1)
Are you using the 3.5mm jack or HDMI?
One thing I have noticed in the past is that if I run alsamixer as user=pi the default device is the 3.5mm aux jack. However if I run sudo alsamixer the default device is now HDMI. Never looked in to why, just know that's a thing.
Spent nearly a day trying to enable the analog out when using the root user.
First make sure the default user (pi) has audio:
aplay sound.wav
If that works then copy the audio configuration to the root user home, try this:
sudo su
cd
cp /home/pi/.asoundrc .asoundrc
reboot

Using aplay to play a sound through subprocess within a python script

There are alot of libraries to play audio within a python script, I was wondering if it would be possible to simply use call aplay through the subprocess feature to play a sound? When I try it I get OSError: [Errno 2] No such file or directory but there is definitely a sound there, it works when I do it through the command prompt. I may be doing something wrong as far as syntax in the python script?
from subprocess import call
call(["aplay /home/pi/file.wav"])
The syntax that will work is :
from subprocess import call
call(["aplay", "/home/pi/file.wav"])
I found that installing
alsa-utils
in this case : sudo apt install alsa-utils
make it work.
example of "text to speech"
import pyttsx3
# init function to get an engine instance
engine = pyttsx3.init()
# say method for input text to be spoken
engine.say('Here the message you want you hear')
# run and wait method, it processes the voice commands.
engine.runAndWait()
I hope it helps.

When running a pygame script as root, no sound is output?

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.

play music using pygame but no sound [duplicate]

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

Categories

Resources