欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

FFmpeg實(shí)戰(zhàn)之利用ffplay實(shí)現(xiàn)自定義輸入流播放

 更新時(shí)間:2022年12月16日 15:35:23   作者:CodeOfCC  
ffplay是FFmpeg提供的一個(gè)極為簡(jiǎn)單的音視頻媒體播放器,可以用于音視頻播放、可視化分析。本文將利用ffplay實(shí)現(xiàn)自定義輸入流播放,需要的可以參考一下

前言

使用ffplay播放視頻,有時(shí)我們只能獲取到byte數(shù)據(jù),比如Windows的嵌入資源只能拿到在內(nèi)存中的視頻文件數(shù)據(jù),或者是自定義協(xié)議網(wǎng)絡(luò)傳輸?shù)囊曨l,這個(gè)時(shí)候我們就需要實(shí)現(xiàn)一個(gè)流數(shù)據(jù)輸入接口來(lái)進(jìn)行播放了,ffmpeg的AVIOContext就支持這一功能,我們只需要對(duì)ffplay進(jìn)行簡(jiǎn)單的拓展即可。

一、如何使用AVIOContext

avio是ffmpeg自定義輸入流的對(duì)象,它是AVformatContext的一個(gè)字段,我只需要?jiǎng)?chuàng)建avio對(duì)象并實(shí)現(xiàn)其回調(diào)方法,然后給AVformatContext.pb賦值即可。

1、定義回調(diào)方法

以文件流為例(省略了打開文件和獲取文件長(zhǎng)度的操作)

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">播放器對(duì)象</param>
/// <param name="read">自定義輸入流,讀取數(shù)據(jù)時(shí)的回調(diào)</param>
/// <param name="seek">自定義輸入流,定位時(shí)的回調(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)容,之所以去實(shí)現(xiàn)這樣的功能是因?yàn)楣P者曾經(jīng)工作中,遇到過(guò)相關(guān)使用場(chǎng)景,在程序啟動(dòng)時(shí)播放mp4嵌入資源,將其讀取出來(lái)保存文件在播放顯然不是很好的方案,而且ffmpeg本身支持自定義輸入流,所以很容易就將此功能添加到ffplay上了。總的來(lái)說(shuō),這個(gè)功能有一定的使用場(chǎng)景而且實(shí)現(xiàn)也不算復(fù)雜。

到此這篇關(guān)于FFmpeg實(shí)戰(zhàn)之利用ffplay實(shí)現(xiàn)自定義輸入流播放的文章就介紹到這了,更多相關(guān)FFmpeg ffplay自定義輸入流播放內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論