本文主要是介绍libVLC 轨道信息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
libVLC是一个流行的开源多媒体播放器框架,它提供了丰富的 API 用于开发视频播放相关的应用程序。获取轨道信息,是指获取当前媒体文件的音频、视频或字幕等轨道信息。
常见的视频文件一般都有音频和视频、特殊的会带有字幕文件。
媒体轨道信息结构存放在libvlc_media_track_t结构体中,如下所示
typedef struct libvlc_media_track_t
{/* Codec fourcc */uint32_t i_codec;uint32_t i_original_fourcc;int i_id;libvlc_track_type_t i_type;/* Codec specific */int i_profile;int i_level;union {libvlc_audio_track_t *audio;libvlc_video_track_t *video;libvlc_subtitle_track_t *subtitle;};unsigned int i_bitrate;char *psz_language;char *psz_description;} libvlc_media_track_t;
轨道的类型如下所示:
typedef enum libvlc_track_type_t
{libvlc_track_unknown = -1,libvlc_track_audio = 0, //音频libvlc_track_video = 1, //视频libvlc_track_text = 2 //字幕
} libvlc_track_type_t;
联合体:分别存放音频、视频、字幕数据。
union {libvlc_audio_track_t *audio;libvlc_video_track_t *video;libvlc_subtitle_track_t *subtitle;};
音频数据如下:通道数量、采样率。
typedef struct libvlc_audio_track_t
{unsigned i_channels;unsigned i_rate;
} libvlc_audio_track_t;
视频数据:存放宽、高等一些数据。
typedef struct libvlc_video_track_t
{unsigned i_height;unsigned i_width;unsigned i_sar_num;unsigned i_sar_den;unsigned i_frame_rate_num;unsigned i_frame_rate_den;libvlc_video_orient_t i_orientation;libvlc_video_projection_t i_projection;libvlc_video_viewpoint_t pose; /**< Initial view point */
} libvlc_video_track_t;
字幕数据:字符串。
typedef struct libvlc_subtitle_track_t
{char *psz_encoding;
} libvlc_subtitle_track_t;
示例:获取轨道数据
libvlc_media_track_t **tracks;int track_count = libvlc_media_tracks_get(vlc_media,&tracks);for (unsigned i = 0; i < track_count; i++) {libvlc_media_track_t* track = tracks[i];// 显示轨道信息printf("Track #%u: %s\n", i, track->psz_description);// 这里可以获取到每一个轨道的信息,比如轨道类型 track->i_type// 可能是 libvlc_track_video, libvlc_track_audio 或者 libvlc_track_text (字幕)if (track->i_type == libvlc_track_video) {// 处理视频轨道信息qDebug("width = %d",track->video->i_width);qDebug("height = %d", track->video->i_height);qDebug("rate_num = %d", track->video->i_frame_rate_num);qDebug("rate_den = %d", track->video->i_frame_rate_den);}else if (track->i_type == libvlc_track_audio) {// 处理音频轨道信息qDebug("channels = %d", track->audio->i_channels);qDebug("rate = %d", track->audio->i_rate);}else if (track->i_type == libvlc_track_text) {// 处理字幕轨道信息}}
运行截图:
打开文件完整示例:
void showWidget::slotOpenFile()
{/*选择文件*/QString filename = QFileDialog::getOpenFileName(this, "选择打开的文件", "D:/", tr("*.*"));std::replace(filename.begin(), filename.end(), QChar('/'), QChar('\\'));vlc_base = libvlc_new(0, NULL);vlc_media = libvlc_media_new_path(vlc_base, filename.toUtf8().data());if (!vlc_media) {return;}// 创建libvlc实例和媒体播放器vlc_mediaPlayer = libvlc_media_player_new_from_media(vlc_media);if (!vlc_mediaPlayer) {return;}// 等待元数据加载完成libvlc_media_parse(vlc_media);// 获取各种元数据const char *title = libvlc_media_get_meta(vlc_media, libvlc_meta_Title);const char *artist = libvlc_media_get_meta(vlc_media, libvlc_meta_Artist);const char *album = libvlc_media_get_meta(vlc_media, libvlc_meta_Album);const char *url = libvlc_media_get_meta(vlc_media, libvlc_meta_URL);const char *date = libvlc_media_get_meta(vlc_media, libvlc_meta_Date);const char *lang = libvlc_media_get_meta(vlc_media, libvlc_meta_Language);int duration = libvlc_media_get_duration(vlc_media); // 获取时长(单位:毫秒)qDebug("Title: %s", title ? title : "N/A");qDebug("Artist: %s", artist ? artist : "N/A");qDebug("Album: %s", album ? album : "N/A");qDebug("Duration: %d ms", duration);qDebug("url: %s", url ? url : "N/A");qDebug("date: %s", date ? date : "N/A");qDebug("lang: %s", lang ? lang : "N/A");libvlc_media_track_t **tracks;int track_count = libvlc_media_tracks_get(vlc_media,&tracks);for (unsigned i = 0; i < track_count; i++) {libvlc_media_track_t* track = tracks[i];// 显示轨道信息printf("Track #%u: %s\n", i, track->psz_description);// 这里可以获取到每一个轨道的信息,比如轨道类型 track->i_type// 可能是 libvlc_track_video, libvlc_track_audio 或者 libvlc_track_text (字幕)if (track->i_type == libvlc_track_video) {// 处理视频轨道信息qDebug("width = %d",track->video->i_width);qDebug("height = %d", track->video->i_height);qDebug("rate_num = %d", track->video->i_frame_rate_num);qDebug("rate_den = %d", track->video->i_frame_rate_den);}else if (track->i_type == libvlc_track_audio) {// 处理音频轨道信息qDebug("channels = %d", track->audio->i_channels);qDebug("rate = %d", track->audio->i_rate);}else if (track->i_type == libvlc_track_text) {// 处理字幕轨道信息}}设置 logo 叠加//libvlc_video_set_logo_int(vlc_mediaPlayer, libvlc_logo_enable, 1);//libvlc_video_set_logo_string(vlc_mediaPlayer, libvlc_logo_file, "D:\\2.png");libvlc_video_set_logo_int(vlc_mediaPlayer, libvlc_logo_position, libvlc_position_top_left);//libvlc_video_set_logo_int(vlc_mediaPlayer, libvlc_logo_x, 100);//libvlc_video_set_logo_int(vlc_mediaPlayer, libvlc_logo_y, 0);//libvlc_video_set_logo_int(vlc_mediaPlayer, libvlc_logo_opacity, 255);//libvlc_video_set_logo_int(vlc_mediaPlayer, libvlc_logo_repeat, 1);创建并设置水印文本//libvlc_video_set_marquee_int(vlc_mediaPlayer, libvlc_marquee_Enable, 1);//libvlc_video_set_marquee_int(vlc_mediaPlayer, libvlc_marquee_Position, libvlc_position_center);//libvlc_video_set_marquee_int(vlc_mediaPlayer, libvlc_marquee_Refresh, 2000);//libvlc_video_set_marquee_int(vlc_mediaPlayer, libvlc_marquee_Size, 24);//libvlc_video_set_marquee_string(vlc_mediaPlayer, libvlc_marquee_Text, "这里是水印文本");libvlc_video_set_marquee_int(w->vlc_mediaPlayer, libvlc_marquee_Timeout, 5000);//libvlc_video_set_marquee_int(vlc_mediaPlayer, libvlc_marquee_Opacity, 255); // 不透明//获取事件管理器libvlc_event_manager_t *em = libvlc_media_player_event_manager(vlc_mediaPlayer);// 注册事件监听器libvlc_event_attach(em, libvlc_MediaPlayerTimeChanged, vlcEvents, this);libvlc_event_attach(em, libvlc_MediaPlayerEndReached, vlcEvents, this);libvlc_event_attach(em, libvlc_MediaPlayerStopped, vlcEvents, this);libvlc_event_attach(em, libvlc_MediaPlayerPlaying, vlcEvents, this);libvlc_event_attach(em, libvlc_MediaPlayerPaused, vlcEvents, this);libvlc_media_player_set_hwnd(vlc_mediaPlayer, (void *)ui.widgetShow->winId());QTimer::singleShot(1000, this, &showWidget::slotPlay);
}void showWidget::slotPlay()
{if (vlc_mediaPlayer)libvlc_media_player_play(vlc_mediaPlayer);
}
更多参考:
libVLC 事件机制-CSDN博客
libVLC windows开发环境搭建-CSDN博客
https://sunnnnnn666.blog.csdn.net/article/details/136854500
libVLC 元数据-CSDN博客
libVLC 添加图片和文本水印-CSDN博客
这篇关于libVLC 轨道信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!