|
| 1 | +import sys |
| 2 | +import gi |
| 3 | +import logging |
| 4 | +import os |
| 5 | + |
| 6 | +gi.require_version("GLib", "2.0") |
| 7 | +gi.require_version("GObject", "2.0") |
| 8 | +gi.require_version("Gst", "1.0") |
| 9 | + |
| 10 | +from gi.repository import Gst, GLib, GObject |
| 11 | + |
| 12 | +logging.basicConfig(level=logging.DEBUG, |
| 13 | + format="[%(name)s] [%(levelname)8s] - %(message)s") |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | +if __name__ == "__main__": |
| 17 | +# Initialize GStreamer passing command line argument |
| 18 | + Gst.init(sys.argv) |
| 19 | + loop = GLib.MainLoop() |
| 20 | + |
| 21 | +if len(sys.argv) < 1: |
| 22 | + print("Missing <output-location> parameter") |
| 23 | + sys.exit(1) |
| 24 | + |
| 25 | +file_format = os.path.splitext(sys.argv[1])[1] |
| 26 | + |
| 27 | +# Create the empty pipeline |
| 28 | +pipeline = Gst.Pipeline.new("audio record") |
| 29 | +pulsesrc = Gst.ElementFactory.make("pulsesrc", "pulseaudio_src"); |
| 30 | +audioconvert = Gst.ElementFactory.make("audioconvert", "audio_convert"); |
| 31 | +capsfilter = Gst.ElementFactory.make("capsfilter", "caps_filter"); |
| 32 | +queue = Gst.ElementFactory.make("queue", "queue"); |
| 33 | +filesink = Gst.ElementFactory.make("filesink", "file_sink"); |
| 34 | + |
| 35 | + |
| 36 | +if not pipeline or not pulsesrc or not audioconvert or not capsfilter or not queue or not filesink: |
| 37 | + print("Create element failed") |
| 38 | + sys.exit(1) |
| 39 | + |
| 40 | + |
| 41 | +pulsesrc.set_property('num-buffers', 1000) |
| 42 | +pulsesrc.set_property('buffer-time', 30000) |
| 43 | + |
| 44 | +caps=Gst.Caps.new_empty_simple("audio/x-raw") |
| 45 | +capsfilter.set_property('caps', caps) |
| 46 | + |
| 47 | +filesink.set_property('location', sys.argv[1]) |
| 48 | + |
| 49 | +if file_format == ".wav": |
| 50 | + wavenc=Gst.ElementFactory.make("wavenc", "wav_enc"); |
| 51 | + if not wavenc: |
| 52 | + print("Create element failed.\n"); |
| 53 | + sys.exit(1) |
| 54 | + |
| 55 | + pipeline.add(pulsesrc) |
| 56 | + pipeline.add(capsfilter) |
| 57 | + pipeline.add(audioconvert) |
| 58 | + pipeline.add(wavenc) |
| 59 | + pipeline.add(queue) |
| 60 | + pipeline.add(filesink) |
| 61 | + |
| 62 | + if not pulsesrc.link(capsfilter): |
| 63 | + print("ERROR: Could not link pulsesrc to capsfilter") |
| 64 | + sys.exit(1) |
| 65 | + if not capsfilter.link(audioconvert): |
| 66 | + print("ERROR: Could not link capsfilter to audioconvert") |
| 67 | + sys.exit(1) |
| 68 | + if not audioconvert.link(wavenc): |
| 69 | + print("ERROR: Could not link audioconvert to wavenc") |
| 70 | + sys.exit(1) |
| 71 | + if not wavenc.link(queue): |
| 72 | + print("ERROR: Could not link wavenc to queue") |
| 73 | + sys.exit(1) |
| 74 | + if not queue.link(filesink): |
| 75 | + print("ERROR: Could not link queue to filesink") |
| 76 | + sys.exit(1) |
| 77 | + |
| 78 | +elif file_format == ".aac": |
| 79 | + avenc_aac=Gst.ElementFactory.make("avenc_aac", "aac_enc"); |
| 80 | + aacparse=Gst.ElementFactory.make("aacparse", "aac_parse"); |
| 81 | + aac_caps=Gst.ElementFactory.make("capsfilter", "aac_caps_filter"); |
| 82 | + |
| 83 | + if not avenc_aac or not aacparse or not aac_caps: |
| 84 | + print("Create element failed.\n"); |
| 85 | + sys.exit(1) |
| 86 | + |
| 87 | + caps=Gst.Caps.new_empty_simple('audio/mpeg') |
| 88 | + aac_caps.set_property('caps', caps) |
| 89 | + pipeline.add(pulsesrc) |
| 90 | + pipeline.add(capsfilter) |
| 91 | + pipeline.add(audioconvert) |
| 92 | + pipeline.add(avenc_aac) |
| 93 | + pipeline.add(aacparse) |
| 94 | + pipeline.add(aac_caps) |
| 95 | + pipeline.add(queue) |
| 96 | + pipeline.add(filesink) |
| 97 | + if not pulsesrc.link(capsfilter): |
| 98 | + print("ERROR: Could not link pulsesrc to capsfilter") |
| 99 | + sys.exit(1) |
| 100 | + |
| 101 | + if not capsfilter.link(audioconvert): |
| 102 | + print("ERROR: Could not link capsfilter to audioconvert") |
| 103 | + sys.exit(1) |
| 104 | + |
| 105 | + if not audioconvert.link(avenc_aac): |
| 106 | + print("ERROR: Could not link audioconvert to avenc_aac") |
| 107 | + sys.exit(1) |
| 108 | + |
| 109 | + if not avenc_aac.link(aacparse): |
| 110 | + print("ERROR: Could not link avenc_aac to aacparse") |
| 111 | + sys.exit(1) |
| 112 | + |
| 113 | + if not aacparse.link(aac_caps): |
| 114 | + print("ERROR: Could not link aacparse to aac_caps") |
| 115 | + sys.exit(1) |
| 116 | + |
| 117 | + if not aac_caps.link(queue): |
| 118 | + print("ERROR: Could not link aac_caps to queue") |
| 119 | + sys.exit(1) |
| 120 | + |
| 121 | + if not queue.link(filesink): |
| 122 | + print("ERROR: Could not link queue to filesink") |
| 123 | + sys.exit(1) |
| 124 | + |
| 125 | +elif file_format == ".mp3": |
| 126 | + lamemp3enc=Gst.ElementFactory.make("lamemp3enc", "mp3_enc"); |
| 127 | + |
| 128 | + if not lamemp3enc: |
| 129 | + print("Create element failed.\n"); |
| 130 | + sys.exit(1) |
| 131 | + |
| 132 | + pipeline.add(pulsesrc) |
| 133 | + pipeline.add(capsfilter) |
| 134 | + pipeline.add(audioconvert) |
| 135 | + pipeline.add(lamemp3enc) |
| 136 | + pipeline.add(queue) |
| 137 | + pipeline.add(filesink) |
| 138 | + |
| 139 | + if not pulsesrc.link(capsfilter): |
| 140 | + print("ERROR: Could not link pulsesrc to capsfilter") |
| 141 | + sys.exit(1) |
| 142 | + |
| 143 | + if not capsfilter.link(audioconvert): |
| 144 | + print("ERROR: Could not link capsfilter to audioconvert") |
| 145 | + sys.exit(1) |
| 146 | + if not audioconvert.link(lamemp3enc): |
| 147 | + print("ERROR: Could not link audioconvert to lamemp3enc") |
| 148 | + sys.exit(1) |
| 149 | + |
| 150 | + if not lamemp3enc.link(queue): |
| 151 | + print("ERROR: Could not link lamemp3enc to queue") |
| 152 | + sys.exit(1) |
| 153 | + if not queue.link(filesink): |
| 154 | + print("ERROR: Could not link queue to filesink") |
| 155 | + sys.exit(1) |
| 156 | + |
| 157 | +else: |
| 158 | + print("Format %s not supported\n", format); |
| 159 | + sys.exit(1) |
| 160 | + |
| 161 | + |
| 162 | +pipeline.set_state(Gst.State.PLAYING) |
| 163 | +print("Audio record started"); |
| 164 | +bus = pipeline.get_bus() |
| 165 | +bus_id = bus.add_signal_watch() |
| 166 | +msg = bus.timed_pop_filtered(Gst.CLOCK_TIME_NONE,Gst.MessageType.ERROR | Gst.MessageType.EOS) |
| 167 | +if msg: |
| 168 | + t = msg.type |
| 169 | + if t == Gst.MessageType.ERROR: |
| 170 | + err, dbg = msg.parse_error() |
| 171 | + print("ERROR:", msg.src.get_name(), ":", err.message) |
| 172 | + if dbg: |
| 173 | + print("debugging info:", dbg) |
| 174 | + elif t == Gst.MessageType.EOS: |
| 175 | + print("stopped recording") |
| 176 | + else: |
| 177 | + print("ERROR: Unexpected message received.") |
| 178 | + |
| 179 | +pipeline.set_state(Gst.State.NULL) |
| 180 | +Gst.Object.unref(bus) |
| 181 | +Gst.deinit() |
0 commit comments