網站首頁 編程語言 正文
前言
使用ffplay播放視頻,有時我們只能獲取到byte數據,比如Windows的嵌入資源只能拿到在內存中的視頻文件數據,或者是自定義協議網絡傳輸的視頻,這個時候我們就需要實現一個流數據輸入接口來進行播放了,ffmpeg的AVIOContext就支持這一功能,我們只需要對ffplay進行簡單的拓展即可。
一、如何使用AVIOContext
avio是ffmpeg自定義輸入流的對象,它是AVformatContext的一個字段,我只需要創建avio對象并實現其回調方法,然后給AVformatContext.pb賦值即可。
1、定義回調方法
以文件流為例(省略了打開文件和獲取文件長度的操作)
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、關聯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">自定義輸入流,讀取數據時的回調</param>
/// <param name="seek">自定義輸入流,定位時的回調</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、關聯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;
}
總結
以上就是今天要講的內容,之所以去實現這樣的功能是因為筆者曾經工作中,遇到過相關使用場景,在程序啟動時播放mp4嵌入資源,將其讀取出來保存文件在播放顯然不是很好的方案,而且ffmpeg本身支持自定義輸入流,所以很容易就將此功能添加到ffplay上了。總的來說,這個功能有一定的使用場景而且實現也不算復雜。
原文鏈接:https://blog.csdn.net/u013113678/article/details/125363296
相關推薦
- 2023-05-31 python常用函數random()函數詳解_python
- 2022-06-01 Python實現圖像的二進制與base64互轉_python
- 2022-03-19 CentOS7下安裝MongoDB數據庫過程_MongoDB
- 2022-11-05 Python入門教程之運算符重載詳解_python
- 2022-11-21 詳解如何使用Python實現刪除重復文件_python
- 2022-08-02 使用shell讀取ini文件方法步驟_linux shell
- 2022-09-17 Golang文件讀寫操作詳情_Golang
- 2022-12-04 WxPython中控件隱藏與顯示的小技巧_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支