Interrupting the omxplayer playback in python - python

I am trying to change the video of omx player in python based on an event. But the problem is the next video gets loaded only after the first video finishes.
import os
os.system('omxplayer video1.mp4')
if(flag==1):
os.system('omxplayer video2.mp4')
Is there a way to interrupt video1 in middle of playback and start second video.
Note:I am working on a raspberrypi with debian wheezy and python 2.7

Omxplayer has keyboard controls so you could send the corresponding keystroke q to stop it normally before opening the next instance. Here is a similar post where a person tried to achieve similar feature, it could be helpful.

Related

playing sound in Python with the ability to cut it off mid-play

I am writing a bit of python code that plays a sound file (MP3 or the equivalent) and should cut that sound off if the user strikes a (hardware) button that is wired into the system. This will be on a Raspberry Pi running Raspbian. The libraries I’ve used historically for playing sound all play to completion. The closest approach I can think of would be using an external sound player (OMXplayer perhaps) and then searching for and killing its process if the button is pressed, but this feels inelegant. Can anybody suggest a better approach?
Using Pyaudio you could simply check a flag before writing each buffer to the stream, and if the audio should stop, fade out the current buffer and then either continue the stream with silence (write 0's) or stop the stream and exit the program. This could be easily be achieved with a latency of ~1/10s after button press.
You would also need to create a UI thread in the application to handle commands since the stopping flag is switched on user input.

asciimatics - how to export to a GIF?

I'm new to asciimatics and would like to export animations I'm making to a GIF, at the command line. Note that I want to ONLY record the animation itself, not me starting some command in the terminal to record the gif as well.
I've looked at the docs, but don't see an asciimatics way to do this?
Note that I'm aware of things like ttygif, but tried to use it and couldn't get it to work with asciimatics, probably due to me not understanding how to use it.
You can do this using toughtty - it allows you to control when the recording starts, which works great for staring the recording manually.
My use case is generating fun gifs to paste into Slack that are branded with lol animations and text ;-) So I don't want anyone to actually see it was a terminal window at all, just a retro looking animation that I made super fast!
So in summary:
get your animation ready to record
start the recorder, toughtty by running $ toughtty record frames.json
start your animation. Note that recording hasn't started yet.
once your animation is running, press Ctrl+T to start recording
when you think it's good, press Ctr+C to stop the recording
generate your gif by running toughtty encode --delay 100 out.json test.gif
open the test.gif in your browser to view the animation!
I found toughtty by browsing the active forks of ttystudio
https://techgaun.github.io/active-forks/index.html#chjj/ttystudio
and then installed it under node v8
Example: https://imgur.com/a/0Su6pI6

Raspberry Pi 3: Unusual behaviour with mpd, PiFi DAC+ audio

I'm looking for some help 'cause I'm getting a bit frustrated on this... :-(
I have a headless Raspberry PI 3 with a PiFi DAC+ audio card, basically an HiFiBerry clone. On the PI I installed mpd and mpc as a client.
On top of those I wrote a python script that invokes some mpc commands to control the underlying mpd daemon (load a playlist, play a stream,...).
Now the issue.
The overall audio setup based on the hifiberry-dacplus overlay works well, the sound is good and I'm fine with it. Mpc & mpd work, I can control all the functionalities of mpd (at least the ones I need) through mpc without a flaw...but, if I try to run my python script suddenly I cannot hear anything anymore, even if no specific errors are traced.
The 'scary' thing is that, after aborting the script execution, I'm no more able to play any sound (I tried with several wav files using aplay), and again no specific errors show up in the log files...looks like someone just 'muted' the volume, but alsamixer shows all playback levels to 100%. I need to reboot the PI to get my sound back.
I checked for clues in the usual places:
/var/log/messages
/var/log/syslog
dmesg
boot.log
/var/log/mpd/mpd.log
I also run aplay -vvv when audio was blocked and compared the output with a session where audio was running fine but I didn't notice any difference...
I know it would be very difficult to diagnose the problem without having access to my system, but do you have any ideas on where else to look to understand if something went wrong?
Just for info, here's my aplay -l output:
**** List of PLAYBACK Hardware Devices ****
card 0: sndrpihifiberry [snd_rpi_hifiberry_dacplus], device 0: HiFiBerry DAC+ HiFi pcm512x-hifi-0 []
Subdevices: 1/1
Subdevice #0: subdevice #0
Thank you!
Michele
EDIT: seems like there is some incompatibility between the audio board and a 16x2 LCD display I'm using to show the name of the stream I'm playing. The display is a very common one, based on the HD44780 chip.
My code uses the AdaFruit python library available here to drive it and I still have to figure where the problem is: the audio board, as per HiFiberry docs is connected through GPIO 2,3,18,19,20,21 (plus ground & +5V for power), so it shouldn't cause any conflict with the LCD which uses different pins, but I wouldn't bet on it.
Anyway, removing the LCD management part from the python code (but leaving the display physically attached to the RaspBerry pins) apparently solved the problem...
I'll keep this question updated, maybe could be useful for someone else, who knows!
Ok, I got it. As usual, I just went too fast with CTRL-C & CTRL-V without properly reading the code...
I didn't notice I left this statement in my python code
lcd_backlight = 2 #GPIO pin to control lcd backlight
Actually The GPIO 2 (which is one of the two I2C enabled pins on the Raspberry) is not connected to the LCD, but it is used by the audio board for configuration purposes: this way, whenever I tried to initialize the LCD, the audio board was somehow reconfigured, making it "mute". The only way to reset the faulty configuration was to reboot the PI itself.
Just leaving the default 'None' value for the backlight control pin (I do not need it) did the trick.

Stop OpenCV for seventeen seconds? python

I have a motion detection program in OpenCV, and I want it to play a sound when it detects motion. I use winsound. However, OpenCV still seems to be gathering frames while the sound is playing, so I want to know a way to stop all OpenCV processes for about 17 seconds. I tried time.sleep and running it with the -u tag. Neither worked. Any ideas? Thanks.
I was able to solve the problem by putting what I want to run before resuming processes (playing the sound) in another script, performing import sound and breaking out of the loop to stop the program, and I can't figure out how to start it again. For my purposes, I can restart it manually.

How to close program in python opened by os.system()?

In python 3.4 , I was trying to open a "wav" file using vlc in Linux. Here is my code:
import os,time
os.system("cvlc audio/some.wav")
time.sleep(3) #audio was one and half sec
a = 3+3
print (a)
It plays the audio but then doesn’t do the rest. What should I do to make it do them? more precisely what should I do to close the vlc program?
With solving the problem it will also be very grateful to know is there any easier way to play audio within the code specifically in python 3.4?
(platform independent code will be even more grateful!)
So the VLC player doesn't exit. The VLC player has a command line argument to close the player once the song/video has been played.
Playlist
These options define the behavior of the playlist. Some of them can be overridden in the playlist dialog box.
--play-and-exit, --no-play-and-exit
Play and exit (default disabled)
Source: https://wiki.videolan.org/VLC_command-line_help
Can you try the following?
os.system("cvlc audio/some.wav --play-and-exit")

Categories