當前位置:網站首頁>VLC音視頻庫

VLC音視頻庫

2022-01-27 13:24:50 潜行~

提示:文章寫完後,目錄可以自動生成,如何生成可參考右邊的幫助文檔


一、VLC是什麼?

VLC是Video Lan Client的縮寫,原先是幾個法國的大學生做的項目,後來他們把VLC作為了一個開源的項目,吸引了來自世界各國的很多優秀程序員來共同編寫和維護VLC,才逐漸變成了現在這個樣子

二、使用步驟

1.引入庫

集成VLC庫後在項目中引入頭文件

#include <Windows.h>
#include <iostream>
#include <conio.h>
#include "vlc.h"

2.使用流程

std::string Unicode2Utf8(const std::wstring& strIn) {
    
    std::string str;
    int  length = ::WideCharToMultiByte(CP_UTF8, 0, strIn.c_str(), strIn.size(), NULL, 0, NULL, NULL);
    str.resize(length + 1);
    ::WideCharToMultiByte(CP_UTF8, 0, strIn.c_str(), strIn.size(), (LPSTR)str.c_str(), length, NULL, NULL);
    return str;

};
int main()
{
    
    int argc = 1;
    char* argv[2];
    argv[0] = (char*) "--ignore-config";
    //1.創建實例
    libvlc_instance_t* vlc_ins = libvlc_new(argc, argv);
    //2.加載媒體
    //std::string path = Unicode2Utf8(L"OD.mp4");
    //libvlc_media_t* media = libvlc_media_new_path(vlc_ins, path.c_str());
    libvlc_media_t* media = libvlc_media_new_location(vlc_ins, Unicode2Utf8(L"file:///D:\\code\\vs-c++_worokspace\\gitLab\\VedioPlay\\VedioPlay\\OD.mp4").c_str());
    //3.創建播放器
    libvlc_media_player_t* player = libvlc_media_player_new_from_media(media);
    //4.設置播放窗口TODO
    do 
    {
    
        //5.開始播放
		int ret = libvlc_media_player_play(player);
		if (ret == -1)
		{
    
			printf("error found\r\n");
            break;
		}
        Sleep(300);//防止media視頻資源還沒解析完成
        //6.獲取播放長度
        libvlc_time_t time = libvlc_media_player_get_length(player);
        printf("%02d:%02d:%02d.%03d\r\n", int(time / 3600000), int((time / 60000)) % 60, int(time / 1000) % 60, int(time) % 1000);
        //7.獲取播放媒體的寬高
        int width =libvlc_video_get_width(player);
        int height = libvlc_video_get_height(player);
        printf("width = %d: height = %d\r\n", width, height);
		//8.獲取播放音量TODO
		int vol = libvlc_audio_get_volume(player);
		printf("音量:%d\r\n", vol);
		//8.5 設置播放音量
		libvlc_audio_set_volume(player, 10);
        //9.獲取播放進度
        while (!_kbhit())
        {
    
            printf("%f%%\r"/* "\r":每次光標移到本行的行首比特置處*/, 100.0 * libvlc_media_player_get_position(player));
            Sleep(500);
        }
        //10.設置播放比特置TODO
		//libvlc_media_player_set_position()
        getchar();
        //11.暫停播放
        libvlc_media_player_pause(player);
        getchar();
        //5.開始播放
        libvlc_media_player_play(player);
        //12.停止播放
        getchar();
        libvlc_media_player_stop(player); 
    } while (0);
    //13.資源釋放
    libvlc_media_player_release(player);
    libvlc_media_release(media);
    libvlc_release(vlc_ins);
    return 0; 
}

總結

音視頻的處理不是那麼穩定的,需要有容錯處理和多次的測試

版權聲明
本文為[潜行~]所創,轉載請帶上原文鏈接,感謝
https://cht.chowdera.com/2022/01/202201271324498774.html

隨機推薦