網(wǎng)站首頁 編程語言 正文
前言
使用ffplay播放視頻,有時我們只能獲取到byte數(shù)據(jù),比如Windows的嵌入資源只能拿到在內(nèi)存中的視頻文件數(shù)據(jù),或者是自定義協(xié)議網(wǎng)絡(luò)傳輸?shù)囊曨l,這個時候我們就需要實現(xiàn)一個流數(shù)據(jù)輸入接口來進行播放了,ffmpeg的AVIOContext就支持這一功能,我們只需要對ffplay進行簡單的拓展即可。
一、如何使用AVIOContext
avio是ffmpeg自定義輸入流的對象,它是AVformatContext的一個字段,我只需要創(chuàng)建avio對象并實現(xiàn)其回調(diào)方法,然后給AVformatContext.pb賦值即可。
1、定義回調(diào)方法
以文件流為例(省略了打開文件和獲取文件長度的操作)
FILE* file;
static int avio_read(ACPlay play, uint8_t* buf, int bufsize)
{
return fread(buf, 1, bufsize, file);
}
static int64_t avio_seek(ACPlay play, int64_t offset, int whence)
{
switch (whence)
{
case AVSEEK_SIZE:
return fileSize;
break;
case SEEK_CUR:
fseek(file, offset, whence);
break;
case SEEK_SET:
fseek(file, offset, whence);
break;
case SEEK_END:
fseek(file, offset, whence);
break;
default:
break;
}
return ftell(test3file);
}
2、關(guān)聯(lián)AVFormatContext
AVFormatContext* ic = NULL;
AVIOContext* avio = avio_alloc_context((unsigned char*)av_malloc(1024 * 1024), 1024 * 1024, 0, s, avio_read, NULL, avio_seek);
if (avio)
{
ic->pb = avio;
ic->flags = AVFMT_FLAG_CUSTOM_IO;
}
avformat_open_input(&ic, "", NULL, NULL);
3、銷毀資源
if (ic->avio)
{
if (ic->avio->buffer)
{
av_free(is->avio->buffer);
}
avio_context_free(&is->avio);
ic->avio = NULL;
}
二、ffplay中使用AVIOContext
1、添加字段
在VideoState中添加如下字段
AVIOContext* avio;
2、定義接口
/// <summary>
/// 開始播放
/// </summary>
/// <param name="play">播放器對象</param>
/// <param name="read">自定義輸入流,讀取數(shù)據(jù)時的回調(diào)</param>
/// <param name="seek">自定義輸入流,定位時的回調(diào)</param>
void ac_play_startViaCustomStream(ACPlay play, ACPlayCustomPacketReadCallback read, ACPlayCustomPacketStreamSeekCallback seek);
{
VideoState* s = (VideoState*)play;
if(read)
s->avio = avio_alloc_context((unsigned char*)av_malloc(1024 * 1024), 1024 * 1024, 0, s, read, NULL, seek);
stream_open(s, "", NULL);
}
3、關(guān)聯(lián)AVFormatContext
在read_thread中avformat_open_input的上一行添加如下代碼:
if (is->avio)
{
ic->pb = is->avio;
ic->flags = AVFMT_FLAG_CUSTOM_IO;
}
4、銷毀資源
在stream_close中添加如下代碼
if (ic->avio)
{
if (ic->avio->buffer)
{
av_free(is->avio->buffer);
}
avio_context_free(&is->avio);
ic->avio = NULL;
}
總結(jié)
以上就是今天要講的內(nèi)容,之所以去實現(xiàn)這樣的功能是因為筆者曾經(jīng)工作中,遇到過相關(guān)使用場景,在程序啟動時播放mp4嵌入資源,將其讀取出來保存文件在播放顯然不是很好的方案,而且ffmpeg本身支持自定義輸入流,所以很容易就將此功能添加到ffplay上了。總的來說,這個功能有一定的使用場景而且實現(xiàn)也不算復(fù)雜。
原文鏈接:https://blog.csdn.net/u013113678/article/details/125363296
相關(guān)推薦
- 2022-04-29 DataTable的AcceptChanges()和RejectChanges()方法介紹并實現(xiàn)Da
- 2023-09-18 element-plus 字體變色之cell-style
- 2022-05-06 matplotlib繪制兩點間連線的幾種方法實現(xiàn)_python
- 2023-12-11 注解開發(fā)Mybatis
- 2022-04-12 C++中的異常實例詳解_C 語言
- 2022-05-23 一起來學(xué)習(xí)C++的函數(shù)指針和函數(shù)對象_C 語言
- 2022-05-29 C++的多態(tài)與虛函數(shù)你了解嗎_C 語言
- 2022-11-20 CPython?垃圾收集器檢測循環(huán)引用詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支