I have a script that records audio for 15 sec, then kills itself (this needs to happen since that is the only way the recording stops using gstreamer).
Now, I want to make this script restart after 2 mins, indefinitely.
How?
I can't do it in the script itself since the process ends up being killed, and I can't figure out how to do it from the outside with another python script or bash (or shell for that matter). Any help would be appreciated.
Here is the code. The name is Rscript.py
import gi
import datetime, time
import sys
import signal
gi.require_version('Gst', '1.0')
from gi.repository import GObject, Gst, Gtk
GObject.threads_init()
Gst.init(None)
pipeline = Gst.Pipeline()
autoaudiosrc = Gst.ElementFactory.make("autoaudiosrc", "autoaudiosrc")
audioconvert = Gst.ElementFactory.make("audioconvert", "audioconvert")
vorbisenc = Gst.ElementFactory.make("vorbisenc", "vorbisenc")
oggmux = Gst.ElementFactory.make("oggmux", "oggmux")
filesink = Gst.ElementFactory.make("filesink", "filesink")
url = datetime.datetime.now()
filesink.set_property("location",url)
pipeline.add( autoaudiosrc)
pipeline.add( audioconvert)
pipeline.add( vorbisenc)
pipeline.add( oggmux)
pipeline.add( filesink)
autoaudiosrc.link( audioconvert)
audioconvert.link( vorbisenc)
vorbisenc.link( oggmux)
oggmux.link( filesink)
pipeline.set_state(Gst.State.PLAYING)
signal.alarm(15)
Gtk.main()
Use the following:
#!/bin/bash
while [[ 1 ]];do
myapp &
pid=$!
sleep 120
kill -9 $pid
done
Related
I want to create a program in Python that allows to edit text that moves and show on top of a video. I want to do that in a website overlay (WPEWebKit).
I have found some code to start a pipeline with wpe and gst-launch-1.0, problem is that when I run gst-launch-1.0 -v wpesrc location="https://gstreamer.freedesktop.org" ! queue ! glimagesink from https://gstreamer.freedesktop.org/documentation/wpe/wpesrc.html it says WARNING: erroneous pipeline: no element "wpesrc". I have checked and I have installed gstreamer1.0-plugins-bad.
The other challange is that I want to create it in a python application. Here's my code so far:
import threading
import time
import gi
gi.require_version("Gst", "1.0")
from gi.repository import Gst, GLib
Gst.init()
main_loop = GLib.MainLoop()
thread = threading.Thread(target=main_loop.run)
thread.start()
pipeline = Gst.parse_launch("videotestsrc ! decodebin ! videoconvert ! autovideosink")
pipeline.set_state(Gst.State.PLAYING)
try:
while True:
time.sleep(0.1)
except KeyboardInterrupt:
pass
pipeline.set_state(Gst.State.NULL)
main_loop.quit()
I will be probably rewrite this because I want it in a GTK application.
I have 2 pieces of code that works fine up until now. Code A (FileRecord) creates a pipeline and writes whatever it hears from mic to an .ogg file named file.ogg. Code B (FilePlayer) must start after the user stops manual (Ctrl + C) the execution of script A and plays in pulsesink (user's headset) whatever recorder recorded during execution(file.ogg).
My goal is to make the 2 pieces of code work simultaneously and playing file from code B while code A records.
Code A (FileRecord)
from time import sleep
import gi
gi.require_version("Gst","1.0")
from gi.repository import Gst
from gi.repository import GLib
import signal
signal.signal(signal.SIGTSTP, signal.SIG_IGN) #When Ctrl + Z is pressed file is not playable (this command ignores Ctrl + Z)
print("In order to stop press: Ctrl C.")
Gst.init()
main_loop = GLib.MainLoop()
main_loop_thread = Thread(target = main_loop.run)
main_loop_thread.start()
#buffer = gst_buffer_new ()
pipeline = Gst.parse_launch('autoaudiosrc ! audioconvert ! tee name="source" ! queue ! vorbisenc ! oggmux ! filesink location=file.ogg')
pipeline.set_state(Gst.State.PLAYING)
try:
while True:
sleep(0.1)
except KeyboardInterrupt:
pass
pipeline.set_state(Gst.State.NULL)
main_loop.quit()
main_loop_thread.join()
Code B (FilePlayer)
from time import sleep
import gi
gi.require_version("Gst","1.0")
from gi.repository import Gst
from gi.repository import GLib
import signal
Gst.init()
main_loop = GLib.MainLoop()
main_loop_thread = Thread(target = main_loop.run)
main_loop_thread.start()
pipeline = Gst.parse_launch('filesrc location=file.ogg ! decodebin ! pulsesink')
pipeline.set_state(Gst.State.PLAYING)
try:
while True:
sleep(0.1)
except KeyboardInterrupt:
pass
pipeline.set_state(Gst.State.NULL)
main_loop.quit()
main_loop_thread.join()
I don't know how to achieve sound stream. Please help me!
I have following code which use infinite loop to check if active window has changed.This program prints the name of current window when active window changes. There must be better way to do this. how can a keep my python code always listening to active window change without infinite loop?
import subprocess
import psutil
pid = subprocess.check_output(["xdotool", "getactivewindow", "getwindowpid"]).decode("utf-8").strip()
prevpid=pid
print(pid)
while(True):
pid = subprocess.check_output(["xdotool", "getactivewindow", "getwindowpid"]).decode("utf-8").strip()
if prevpid!=pid:
process=psutil.Process(int(pid))
processName=process.name()
print("Current Active Process is "+processName)
prevpid=pid
If you are on X, instead of polling, it is much better to use the Wnck.Screen's active_window_changed signal, running from a Gtk loop. In a simple example:
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Wnck', '3.0')
from gi.repository import Gtk, Wnck
def dosomething(screen, previous_window):
try:
print(
previous_window.get_name(), "||",
screen.get_active_window().get_name()
)
# or run any action
except AttributeError:
pass
wnck_scr = Wnck.Screen.get_default()
wnck_scr.force_update()
wnck_scr.connect("active-window-changed", dosomething)
Gtk.main()
I would like to measure execution time of a QProcess object.
Is there an internal attribute, method or object in PySide for execution time measurements?
The current approach is to measure it from the outside using time.time().
Example code:
from PySide import QtCore
import time
p = QtCore.QProcess()
start_time = time.time()
p.start('ping -n 5 127.0.0.1 >nul')
p.waitForFinished(-1)
end_time = time.time() - start_time
print(end_time)
One way you could do this is as follows. This uses the systems time command to get the time of execution.
from PySide import QtCore
import time
p = QtCore.QProcess()
p.start('time -p ping -n 5 127.0.0.1 >nul')
p.waitForFinished(-1)
stdOut = p.readAllStandardOutput()
print(stdOut)
#TODO you will have to regex the stdOut to get the values you want.
Here is another approach:
from PySide import QtCore
import time
timer = QtCore.QTime()
def handle_proc_stop(*vargs):
procTime = timer.elapsed()
print("Process took {} miliseconds".format(procTime))
p = QtCore.QProcess()
p.started.connect(timer.start)
p.finished.connect(handle_proc_stop)
p.start('ping -n 5 127.0.0.1 >nul')
p.waitForFinished(-1)
I want to record audio from mic and play it immediately from same pc's speakers using gstreamer. In other words; make a wire between input and output record a
few samples and play them back immediately. I can record audio to an ogg file with this code:
#!/usr/bin/env python
import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject, Gst, Gtk
GObject.threads_init()
Gst.init(None)
pipeline = Gst.Pipeline()
autoaudiosrc = Gst.ElementFactory.make("autoaudiosrc", "autoaudiosrc")
audioconvert = Gst.ElementFactory.make("audioconvert", "audioconvert")
vorbisenc = Gst.ElementFactory.make("vorbisenc", "vorbisenc")
oggmux = Gst.ElementFactory.make("oggmux", "oggmux")
filesink = Gst.ElementFactory.make("filesink", "filesink")
url = "1.ogg"
filesink.set_property("location",url)
pipeline.add( autoaudiosrc)
pipeline.add( audioconvert)
pipeline.add( vorbisenc)
pipeline.add( oggmux)
pipeline.add( filesink)
autoaudiosrc.link( audioconvert)
audioconvert.link( vorbisenc)
vorbisenc.link( oggmux)
oggmux.link( filesink)
pipeline.set_state(Gst.State.PLAYING)
Gtk.main()
But how can i play the audio while recording?
After audioconvert, you can add a tee and queue to have a new branch.
You can have something like that:
autoaudiosrc ! audioconvert ! tee name="source" ! queue ! vorbisenc ! oggmux ! filesink location=file.ogg source. ! queue ! audioconvert ! alsasink