Gstreamer的音视频同步

一 概述

    Gstreamer的音频视频同步,概括起来是一个比较大的问题,因为在网上可以看到很多音视频同步的办法。这里我们只看最普通的一种。以音频时钟做为参考时钟(要求参考时钟上的时间是线性递增的);生成数据流时依据参考时钟上的时间给每个数据块都打上时间戳(一般包括开始时间和结束时间);在播放时,读取数据上的时间戳,同时参考当前参考时钟上的时间来安排播放(如果数据块上的时间大于参考时钟的时间,则不急于播放,直到参考时钟达到数据块的开始时间;如果数据块上的时间小于参考时钟的时间,则应"尽快"播放或者干脆"丢弃"该数据块,以使得播放赶上播放进度。)

    Gstreamer的因视频分离器如下图:

    demux element将音频,视频分离后,给各自的解码器进行解码播放。     

                     +-----------+
                     |   Audio   |
                  +--|           |
                 /   +-----------+
   +----------+ /
   |  demux   |/
   |          |\
   +----------+ \
                 \   +-----------+
                  +--|   Video   |
                     |           |
                     +-----------+

二  提供时钟

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的用 GStreamer 实现音视频同步的示例代码: ```python import gi gi.require_version('Gst', '1.0') from gi.repository import Gst, GObject GObject.threads_init() Gst.init(None) player = Gst.ElementFactory.make("playbin", "player") player.set_property("uri", "file:///path/to/media/file") audio_sink = Gst.ElementFactory.make("autoaudiosink", "audio_sink") video_sink = Gst.ElementFactory.make("autovideosink", "video_sink") player.set_property("audio-sink", audio_sink) player.set_property("video-sink", video_sink) bus = player.get_bus() def on_message(bus, message): t = message.type if t == Gst.MessageType.EOS: player.set_state(Gst.State.NULL) loop.quit() elif t == Gst.MessageType.ERROR: err, debug = message.parse_error() print ("Error: %s" % err, debug) player.set_state(Gst.State.NULL) loop.quit() elif t == Gst.MessageType.STATE_CHANGED: if message.src == player: old_state, new_state, pending_state = message.parse_state_changed() if new_state == Gst.State.PLAYING: # Get the current time when playing starts query = Gst.Query.new_seeking(Gst.Format.TIME) if player.query(query): _, start, _ = query.parse_seeking() global start_time start_time = start elif t == Gst.MessageType.QOS: # Get the running time of the pipeline query = Gst.Query.new_position(Gst.Format.TIME) if player.query(query): _, running_time = query.parse_position() running_time += start_time # Get the running time of the last buffer struct = message.get_structure() _, running_time_buffer, _ = struct.get("running-time") running_time_buffer += start_time # Calculate the difference between the two running times diff = running_time - running_time_buffer # If the difference is too big, seek to the correct position if abs(diff) > Gst.SECOND / 10: player.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT, running_time) bus.add_signal_watch() bus.connect("message", on_message) # Start playing player.set_state(Gst.State.PLAYING) # Start the main loop loop = GObject.MainLoop() loop.run() ``` 此示例中使用了 `autoaudiosink` 和 `autovideosink` 作为音视频的输出,你还可以将其替换为其他的 sink。在收到 QOS 消息时,获取管道的当前时间以及最后一个缓冲区的运行时间,并在两个运行时间之间计算差异,如果差异太大,则使用 `seek_simple()` 函数跳转到正确的位置,以保持音视频同步

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值