简体   繁体   English

使用gstreamer / Python剪切视频的一部分(gnonlin?)

[英]cut parts of a video using gstreamer/Python (gnonlin?)

I have a video file and I'd like to cut out some scenes (either identified by a time position or a frame). 我有一个视频文件,我想剪切出一些场景(通过时间位置或帧来标识)。 As far as I understand that should be possible with gnonlin but so far I wasn't able to find a sample how to that (ideally using Python). 据我了解,使用gnonlin应该可以实现,但是到目前为止,我还没有找到一个示例(理想情况下使用Python)。 I don't want to modify the video/audio parts if possible (but conversion to mp4/webm would be acceptable). 我不想尽可能修改视频/音频部分(但可以转换为mp4 / webm)。

Am I correct that gnonlin is the right component in the gstreamer universe to do that? 我是否正确认为gnonlin是gstreamer宇宙中执行此操作的正确组件? Also I'd be glad for some pointers/recipes how to approach the problem (gstreamer newbie). 我也很高兴有一些指示/方法来解决这个问题(gstreamer新手)。

Actually it turns out that "gnonlin" is too low-level and still requires a lot of gstreamer knowledge. 实际上,事实证明“ gnonlin”的级别太低 ,仍然需要大量gstreamer知识。 Luckily there is "gstreamer-editing-services" ( gst-editing-services ) which is a library offering a higher level API on top of gstreamer and gnonlin. 幸运的是,有一个“ gstreamer-editing-services”( gst-editing-services ),它是一个在gstreamer和gnonlin之上提供更高级别API的库。

With a tiny bit of RTFM reading and a helpful blog post with a Python example I was able to solve my basic problem: 通过少量的RTFM阅读和一个有用的Python示例博客文章,我就能解决我的基本问题:

  1. Load the asset (video) 加载资产(视频)
  2. Create a Timeline with a single layer 创建具有单个图层的时间轴
  3. add the asset multiple times to the layer, adjusting start, inpoint and duration so only the relevant parts of a video are present in the output video 多次将资产添加到图层,调整开始,入点和持续时间,以便仅视频的相关部分出现在输出视频中

Most of my code is directly taken from the referenced blog post above so I don't want to dump all of that here. 我的大部分代码都是直接从上面引用的博客文章中获取的,因此我不想将所有内容都转储到这里。 The relevant stuff is this: 相关的东西是这样的:

    asset = GES.UriClipAsset.request_sync(source_uri)
    timeline = GES.Timeline.new_audio_video()
    layer = timeline.append_layer()

    start_on_timeline = 0
    start_position_asset = 10 * 60 * Gst.SECOND
    duration = 5 * Gst.SECOND
    # GES.TrackType.UNKNOWN => add every kind of stream to the timeline
    clip = layer.add_asset(asset, start_on_timeline, start_position_asset,
        duration, GES.TrackType.UNKNOWN)

    start_on_timeline = duration
    start_position_asset = start_position_asset + 60 * Gst.SECOND
    duration = 20 * Gst.SECOND
    clip2 = layer.add_asset(asset, start_on_timeline, start_position_asset,
        duration, GES.TrackType.UNKNOWN)
    timeline.commit()

The resulting video includes the segments 10:00–10:05 and 11:05-11:25 so essentially there are two cuts: One in the beginning and one in the middle. 生成的视频包括10:00–10:05和11:05-11:25片段,因此基本上有两个剪辑:一个在开头,一个在中间。

From what I have seen this worked perfectly fine, audio and video in sync, no worries about key frames and whatnot. 从我所看到的效果来看,它可以很好地工作,音频和视频同步,不用担心关键帧和其他问题。 The only part left is to find out if I can translate the "frame number" into a timing reference for gst editing services. 剩下的唯一部分就是找出是否可以将“帧号”转换为GST编辑服务的时序参考。

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

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