简体   繁体   English

文件上的Gstreamer音频捕获

[英]Gstreamer audio capture on file

i'm new to Gstreamer and basically have been reading the documentation for a day now. 我是Gstreamer的新手,现在基本上已经阅读了一天的文档。 I need a program that captures audio from an audio input for 10-15 sec every 5 mins and stores it into a file. 我需要一个程序,每5分钟从音频输入中捕获音频10-15秒,并将其存储到文件中。 The only thing i have no idea how to do is the capture itself, since i haven't worked with inputs before. 我唯一不知道该怎么做的就是捕获本身,因为我之前从未使用过输入。 Now i found this code on the net but have no idea if it will help me (i understand most of it but have no idea where does it save the files, if it saves them at all) any help would be really appreciated 现在,我在网上找到了这段代码,但不知道它是否对我有帮助(我了解其中的大部分内容,但不知道它在哪里保存了文件,如果可以将它们保存的话),对您的帮助将不胜感激

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

PS a professor suggested gstreamer to me, and i haven't found a good alternative, so that's why i'm trying it, but if there is a better way out there please tell me, since i feel like gstreamer is more in a player/ playback nature PS一位教授向我建议了gstreamer,但我还没有找到一个好的选择,所以这就是我正在尝试的原因,但是如果有更好的解决方法,请告诉我,因为我觉得gstreamer在玩家中更多/播放性质

the question is nicely written, however you could try out more investigation before you ask.. also you can add tag python and the code would be highlighted.. 这个问题写得很好,但是您可以在问之前尝试更多的调查..也可以添加标签python,代码将突出显示。

Anyway gstreamer is perfectly ok for saving to file.. 无论如何,gstreamer完全可以保存到文件。

The example saves the output into file called 1.ogg in current directory where you run the script. 该示例将输出保存到运行脚本的当前目录中的名为1.ogg文件中。 Have you already run the script? 您已经运行脚本了吗? look for that file where you ran it.. 在运行文件的位置查找该文件。

The filesink element does the actual file saving. filesink元素会进行实际的文件保存。 If you want to save each capture in another file you could generate some filename in format for example YYYY-MM-DD_HH_mm_ss.ogg(this is typical datetime format where YYYY represents year, MM month etc). 如果要将每个捕获保存到另一个文件中,则可以生成一些文件名,格式为YYYY-MM-DD_HH_mm_ss.ogg(这是典型的日期时间格式,其中YYYY代表年,MM月等)。

You can either 你可以

  • run the script every 5 minues if you are on linux thic could be a cron job 如果您使用的是Linux,则每5分钟运行一次脚本,这可能是一项Cron工作
  • or just a bash script.. 或只是一个bash脚本。
  • or you can code it in python in same script to run the gstreamer pipe again and again.. this would be harder but 'nicer' 或者您也可以在同一脚本中的python中对其进行编码,以一次又一次地运行gstreamer管道。
import gi
import datetime, time
import sys
import signal  
signal.alarm(15)
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", "audioconv")
audioresample= Gst.ElementFactory.make("audioresample","audioresample")
vorbisenc = Gst.ElementFactory.make("vorbisenc", "vorbisenc")
oggmux = Gst.ElementFactory.make("oggmux", "oggmux")
filesink = Gst.ElementFactory.make("filesink", "filesink")
url = datetime.datetime.now()

audioresample.set_property("quality", 10) 
vorbisenc.set_property("quality", 1)

filesink.set_property("location",url)
pipeline.add( autoaudiosrc)
pipeline.add( audioconvert)
pipeline.add( vorbisenc)
pipeline.add( oggmux)
pipeline.add( filesink)
pipeline.add( audioresample)

autoaudiosrc.link( audioconvert)
audioconvert.link( audioresample)
audioresample.link(vorbisenc)
vorbisenc.link( oggmux)
oggmux.link( filesink)

pipeline.set_state(Gst.State.PLAYING)

Gtk.main()

As promised, the full code. 如所承诺的那样,完整的代码。 The best audio quality you can have, plus it saves the files as a date and dime now. 您可以拥有最佳的音频质量,再加上现在可以将文件另存为日期和一角钱。 As for the storing, i made a bash script that works perfectly, so that solved that (found out that if i do it in the script itself, the file never saves properly) 至于存储,我制作了一个bash脚本,它可以完美地工作,因此解决了这一问题(发现如果我在脚本本身中进行操作,该文件将永远无法正确保存)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM