I'm working on a Voice Assistant based on the Raspberry Pi (2 B+), and one of the functions I was looking to integrate was to play my playlist whenever I want it to.
I tried out using youtube-dl and mplayer within os.system like so, inspired by this answer
youtube-dl --playlist-random -o - "https://www.youtube.com/playlist?list=PLFepKcct_CJG0mu-nb-HvQ52FRKTEO6hT" | mplayer -
I understand that youtube-dl will download the video and send the stream piped to mplayer which will play the music.
What is happening is that the code is playing the first video flawlessly, but as soon as the video ends, mplayer just stops at whatever was the last frame. It's not continuing to the next video even though I can see youtube-dl is to downloading videos in the playlist.
Is there any way I can play multiple songs in a playlist using youtube-dl seamlessly?
You can try omxplayer instead...
try this small sh script with named pipe
first mkfifo /home/midia/omxfifo
then save this script below script.sh and chmod +x:
#!/bin/bash
FileToPlay="$1"
for LINK in $(cat $FileToPlay)
do
echo "Playing : $LINK"
omxplayer -o hdmi --vol -2 $(youtube-dl -g -f mp4 $LINK)</home/midia/omxfifo>/dev/null 2>&1 & echo -n '' >/home/midia/omxfifo
echo "$LINK" >> watched.log
done
call the script like this:
./script.sh file_with_yt_url.txt
Related
The idea was to use my RBP as a baby monitor that would stream over the network so that I could watch from any computer/TV at home.
1.To begin with, I was trying to stream a video from my raspberry pi to be access from another PC using VLC Network Stream.
It worked using this command line :
raspivid -o - -t 0 -hf -w 800 -h 400 -fps 24 |cvlc -vvv stream:///dev/stdin --sout'#standard{access=http,mux=ts,dst=:8160}' :demux=h264
After that i wanted it to start from a python program.
Done using that simple program:
from subprocess import call
call(["raspivid -o - -t 0 -hf -w 800 -h 400 -fps 24 |cvlc -vvv stream:///dev/stdin --sout '#standard{access=http,mux=ts,dst=:8160}' :demux=h264"])
Now it needed to launch the python program at startup. So I created a .desktop file in autostart:
mkdir /home/pi/.config/autostart
nano /home/pi/.config/autostart/autostartCamStream.desktop
autostartCamStream.desktop :
[Desktop Entry]
Type=Application
Name=AutostartCam
Exec=/usr/bin/python3 /home/pi/startCamStream.py
Everything is doing exactly what it is supposed to do! On reboot, the startCamStream.py start streaming and I can access it over my VLC player on my PC on port 8160.
But the problem is that on the Raspberry pi, the stream is full screen and I cant close it or do anything else... Ive try CRTL+Q, ATL+F4 etc. Nothing is working (I cant even see a terminal, the recording seems to be in front of every other programs) Before the autostart, I was closing the stream using CTRL-C since it was running in a terminal.
As I only have basic knowledge in linux environment, I was wondering if anyone has a pretty obvious solution I wouldn't know about... Thank you !
just adding -n to the raspivid command solved the problem.
-n stops the video being previewed.
I am facing a wired bug in my Raspberry Pi where my startup Python script can't play sound. I trigger my script to run at startup with ~/.bashrc file. Here is the code in ~/.bashrc.
# run python script
openvt -s -w /home/pi/path/to/script.sh
where script.sh will run python script. The code in script.sh is as simple as cd to python script and run it.
cd /path/to/project/folder
python script.py
Everything works great except the audio.
Here is the code in my Python script which will play the sound
import subprocess
subprocess.Popen(['omxplayer', "/path/to/audio.wav".format(filename)], stdin=subprocess.PIPE, stdout=None, stderr=None, bufsize=0)
Note: I've tried running python script manually without .bashrc it works fine, there is no problem with audio. I also tried running python script in /etc/rc.local, audio works great. It seems like all script triggered from .bashrc can't play audio.
The reason why I don't run script from /etc/rc.local is that it runs as root user and has problem with my Ajax request, ~/.bashrc doesn't have problem with my Ajax.
How do I play sound in startup script? Thanks in advance for any helps
I can show videos all day with omxplayer, no problem. Showing a simple jpeg, apparently is rocket science. I've spent days with fbi library and it doesn't work very well. I'm running buster raspbian (and same issues on stretch raspbian) and programming in Python.
Simple: I want to display picture for an arbitrary amount of time then either show another or return the display to normal. No such luck.
sudo fbi -T 2 -a --noverbose -1 -t 30 foo.png
I do this with:
subprocess.Popen().
I use:
"-T 2"
because it is HDMI. If I don't do -1 and run fbi again, bad news. It appears to cache each image and will display each again like I'm building a playlist. But if I do "-1" it will not block but will insist on showing the image for the full -t time (30 secs above). There seems to be no way to "send" it a keyboard key to quit before the -t time. I can use the physical keyboard. Since it doesn't block, you'd think I could always do a
"sudo killall -9 fbi"
but doing so while an image is still on the screen freezes the image there.
So any other python library or shell command for display an image? Anyway to get this fbi to actually work?? Thanks much!!
Try using "feh",
something like:
feh --hide-pointer -x -q -B black -g 1280x800 "image.jpg"
with subprocess.Popen or os.system
Or if you want to use only fbi, with python,
Use:
import os
and then use os.system in front of the code you are using, works with python code.
Something like this has worked for me:
os.system('sudo fbi -a /dev/fb0 --noverbose -T 2 image.png')
[Intro]
OS: Raspbian Jessie;
Device: Raspberry pi 3
I wrote a python-script, that plays audio files via vlc (python vlc bindings).
Next, I wrote a bash script, that runs python-script.
Next, I added the bash-script to autorun, using crontab.
Everything was working, everything was playing.
[main part]
Next, I decided to test external audio card.
Everything was working out of the box.
When I pulled out the sound card and connected to the headphone jack on the raspberry - the problems started.
Now. when I run bash script, catch next errors:
cannot open ALSA device "default": No such file or directory
BUT if I perform the exactly same actions (as in bash script) manually, everything works as it should.
mystic!
bash script looks like:
cd /app/path
sudo python2 app.py
cd /
python script:
import vlc
a = vlc.MediaPplayer(file)
a.play()
I have been using my RaspberryPi 3 model B with the Picamera to record videos.
I saw in the RaspberryPi website that the default video format that you get from capturing video is h264, however when I use the camera, the file created appears as fyle type unknown.
To capture I tried both the basic command line
raspivid -o video -t 10 #for a 10s video
or a small programm using the PiCamera.start_recording (wich i believe lets you choose the format of the output file) but I still get these not known format files.
If it helps (doubtful) I try to read the recorded files with omxplayer and the footage is displayed at roughly twice the speed it should.
You're not specifiying the h264 extension - without that omxplayer doesn't know what type of video format it is - unless you can suggest what to try via command line
Try:
raspivid -o video.h264 -t 10 #for a 10s video
There's plenty of online help and examples for raspivid
Also - you may have to wrap your h264 file in mp4 "box" to play it
Here's one suggestion on how to do this wrapping using MP4Box
sudo apt-get update
sudo apt-get install -y gpac
Once installed use the following command to wrap your H264 video in an MP4 container file. This allows most media players to play the video.
MP4Box -fps 30 -add video.h264 video.mp4
This will give you a nice video at 30 frames per second that should play in most modern media players.