gstreamer播放教程二:字幕

参考:https://blog.csdn.net/sakulafly/article/list/2

https://gstreamer.freedesktop.org/documentation/tutorials/playback/subtitle-management.html

如何获取字幕、音轨、视频流个数,及其具体信息

如何切换字幕,切换音轨,切换视频流

如何外挂一个字幕

如何设置字幕的字体

下面结合播放教程一,实现了以上功能。

#include "pch.h"

//如何选择字幕
//如何添加字幕
//如何定制字幕的字体

#include<string.h>
#include<stdio.h>
#include <gst/gst.h>

typedef struct _CustomData{
	GstElement *playbin;

	gint n_video;
	gint n_audio;
	gint n_text;

	gint current_video;
	gint current_audio;
	gint current_text;

	GMainLoop *main_loop;
}CustomData;

typedef enum {
	GST_PLAY_FLAG_VIDEO = (1 << 0), //we want video output
	GST_PALY_FLAG_AUDIO = (1 << 1),
	GST_PALY_FLAG_TEXT = (1 << 2)
}GstPlayFlags;

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;

	gst_init(&argc, &argv);

	data.playbin = gst_element_factory_make("playbin", "playbin");
	if (!data.playbin) {
		g_printerr("could not create playbin\n");
		return -1;
	}
	g_object_set(data.playbin, "uri", "file:///C:/Users/lenovo/Desktop/testVideo/[PGS][Tintin-004][DTS-AC3][5PGS].mkv", NULL);
	//外挂字幕地址
	g_object_set(data.playbin, "suburi", "file:///C:/Users/lenovo/Desktop/testVideo/shenhua_waigua.srt", NULL);
	//设置字幕的字体
	g_object_set(data.playbin, "subtitle-font-desc", "Sans,18", NULL);

	g_object_get(data.playbin, "flags", &flags, NULL);
	flags |= GST_PLAY_FLAG_VIDEO | GST_PALY_FLAG_AUDIO | GST_PALY_FLAG_TEXT;
	g_object_set(data.playbin, "flags", flags, NULL);

	//  我们逐个的设置这些属性,但我们也可以仅调用g_object_set()一次,来设置uri,flags,connect-speed
	
	bus = gst_element_get_bus(data.playbin);
	gst_bus_add_watch(bus, (GstBusFunc)handle_message, &data);

	//这几行连接了一个标准输入(键盘)和一个回调函数。这里使用的机制是GLib的,并非是基于GStreamer的.
#ifdef G_OS_WIN32
	io_stdin = g_io_channel_win32_new_fd(_fileno(stdin));
#else
	io_stdin = g_io_channel_unix_new(fileno(stdin));
#endif
	g_io_add_watch(io_stdin, G_IO_IN, (GIOFunc)handle_keyboard, &data);
	

	ret = gst_element_set_state(data.playbin, GST_STATE_PLAYING);
	if (ret == GST_STATE_CHANGE_FAILURE) {
		g_printerr("could not set sate to playing\n");
		gst_object_unref(data.playbin);
		return -1;
	}

	//为了交互,不再手动轮询gstreamer总线,我们创建main_loop,并且使用了g_main_loop_run函数让它运行起来。
	//,直到调用g_main_loop_quit()才被返回
	data.main_loop = g_main_loop_new(NULL,false);
	g_main_loop_run(data.main_loop);

	g_main_loop_unref(data.main_loop);
	g_io_channel_unref(io_stdin);
	gst_object_unref(bus);
	gst_element_set_state(data.playbin, GST_STATE_NULL);
	g_object_unref(data.playbin);
	return 0;
}

static gboolean handle_keyboard(GIOChannel *source, GIOCondition cond, CustomData *data) {
	gchar *str = NULL;
	if (g_io_channel_read_line(source, &str, NULL, NULL, NULL) == G_IO_STATUS_NORMAL){ 
		//获取用户要切换什么流
		int select = g_ascii_strtoull(str, NULL, 0);
		if (select == 0){ 
			g_print("input n_video:");
			gchar *strv = NULL;
			if (g_io_channel_read_line(source, &strv, NULL, NULL, NULL) == G_IO_STATUS_NORMAL) {
				//获取用户要切换都哪个流
				int index = g_ascii_strtoull(strv, NULL, 0);
				if (index < 0 || index >= data->n_video) {
					g_printerr("Index out of bounds\n");
				}
				else {
					//切换到输入编号对应的视频流
					g_print("Setting current video stream to %d\n", index);
					g_object_set(data->playbin, "current-video", index, NULL);
					g_print("0: selecet video; 1: selecet audio; 2: selecet subtitle.\n");
				}
				g_free(strv);
			}
		}
		else if (select == 1) {
			g_print("input n_audio:");
			gchar *stra = NULL;
			if (g_io_channel_read_line(source, &stra, NULL, NULL, NULL) == G_IO_STATUS_NORMAL) {
				int index = g_ascii_strtoull(stra, NULL, 0);
				if (index < 0 || index >= data->n_audio) {
					g_printerr("Index out of bounds\n");
				}
				else {
					g_print("Setting current audio stream to %d\n", index);
					g_object_set(data->playbin, "current-audio", index, NULL);
					g_print("0: selecet video; 1: selecet audio; 2: selecet subtitle.\n");

				}
				g_free(stra);
			}
		}
		else if (select == 2) {
			g_print("input n_subtitle:");
			gchar *strs = NULL;
			if (g_io_channel_read_line(source, &strs, NULL, NULL, NULL) == G_IO_STATUS_NORMAL) {
				int index = g_ascii_strtoull(strs, NULL, 0);
				if (index < 0 || index >= data->n_text) {
					g_printerr("Index out of bounds\n");
				}
				else {
					g_print("Setting current text stream to %d\n", index);
					g_object_set(data->playbin, "current-text", index, NULL);
					g_print("0: selecet video; 1: selecet audio; 2: selecet subtitle.\n");
				}
			}
			g_free(strs);
		}
		else {
			g_printerr(" inout error\n");
		}		
		
	}
	g_free(str);
	return TRUE;
}

