GStreamer官方教程系列
Playback tutorial 2: Subtitle management
目标
这个教程与之前的非常相似,但这次在不同字幕流之间切换,而不是音频或视频流。本教程学习重点:
- 如何选择字幕流
- 如何添加外部字幕流
- 如何自定义字幕字体
介绍
从之前的教程中我们已经知道容器文件可以存储多音频及视频流,并且我们可以通过改变current-audio及current-video playbin的属性轻松地在这些流之间切换。字幕流的切换也是一样的简单。
需要注意到,就像音频与视频一样,playbin负责为字幕选择合适的解码器,而且GStreamer的插件结构使得添加新的格式的支持就像复制文件一样容易。对应用开发者而言一切都是不可见的。
除了嵌入在容器中的字幕,playbin也支持通过外部URI添加额外的字幕流。
本教程将打开一个已经有5个字幕流的文件,并且会从其他文件添加一个另外的字幕流(希腊语)。
多语言播放器与字幕
playback-tutorial-2.c代码见下。
#include <stdio.h>
#include <gst/gst.h>
/* Structure to contain all our information, so we can pass it around */
typedef struct _CustomData {
GstElement *playbin; /* Our one and only element */
gint n_video; /* Number of embedded video streams */
gint n_audio; /* Number of embedded audio streams */
gint n_text; /* Number of embedded subtitle streams */
gint current_video; /* Currently playing video stream */
gint current_audio; /* Currently playing audio stream */
gint current_text; /* Currently playing subtitle stream */
GMainLoop *main_loop; /* GLib's Main Loop */
} CustomData;
/* playbin flags */
typedef enum {
GST_PLAY_FLAG_VIDEO = (1 << 0), /* We want video output */
GST_PLAY_FLAG_AUDIO = (1 << 1), /* We want audio output */
GST_PLAY_FLAG_TEXT = (1 << 2) /* We want subtitle output */
} GstPlayFlags;
/* Forward definition for the message and keyboard processing functions */
static gboolean handle_message (GstBus *bus, GstMessage *msg, CustomData *data);
static gboolean handle_keyboard (GIOChannel *source, GIOCondition cond, CustomData *data);
int main(int argc, char *argv[]) {
CustomData data;
GstBus *bus;
GstStateChangeReturn ret;
gint flags;
GIOChannel *io_stdin;
/* Initialize GStreamer */
gst_init (&argc, &argv);
/* Create the elements */
data.playbin = gst_element_factory_make ("playbin", "playbin");
if (!data.playbin) {
g_printerr ("Not all elements could be created.\n");
return -1;
}
/* Set the URI to play */
g_object_set (data.playbin, "uri", "https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.ogv", NULL);
/* Set the subtitle URI to play and some font description */
g_object_set (data.playbin, "suburi", "https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer_gr.srt", NULL);
g_object_set (data.playbin, "subtitle-font-desc", "Sans, 18", NULL);
/* Set flags to show Audio, Video and Subtitles */