如何在视频上的SRT文件中添加字幕并在C程序中使用Gstreamer播放字幕 [英] How to add subtitles from a SRT file on a video and play it with Gstreamer in a c program

查看:505
本文介绍了如何在视频上的SRT文件中添加字幕并在C程序中使用Gstreamer播放字幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Gstreamer使用C程序播放视频,并从SRT文件中添加字幕.

I want to play a video with a C program using Gstreamer and adding subtitles from a SRT file.

我是gstreamer的新手,我以某种方式想出了让它在命令行中运行的人:

I'm new to gstreamer and I somehow figured out who to make it work in command-line :

gst-launch filesrc location=video.srt ! subparse ! \
    overlay. filesrc location=video.ogv ! oggdemux name=demux \
    demux. ! queue ! vorbisdec ! audioconvert ! autoaudiosink \
    demux. ! queue ! theoradec ! ffmpegcolorspace ! subtitleoverlay name=overlay ! autovideosink;

问题是我可以从C程序播放视频,但我不知道如何添加字幕.

The problem is that I can play the video from a C program but I didn't understood how to add the subtitles.

int main (int argc, char *argv[]) {
    GMainLoop *loop;

    GstElement *pipeline, *source, *demuxer, *audioDecoder, *videoDecoder, *audioConv, *videoConv, *videosink, 
            *audiosink, *audioQueue, *videoQueue;
    GstBus *bus;

    gst_init (&argc, &argv);

    loop = g_main_loop_new (NULL, FALSE);

    if (argc < 2 && argc > 3) {
        g_printerr ("Usage: %s <Ogg/Vorbis filename> [Srt filename]\n", argv[0]);
        return -1;
    }

    pipeline     = gst_pipeline_new ("audiovideo-player");
    source       = gst_element_factory_make ("filesrc",          "file-source");
    demuxer      = gst_element_factory_make ("oggdemux",         "ogg-demuxer");
    audioQueue   = gst_element_factory_make ("queue",            "audio-queue");
    videoQueue   = gst_element_factory_make ("queue",            "video-queue");
    audioDecoder = gst_element_factory_make ("vorbisdec",        "vorbis-decoder");
    videoDecoder = gst_element_factory_make ("theoradec",        "theora-decoder");
    audioConv    = gst_element_factory_make ("audioconvert",     "audio-converter");
    videoConv    = gst_element_factory_make ("ffmpegcolorspace", "video-converter");
    videosink    = gst_element_factory_make ("autovideosink",    "video-output");
    audiosink    = gst_element_factory_make ("autoaudiosink",    "audio-output");


    if (!pipeline || !source || !demuxer || !audioDecoder || !audioConv || !videoDecoder || !videoConv || !audioQueue 
            || !videoQueue || !audiosink || !videosink) {
        g_printerr ("One element could not be created. Exiting.\n");
        exit(-1);
    }

    g_object_set (G_OBJECT (source), "location", argv[1], NULL);

    bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
    gst_bus_add_watch (bus, bus_call, loop);
    gst_object_unref (bus);

    gst_bin_add_many (GST_BIN (pipeline),
                        source, demuxer, 
                        audioQueue, videoQueue, audioDecoder, videoDecoder,
                        videoConv, audioConv, videosink, audiosink, NULL);

    gst_element_link (source, demuxer);

    gst_element_link_many (videoQueue, videoDecoder, videoConv, videosink, NULL);
    g_signal_connect (demuxer, "pad-added", G_CALLBACK (on_pad_added), videoQueue);

    gst_element_link_many (audioQueue, audioDecoder, audioConv, audiosink, NULL);
    g_signal_connect (demuxer, "pad-added", G_CALLBACK (on_pad_added), audioQueue);

    g_print ("Lecture de : %s\n", argv[1]);
    gst_element_set_state (pipeline, GST_STATE_PLAYING);

    g_print ("En cours...\n");
    g_main_loop_run (loop);

    g_print ("Arret de la lecture\n");
    gst_element_set_state (pipeline, GST_STATE_NULL);
    g_print ("Suppression du pipeline\n");
    gst_object_unref (GST_OBJECT (pipeline));
    return 0;
} 

推荐答案

在添加了打击垫的回调中(您只需连接一次!),您必须检查新添加的打击垫的大小写.

In the pad-added callback (which you only need to connect to once!) you have to check the caps of the newly added pad.

无论如何,文件中的视频也需要连接到字幕叠加,并且您需要在字幕叠加后链接视频接收器.而且,您也可以将字幕文件的附加文件rc链接到字幕覆盖.

Anyway, the video from the file would also need to be connected to subtitleoverlay, and you would link the video sink after subtitleoverlay. And you would link an additional filesrc for the subtitle file to subtitleoverlay too.

看一下playbin/playink中的用于字幕处理的代码,尤其是用于动态处理这些内容的代码.另外请注意,playbin具有sub-uri属性,可让您选择应该覆盖在视频上的外部字幕文件.

Take a look at the code inside playbin / playsink for subtitle handling, and especially for handling these things dynamically. Also note that playbin has a sub-uri property which allows you to select an external subtitle file that should be overlayed over the video.

这篇关于如何在视频上的SRT文件中添加字幕并在C程序中使用Gstreamer播放字幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