static void analyze_streams(CustomData *data) {
	gint i;
	GstTagList *tags;
	gchar *str;
	guint rate;
	
	//获取音频流,视频流,字幕流的个数
	g_object_get(data->playbin, "n-video", &data->n_video, NULL);
	g_object_get(data->playbin, "n-audio", &data->n_audio, NULL);
	g_object_get(data->playbin, "n-text", &data->n_text, NULL);

	g_print("%d video streams(s) ,%d audio stream(s),%d text stream(s)\n", data->n_video, data->n_audio, data->n_text);

	g_print("\n");

	for (i = 0; i < data->n_video; i++) {
		tags = NULL;
		//遍历视频流,打印每一个视频流的信息
		g_signal_emit_by_name(data->playbin, "get-video-tags", i, &tags);
		if (tags) {
			g_print("video stream %d:\n", i);
			gst_tag_list_get_string(tags, GST_TAG_VIDEO_CODEC, &str);
			g_print("  codec: %s\n", str ? str : "unknown");
			g_free(str);
			gst_tag_list_free(tags);
		}
	}

	g_print("\n");
	for (i = 0; i < data->n_audio; i++) {
		tags = NULL;
		//遍历音频流,打印每一个音频流的信息
		g_signal_emit_by_name(data->playbin, "get-audio-tags", i, &tags);
		if (tags) {
			g_print("audio stream %d:\n", i);
			if (gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &str)) {
				g_print("  codec: %s\n", str);
				g_free(str);
			}
			if (gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &str)) {
				g_print("  language: %s\n", str);
				g_free(str);
			}
			if (gst_tag_list_get_uint(tags, GST_TAG_BITRATE, &rate)) {
				g_print("  bitrate: %d\n", rate);
			}
			gst_tag_list_free(tags);
		}
	}

	g_print("\n");
	for (i = 0; i < data->n_text; i++) {
		tags = NULL;
		//遍历字幕流,打印每一个字幕流的信息
		g_print("subtitle stream %d:\n", i);
		g_signal_emit_by_name(data->playbin, "get-text-tags", i, &tags);
		if (tags) {
			if (gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &str)) {
				g_print("  language: %s\n", str);
				g_free(str);
			}
			gst_tag_list_free(tags);
		}
		else {
			g_print("  no tags found\n");
		}

		//获取当前视频流,音频流,字幕流的编号,编号一般从0开始
		g_object_get(data->playbin, "current-video", &data->current_video, NULL);
		g_object_get(data->playbin, "current-audio", &data->current_audio, NULL);
		g_object_get(data->playbin, "current-text", &data->current_text, NULL);

		g_print("\n");
		g_print("Currently playing video stream %d, audio stream %d and subtitle stream %d\n",
			data->current_video, data->current_audio, data->current_text);
		//用户输入序号,选择要切换什么流
		g_print("0: selecet video; 1: selecet audio; 2: selecet subtitle.\n");
	}
}
static gboolean handle_message(GstBus *bus, GstMessage *msg, CustomData *data) {
	GError *err;
	gchar *debug_info;

	switch (GST_MESSAGE_TYPE(msg)) {
	case GST_MESSAGE_ERROR:
		gst_message_parse_error(msg, &err, &debug_info);
		g_printerr("Error received from element %s: %s\n", GST_OBJECT_NAME(msg->src), err->message);
		g_printerr("Debugging information: %s\n", debug_info ? debug_info : "none");
		g_clear_error(&err);
		g_free(debug_info);
		g_main_loop_quit(data->main_loop);
		break;
	case GST_MESSAGE_EOS:
		g_print("End-Of-Stream reached.\n");
		g_main_loop_quit(data->main_loop);
		break;
	case GST_MESSAGE_STATE_CHANGED: {
		GstState old_state, new_state, pending_state;
		gst_message_parse_state_changed(msg, &old_state, &new_state, &pending_state);
		if (GST_MESSAGE_SRC(msg) == GST_OBJECT(data->playbin)) {
			if (new_state == GST_STATE_PLAYING) {
				//当状态改变为playing的时候就分析流信息,也就是打开视频开始播放的时候
				analyze_streams(data);
			}
		}
	} break;
	}

	/* We want to keep receiving messages */
	return TRUE;
}

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值