C++利用VLC庫制作音視頻的示例代碼
VLC支持情況
操作系統(tǒng):Windows、WinCE、Linux、MacOSX、BEOS、BSD、Android
訪問形式:文件、DVD/VCD/CD、http、ftp、mms、TCP、UDP、RTP、IP組播、IPv6、rtsp
編碼格式:MPEG*、DIVX、WMV、MOV、3GP、FLV、H.263、H.264、FLAC
視頻字幕:DVD、DVB、Text、Vobsub
視頻輸出:DirectX、X11、XVideo、SDL、FrameBuffer、ASCII
控制界面:WxWidgets、QT4/5、Web、Telnet
缺點(diǎn):對(duì)Real Video支持不算好,需要額外的插件;不支持3GP的AMR音頻格式
VLC庫的基本使用
關(guān)鍵的庫函數(shù):
創(chuàng)建實(shí)例
libvlc_new()
加載媒體
libvlc_media_new_path()/libvlc_media_new_location()(file:///、http://、rtsp://、screen://)
創(chuàng)建播放器
libvlc_media_player_new_from_media()
設(shè)置播放窗口
libvlc_media_player_set_hwnd()
開始播放
libvlc_media_player_play()
獲取播放長(zhǎng)度---需要播放之后才能獲取
libvlc_media_player_get_length()
獲取播放媒體的寬/高
libvlc_video_get_width()/libvlc_video_get_height()
獲取播放音量
libvlc_audio_get_volume()
設(shè)置播放的音量
libvlc_audio_set_volume()
獲取播放位置
libvlc_media_player_get_position()
設(shè)置播放位置
libvlc_media_player_set_position()
暫停播放
libvlc_media_player_pause()
停止播放
libvlc_media_player_stop()
釋放播放器
libvlc_media_player_release()
釋放媒體
libvlc_media_release()
釋放實(shí)例
libvlc_release()
創(chuàng)建流程
實(shí)例-->媒體-->播放器-->播放(播放后各種操作)-->釋放播放器-->釋放媒體-->釋放實(shí)例
案例代碼
#include <iostream>
#include "vlc.h"
#include <conio.h>
//編碼轉(zhuǎn)換
#include <Windows.h>
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"; //忽略配置
libvlc_instance_t* vlc_ins = libvlc_new(argc, argv); //①創(chuàng)建實(shí)例
std::string path = Unicode2Utf8(L"file:///D:\\C++Project\\VideoPlay\\VideoPlay\\音樂.mp4");
libvlc_media_t* media = libvlc_media_new_location(vlc_ins, path.c_str());
//libvlc_media_t* media = libvlc_media_new_path(vlc_ins, "音樂.mp4"); //②創(chuàng)建媒體
//media = libvlc_media_new_location(vlc_ins, "file:///D:\\C++Project\\VideoPlay\\VideoPlay\\音樂.mp4");
libvlc_media_player_t* player = libvlc_media_player_new_from_media(media); //③創(chuàng)建播放器
do
{
int ret = libvlc_media_player_play(player); //④播放 返回值0開始 -1錯(cuò)誤
if (ret == -1) {
printf("error found!\r\n");
break;
}
int vol = -1;
while (vol == -1)
{
Sleep(10);
vol = libvlc_audio_get_volume(player); //獲取音量 默認(rèn)100
}
//只有media解析加載完成,才會(huì)有下面的參數(shù)
printf("volume is %d\r\n", vol);
libvlc_audio_set_volume(player, 10); //將音量設(shè)置到10
libvlc_time_t tm = libvlc_media_player_get_length(player); //獲取長(zhǎng)度(毫秒數(shù))
printf("%02d:%02d:%02d\r\n", int(tm / 1000 / 3600), int((tm / 1000 / 60) % 60), int(tm / 1000 % 60));
int width = libvlc_video_get_width(player); //獲取寬和高
int height = libvlc_video_get_height(player);
printf("width = %d height = %d\r\n", width, height);
while (!_kbhit()) { //檢驗(yàn)鍵盤是否按下 按下為1
printf("%f%%\r", 100.0 * libvlc_media_player_get_position(player)); //獲取播放進(jìn)度
Sleep(500);
}
getchar();
libvlc_media_player_pause(player); //暫停
getchar();
libvlc_media_player_play(player); //繼續(xù)
getchar();
libvlc_media_player_stop(player); //停止
} while (0);
libvlc_media_player_release(player); //⑤釋放播放器
libvlc_media_release(media); //⑥釋放媒體
libvlc_release(vlc_ins); //⑦釋放實(shí)例
return 0;
}到此這篇關(guān)于C++利用VLC庫制作音視頻的示例代碼的文章就介紹到這了,更多相關(guān)C++ VLC制作音視頻內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解如何用alpine鏡像做一個(gè)最小的鏡像并運(yùn)行c++程序
這篇文章主要介紹了詳解如何用alpine鏡像做一個(gè)最小的鏡像并運(yùn)行c++程序,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
利用Qt實(shí)現(xiàn)網(wǎng)絡(luò)數(shù)據(jù)報(bào)文大小端數(shù)據(jù)的收發(fā)
大小端(Endianness)是計(jì)算機(jī)體系結(jié)構(gòu)的一個(gè)術(shù)語,它描述了多字節(jié)數(shù)據(jù)在內(nèi)存中的存儲(chǔ)順序,下面我們來看看如何利用Qt實(shí)現(xiàn)網(wǎng)絡(luò)數(shù)據(jù)報(bào)文大小端數(shù)據(jù)的收發(fā)吧2024-11-11
C/C++?Qt?StringListModel?字符串列表映射組件詳解
StringListModel?字符串列表映射組件,該組件用于處理字符串與列表框組件中數(shù)據(jù)的轉(zhuǎn)換,通常該組件會(huì)配合ListView組件一起使用,本文給大家介紹了C/C++?Qt?StringListModel?字符串列表映射組件的相關(guān)知識(shí),感興趣的朋友跟隨小編一起看看吧2021-12-12
MFC控件之CListCtrl的應(yīng)用實(shí)例教程
這篇文章主要介紹了MFC控件中CListCtrl的應(yīng)用方法,包括了針對(duì)表格的一些操作,是MFC中比較重要的一個(gè)控件類,需要的朋友可以參考下2014-08-08
在Qt中遍歷QStringList子集并存儲(chǔ)的三種方法
本文介紹了在Qt中遍歷QStringList子集并存儲(chǔ)的三種方法:1)使用mid()函數(shù)提取連續(xù)范圍的元素;2)通過循環(huán)遍歷指定索引范圍;3)利用filter()函數(shù)按內(nèi)容篩選,每種方法適用于不同場(chǎng)景,需要的朋友可以參考下2026-01-01
C++中volatile限定符的實(shí)現(xiàn)示例
volatile關(guān)鍵字在C和C++中用于確保編譯器不優(yōu)化特定變量的訪問,主要用于多線程和硬件交互場(chǎng)景,本文就來介紹C++中volatile限定符的實(shí)現(xiàn)示例,感興趣的可以了解一下2024-11-11
C語言巧用二分查找實(shí)現(xiàn)猜數(shù)游戲
二分查找也稱折半查找(Binary?Search),它是一種效率較高的查找方法。但是,折半查找要求線性表必須采用順序存儲(chǔ)結(jié)構(gòu),而且表中元素按關(guān)鍵字有序排列,本篇文章教你用二分查找編寫猜數(shù)字游戲2022-02-02

