Rasberry pi - force raspivid to quit - python

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.

Related

Raspberry pi 3 crontab doens't work without log

I already found a lot of posts with crontab errors but i still can't figure out where is my issu.
I want to start a python script on /home/pi/somedir/main.py and to fix any relative path issu in my script i made a start.sh next to it containing
cd /home/pi/somedir
/usr/bin/python3.7 main.py
I made it executable with sudo chmod -x start.sh and it works find went i do /home/pi/somedir/start.sh manually, but i want to autostart it at start.
I tried multiple ways (like /etc/rc.local) and it never worked. As crontab seems to be the better way to do this, i tried crontab -e (the crontab for the pi user so) and i added
#reboot bash /home/pi/somedir/start.sh > /home/pi/somedir/logstart.txt &
to have the logs and to execute the script without blocking the rest of the system. But the script doesn't start and there is nothing in the log file.
The script is a python3 discord bot with logging output, running just find when launched manually. It is a raspberry pi 3B+, with a fresh install of raspberry pi os desktop and i have nothing else running on it.
Thanks for any help and sorry if my english is not correct !
ps: If you know better options to host python3 scripts like discord bots on raspberry pi it would also be usefull of course, i am clearly a beginner !
The issu was that the script won't start at reboot as it is before the system is fully booted. One solution was to start the process at precise hour when the system is fully start, like every morning. An other would be to add a delay before running the start.sh script, somethings like
#reboot sleep 60 && /home/pi/somedir/start.sh > /home/pi/somedir/logstart.txt &
works for me aswell.

Streaming a YouTube playlist on Raspberry Pi and Python

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

Raspbian Buster: Forcing a Terminal Window for a Python Script # startup

Running Raspbian Buster on an RPi3B+.
Have a good autostart running at /etc/xdg/lxsession/LXDE-pi which calls a bash script called launcher.sh:
#!/bin/bash -x
sleep 10 # Just giving time for other stuff to settle in.
/usr/bin/python3 /home/pi/Desktop/HOSTCORE/SpeechCenter.py &
#Listens (binds) to :5555 for speech to input
sleep 55
/usr/bin/python3 /home/pi/Desktop/HOSTCORE/visionCommandTest.py
#Talks to :5555 for speech to output
sleep 10
The first call to SpeechCenter.py works perfectly, running in the background, and the system announces it is connected to the Speech Center.
I need the second call, visionCommandTest.py, to run in a terminal window, so the user can input text for the system to speak, but the command does not open a terminal window. The command IS working because a ps -ax reveals that python visionCommandTest.py is running with (in this case) "PID 934 pts/0 Tl" - however, without it opening a terminal window, there is no ability for user input. (Also, SpeechCenter.py shows a PID of "911 ? Sl"... so there's definitely a difference in the way they show up in my ps -ax listing)
Just need to know how to tell the bash script to open a terminal window and then run visionCommandTest.py in it...
Thanks!

Trying to display picture on Raspberry Pi with fbi ... doesn't really work

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')

How can I play an audio file using Raspberry Pi motion? Possible permissions issue?

Alright, so here's what I have:
I have a script, called detect.sh. This file is located in /home/pi/detect/detect.sh. It's permissions are: -rxwrxwrxw 1 motion motion 66 Feb 19 10:20 /home/pi/detect/detect.sh
This script, contains this code:
#!/bin/sh
date >> /home/pi/detect/detect_log.txt
python /home/pi/detect/detect.py
As you can see, this script executes my Python script.
My python script is located in /home/pi/detect/detect.py, as you can also see. My python script has the following permissions: -rwxrwxrwx 1 motion motion 178 Feb 19 10:28 /home/pi/detect/detect.py
My Python script contains the following code:
import os
import subprocess
text_file = open('/home/pi/Desktop/output.txt', 'w')
text_file.write('Test 1')
text_file.close()
os.system("sudo aplay /home/pi/detect/bark.wav")
When I start motion, using sudo service motion restart, nothing happens. I have, in the configuration file for motion, to
on_event_start sh /home/pi/detect/detect.sh
I know this is working. Because my detect_log.txt is created AND updated correctly, which means my script is running. But my python script is not. I do not hear a dog bark, nor is a .txt file created.
Also, my Python script runs when I run it using the standalone module, using Python. I hear a bark and the file is created.
Is this a permissions issue? What is going on? Could it be a permissions for Python issue? I am thoroughly confused, and have been searching for an answer for weeks. Any help is appreciated.
I still haven't been able to figure out how to execute a python script when motion starts. Instead, I just run a bash script on startup which creates a new .txt file. On start-up, I also have the python script of interest running a while loop; once the .txt file is created, it will exit the while loop and run the remainder of the script.
Let's walk through this.
When user pi executes the Python script directly, everything works fine.
When user pi executes the shell script wrapper everything executes as it should. (I'm not sure why you are adding an extra layer of complexity, you can accomplish all of that within the Python script itself. 1st rule of Linux - K.I.S.S., 3rd Zen of Python - Simple is better than complex)
This tells you there is nothing fundamentally wrong with the scripts themselves, they work. But you do have a permission error, just not where you are thinking. There is another script which you only passingly reference - the motion service script located at /etc/init.d/motion
In there you will find:
restart)
$0 restart-motion
;;
Which points to:
restart-motion)
if check_daemon_enabled ; then
log_action_begin_msg "Restarting $NAME"
if $0 stop && $0 start ; then
log_action_end_msg 0
else
log_action_cont_msg "(failed)"
RET=1
fi
fi
;;
Which references:
start)
if check_daemon_enabled ; then
if ! [ -d /var/run/motion ]; then
mkdir /var/run/motion
fi
chown motion:motion /var/run/motion
log_daemon_msg "Starting $DESC" "$NAME"
if start-stop-daemon --start --oknodo --exec $DAEMON -b --chuid motion ; then
log_end_msg 0
else
log_end_msg 1
RET=1
fi
fi
;;
Specifically, we are looking at:
if start-stop-daemon --start --oknodo --exec $DAEMON -b --chuid motion ; then
The start-stop-daemon '--chuid' flag changes to a user before starting the process, making that user the owner of the process. In this case the process owner is the 'motion' user. For future reference, another common method is to set the user in a variable in the application configuration file and '--chuid $'.
If you can get the service up, an easy way to get this info is to check the owner of the running process, but you should know where to look on your distro for the various service manager scripts (init/upstart/systemd) when you can't get the service up.
We know your scripts work when you execute them, we know that the motion user is able to execute your shell script, we know that the motion user has permission to execute the Python script. So there must be something in the Python script itself that is failing when executed.
The one thing that stands out:
os.system("sudo aplay /home/pi/detect/bark.wav")
As user motion, you are attempting to sudo this command. Does the motion user have sudo provileges?
$ sudo -n -l -U motion
User motion is not allowed to run sudo on nagiospi.
There is your permission problem.
So you have two approaches, give the motion user sudo privilege to play the audio file (bad, see below) OR find a way to play the file without sudo - I'd look for a Python library or user accessible utility if aplay requires root.
A few helpful suggestions:
As slugonamission commented - making an executable world-writable is bad juju, I'll piggy back and say that world-read/write/executables probably shouldn't be in your home directory, add to that list files required by service scripts.
Why are you using a shell script to execute a Python script to execute a shell command - pick a script platform and stick with it. Do not overcomplicate things, what we do is magical enough to the norms.
Granting sudo privileges should be the absolute last thing you try. You should never run a daemon as root unless you absolutely, positively can not do it some other way - and even then I would start looking at a different solution altogether. This is an absolutely unbreakable rule when talking about anonymously accessible services.

Categories