要在工控机里实现播放rtsp海康摄像头视频,同时在屏幕上显示串口过来的信息,并且要保存。试验了很多方案,打算采用libvlc二次开发,mfc界面。这种方案工控机cpu占用率只有30%,opencv的方案有80%。现在如何在libvlc上显示动态实时字幕,请各位大拿发表高见。
要在使用libvlc播放海康摄像头视频的同时,在屏幕上显示串口信息是可行的。为了在libvlc中实现动态字幕,可以使用libvlc的API来实现。
以下是一些基本步骤:
使用libvlc_media_player_new()函数创建一个媒体播放器对象。
使用libvlc_media_new_location()或libvlc_media_new_path()函数创建一个媒体对象。
将媒体对象添加到媒体播放器中,使用libvlc_media_player_set_media()函数。
使用libvlc_video_set_subtitle_file()函数将字幕文件添加到视频流中。
使用libvlc_video_set_marquee_int()函数设置字幕的位置和属性。
使用libvlc_video_set_logo_string()函数在视频流上添加标志。
下面是一些示例代码:
// 创建媒体播放器对象
libvlc_instance_t* vlcInstance = libvlc_new(0, NULL);
libvlc_media_player_t* vlcMediaPlayer = libvlc_media_player_new(vlcInstance);
// 创建媒体对象
libvlc_media_t* vlcMedia = libvlc_media_new_location(vlcInstance, "");
// 设置媒体对象
libvlc_media_player_set_media(vlcMediaPlayer, vlcMedia);
// 添加字幕
libvlc_video_set_subtitle_file(vlcMediaPlayer, "");
// 设置字幕属性
libvlc_video_set_marquee_int(vlcMediaPlayer, libvlc_marquee_Enable, 1);
libvlc_video_set_marquee_int(vlcMediaPlayer, libvlc_marquee_Location, libvlc_marquee_Location_Bottom);
libvlc_video_set_marquee_int(vlcMediaPlayer, libvlc_marquee_Size, 20);
libvlc_video_set_marquee_string(vlcMediaPlayer, libvlc_marquee_Text, "");
// 播放视频
libvlc_media_player_play(vlcMediaPlayer);
注意,以上代码仅供参考。您需要根据自己的需求来实现动态字幕的逻辑。
要在libvlc上显示动态实时字幕,可使用libvlc的subpicture功能。该功能是通过创建字幕通道,将字幕渲染至视频上的指定位置,并且可以通过修改字幕通道上的相应文本和时间戳,实现动态字幕的添加和修改。以下是使用libvlc实现动态实时字幕的一般步骤:
libvlc_instance_t* vlc_inst = libvlc_new(0, NULL);
libvlc_media_player_t* player_inst = libvlc_media_player_new(vlc_inst);
libvlc_media_player_set_hwnd(player_inst, hwnd);
libvlc_media_player_set_fullscreen(player_inst, 0);
libvlc_media_player_set_rate(player_inst, 1.0f);
libvlc_media_player_set_video_title_display(player_inst, libvlc_position_disable, 0);
libvlc_event_manager_t* event_mgr = libvlc_media_player_event_manager(player_inst);
libvlc_event_attach(event_mgr, libvlc_MediaPlayerEndReached, on_player_end_callback, this);
libvlc_video_set_mouse_input(player_inst, 1);
其中,hwnd为MFC界面所在的窗口句柄,on_player_end_callback为播放结束后的回调函数。
libvlc_media_t* media_inst = libvlc_media_new_location(vlc_inst, url);
libvlc_media_add_option(media_inst, "network-caching=0");
libvlc_media_add_option(media_inst, "no-video-title-show");
libvlc_media_add_option(media_inst, "no-sub-autodetect-file");
libvlc_media_add_option(media_inst, "sub-file=/path/to/subtitle.srt");
libvlc_media_player_set_media(player_inst, media_inst);
libvlc_media_release(media_inst);
其中,url为rtsp视频的地址,"network-caching=0"为实时播放所需的缓存时间,"no-video-title-show"为禁止在播放窗口中显示视频标题,"no-sub-autodetect-file"和"sub-file=/path/to/subtitle.srt"为禁止自动检测字幕文件,采用指定的字幕文件。
libvlc_media_player_set_subtitle_file(player_inst, "/path/to/subtitle.srt");
libvlc_media_player_set_subtitle_track(player_inst, 0);
libvlc_track_description_t* tracks = NULL;
libvlc_video_get_spu_description(player_inst, &tracks);
libvlc_track_description_release(tracks);
libvlc_video_set_subtitle_file(player_inst, "/path/to/subtitle.srt");
libvlc_video_set_subtitle_track(player_inst, 0);
libvlc_video_set_subtitle_text_renderer(player_inst, on_subtitle_render_callback, this);
其中,"/path/to/subtitle.srt"为字幕文件路径,on_subtitle_render_callback为字幕渲染回调函数,tracks为字幕通道描述信息。
在on_subtitle_render_callback回调函数中,可以编写代码实现对字幕文本和时间戳的修改,从而实现动态字幕的添加和修改。例如:
static void on_subtitle_render_callback(void* opaque, void* data, unsigned int width, unsigned int height, unsigned int pitch)
{
//获取当前时间戳和要显示的字幕文本
uint64_t pts = ...;
std::string text = ...;
//创建字幕图像和面板
libvlc_video_subtitle_t* sub = libvlc_video_subtitle_new();
sub->i_original_frame_width = width;
sub->i_original_frame_height = height;
sub->i_frame_stride = pitch;
sub->i_background_color = 0x00FFFFFF;
sub->i_x = 100;
sub->i_y = 100;
sub->time = pts;
sub->i_duration = 5000000;
sub->p_picture = libvlc_picture_new(width, height);
//将文本渲染至图像上
FT_Library ft_lib;
FT_Face ft_face;
FT_Init_FreeType(&ft_lib);
FT_New_Face(ft_lib, "arial.ttf", 0, &ft_face);
FT_Set_Char_Size(ft_face, 64, 0, 0, 0);
FT_Set_Pixel_Sizes(ft_face, 0, 64);
cairo
这里的实时字幕应该指的是界面的一些显示信息吧?
有没有例如像DrawText函数的方法,直接给我们调用,产生字幕文件太烦了。
你直接在窗口上绘制文字就行了
可以借鉴下
vlc是一个开源的视频播放器,并提供了库供二次开发,其视频解码库是ffmpeg,网络库是live555。
可以试试加滤镜的方式,比如Marquee 滤镜、Marquee 滤镜等,来实现在libvlc上显示动态实时字幕
据我了解,要在libvlc上显示动态实时字幕,你可以使用libvlc提供的API来实现,具体步骤如下:
1.使用libvlc_media_player_new创建一个新的媒体播放器。
2.使用libvlc_video_set_subtitle_file设置字幕文件。
3.使用libvlc_video_set_subtitle_text_renderer设置字幕渲染器。
4.使用libvlc_media_player_play开始播放视频。