c++ uses libvlc to implement a simple player

VLC Media Player is a very powerful player that basically supports most codecs. However, it is very difficult to compile the source code, so it is better to use the compiled library.

vlc sdk download address: http://download.videolan.org/pub/videolan/vlc/

vlc related

  1. VLC-QT :
    Friends who use QT can use the open source VLC-QT library, which is supported by both widget and qml. Since VLC-Qt integrates the entire libVLC, it has all the features of libVLC, such as: libVLC instance and player, single File and list playback, audio and video control, metadata management, if you want to quickly create a full-featured cross-platform media player, this library is very suitable! The detailed calling method will not be explained here, just go to Baidu.
    VLC-QT library official website address: https://vlc-qt.tano.si/
    VLC_QT library github address: https://github.com/vlc-qt
    VLC_QT library usage example address: https://github.com/vlc -qt/examples
  2. vlmc :
    vlmc (VideoLAN Movie Creator) is a non-linear video editing software based on libVLC that can run on Windows, Linux and Mac OS X. However, vlmc is still under development and is not yet ready for beta testing or release, so I feel Interested friends can download the source code and tinker with it, which is very interesting.
    Official website address: https://www.videolan.org/vlmc/
    GitHub address: https://code.videolan.org/videolan/vlmc.git

Okay, let’s get to the point

Problems encountered during the use of libvlc:

1.libvlc_new(0,NULL); always fails
. Solution: Copy the plugins folder in the downloaded SDK directory to the exe directory.

2.libvlc_media_t *vlc_mdia= libvlc_media_new_path(inst,“D:/box.mp4”); fails and returns empty.
Solution: libvlc_media_new_path(inst,“D:\box.mp4”);

3. "libvlc_media_read_cb": undeclared identifier
Solution: Add typedef __int64 ssize_t; in the vlc.h file or other files;

4.libvlc_media_get_duration/libvlc_media_player_get_length/libvlc_media_player_get_time fails and returns -1
Reason: Metadata is not parsed.
Workaround: ibvlc_media_t The video must be parsed or played at least once before the metadata can be obtained. That is, call libvlc_media_parse_with_options to parse local media asynchronously (the synchronous parsing function libvlc_media_parse has been deprecated), or call libvlc_media_player_play to play it once. The libvlc_media_parse_with_options calling method will be discussed below.

Parse media data + vlc event subscription

  1. Create event manager
libvlc_event_manager_t* vlc_media_event_mng = libvlc_media_event_manager(vlc_media);
  1. Subscribe to specified events (subscribe here to media analysis and video total duration events)
libvlc_event_attach(vlc_media_event_mng, libvlc_MediaParsedChanged, event_callback, nullptr);libvlc_event_attach(vlc_media_event_mng, libvlc_MediaDurationChanged, event_callback, nullptr);

3. Start parsing local media asynchronously

libvlc_media_parse_with_options(vlc_media, libvlc_media_parse_local,-1);

event_callback :
Description: event callback function
parameter event: event type
parameter userData: custom data type, can be Null

void event_callback(const libvlc_event_t *event, void *userData)
{switch (event->type){case libvlc_MediaParsedChanged:{//在该事件中获取帧率fpslibvlc_media_track_t **ppTracks = nullptr;int uiStreamCount = libvlc_media_tracks_get(g_vlcModel->m_media, &ppTracks);for (unsigned i = 0; i < uiStreamCount; i++){libvlc_media_track_t* pTrack = ppTracks[i];if (pTrack && libvlc_track_video == pTrack->i_type){libvlc_video_track_t* pVideoTrack = pTrack->video;int fps = pVideoTrack->i_frame_rate_num / (float)pVideoTrack->i_frame_rate_den;break;}}if(ppTracks)libvlc_media_tracks_release(ppTracks, uiStreamCount);break;}case libvlc_MediaDurationChanged:{//获取总时长int64_t duration = event->u.media_duration_changed.new_duration;break;}case libvlc_MediaPlayerPositionChanged:{//获取当前播放进度float pos = event->u.media_player_position_changed.new_position;break;}}	
}libvlc_instance_t *vlc_inst= nullptr;
libvlc_media_t *vlc_media = nullptr;
libvlc_media_player_t *vlc_player = nullptr;
libvlc_event_manager_t *vlc_media_event_mng = nullptr;
libvlc_event_manager_t *vlc_player_event_mng = nullptr;vlc_inst = libvlc_new(0, NULL);
if (vlc_inst)
{vlc_media = libvlc_media_new_path(vlc_inst ,"D:\\box.mp4");if (vlc_media){//创建事件管理器vlc__media_event_mng = libvlc_media_event_manager(vlc_media);//订阅指定事件(视频解析,视频总时长,当前播放进度)libvlc_event_attach(vlc__media_event_mng, libvlc_MediaParsedChanged, event_callback, nullptr);libvlc_event_attach(vlc__media_event_mng, libvlc_MediaDurationChanged, event_callback, nullptr);libvlc_event_attach(vlc__media_event_mng, libvlc_MediaPlayerPositionChanged, event_callback, nullptr);//异步解析本地媒体libvlc_media_parse_with_options(vlc_media, libvlc_media_parse_local,-1);vlc_player = libvlc_media_player_new_from_media(vlc_media);if (vlc_player){vlc__player_event_mng = libvlc_media_player_event_manager(vlc__player);libvlc_event_attach(vlc__player_event_mng, libvlc_MediaPlayerPositionChanged, event_callback, nullptr);auto imgBuffer = malloc(480 * 480* 4);libvlc_video_set_callbacks(vlc_player, [imgBuffer](void** pBuffer) -> void* {std::cout << "Lock" << std::endl;*pBuffer = imgBuffer;return NULL;}, [](void*, void*const*) {std::cout << "unlock" << std::endl;}, nullptr, param);libvlc_video_set_format(vlc_player, "RGBA", 450, 450, 450* 4);}}
}

As a benefit for this article, you can receive a Qt development learning package and technical videos for free , including (Qt practical projects, C++ language basics, C++ design patterns, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project practice, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click at the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/m0_73443478/article/details/133272639