quit after MP3 conversion of previously recorded sound

Pablo Rodríguez oinos at web.de
Tue Apr 12 18:01:44 UTC 2022


Dear list,

I have a Python script to record voice to a WAVE file (minimal sample
below). I’m new to coding and my backgorund is in humanities.

I have the player pipeline that records the voice and the mp3_converter
pipeline that converts the WAVE file to MP3.

I set the state of the mp3_converter pipeline to playing, but I have no
idea about how to quit the script once conversion is done.

Is there no way that the pipeline has finished the task?

Excuse my very basic question, but this is all Greek to me.

Many thanks for your help,

Pablo


import sys
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("Gst", "1.0")
from gi.repository import Gtk, Gdk, GObject, Gst

Gst.init(sys.argv)

class record:
    def __init__(self):
        self.audiofilename = "recording.wav"
        self.mp3_audio = "recording.mp3"

        self.win = Gtk.Window()
        self.win.set_title("recording script")
        self.win.set_default_size(500, 500)
        self.win.set_position(Gtk.WindowPosition.CENTER_ALWAYS)
        self.vbox = Gtk.VBox()

        self.win.connect("destroy", Gtk.main_quit)
        self.win.show_all()

        self.win.connect('key-press-event', self.key_press_event)

        self.player = Gst.parse_launch("autoaudiosrc !
        audio/x-raw,format=F32LE,rate=48000,channels=1 ! wavenc !
        filesink location=\"" + self.audiofilename + "\"")

        bus = self.player.get_bus()

        bus.add_signal_watch()
        bus.connect('message', self.on_message)

        self.mp3_converter = Gst.parse_launch("filesrc location=\"" +
        self.audiofilename + "\" ! decodebin ! audioconvert !
        audioresample ! lamemp3enc mono=true target=bitrate bitrate=32
        ! id3v2mux ! filesink location=\"" + self.mp3_audio + "\"")

        mp3_bus = self.mp3_converter.get_bus()
        mp3_bus.connect('message', self.on_mp3_message)

    def on_message(self, bus, message):
        t = message.type
        if t == Gst.MessageType.EOS:
            self.player.set_state(Gst.State.NULL)
            # ~ Gtk.main_quit()
        elif t == Gst.MessageType.ERROR:
            err, debug = message.parse_error()
            print ("Error: %s" % err, debug)
            self.player.set_state(Gst.State.NULL)

    def on_mp3_message(self, mp3_bus, message):
        t = message.type
        if t == Gst.MessageType.EOS:
            self.mp3_converter.set_state(Gst.State.NULL)
            Gtk.main_quit()
        elif t == Gst.MessageType.ERROR:
            err, debug = message.parse_error()
            print ("Error: %s" % err, debug)
            self.mp3_converter.set_state(Gst.State.NULL)

    def key_press_event(self, widget, event):
        if event.keyval == Gdk.keyval_from_name("space"):
            if self.player.get_state(0)[1] == Gst.State.NULL:
                self.player.set_state(Gst.State.PLAYING)
                self.win.set_title("recording...")
            elif self.player.get_state(0)[1] == Gst.State.PLAYING:
                self.player.send_event(Gst.Event.new_eos())
                self.win.set_title("MP3 conversion...")
                self.mp3_converter.set_state(Gst.State.PLAYING)
                # ~ self.mp3_converter.send_event(Gst.Event.new_eos())

    def gtk_main_quit(self, widget, event):
        Gtk.main_quit()

    def main(self):
        Gtk.main()

rec = record()
rec.main()


More information about the gstreamer-devel mailing list