最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語言結(jié)合ffmpeg打印音視頻信息

 更新時(shí)間:2021年12月28日 11:56:56   作者:TrueDei  
這篇文章主要介紹了如何通過C語言或者C++編程語言結(jié)合ffmpeg拿到一些音視頻的關(guān)鍵信息,例如:幀率等。感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

一、通過此文可以得到什么

通過此練習(xí):

1、知道了如何計(jì)算一個(gè)音頻和視頻的播放時(shí)間;

2、知道了音視頻解碼的思路的大體流程,之后無非就是在這個(gè)流程上進(jìn)行擴(kuò)充細(xì)節(jié);

3、知道了如何通過C語言或者C++編程語言結(jié)合ffmpeg拿到一些音視頻的關(guān)鍵信息,例如:幀率等;

二、實(shí)現(xiàn)思路

三、實(shí)現(xiàn)效果

zhenghui@zh-pc:/data/project/VSCProject/ffmpegStudy$ make
make all
make[1]: 進(jìn)入目錄“/data/project/VSCProject/ffmpegStudy/src”
Compiling main.cpp to main.o ..
cp hello /data/project/VSCProject/ffmpegStudy/src/../
# make 【hello】 finish !!!	
make[1]: 離開目錄“/data/project/VSCProject/ffmpegStudy/src”
zhenghui@zh-pc:/data/project/VSCProject/ffmpegStudy$ 
zhenghui@zh-pc:/data/project/VSCProject/ffmpegStudy$ ./hello 
Hello World !
decode video fileName=/home/zhenghui/視頻/1080P.mp4 
[NULL @ 0x5654cc332d80] Opening '/home/zhenghui/視頻/1080P.mp4' for reading
[file @ 0x5654cc333380] Setting default whitelist 'file,crypto,data'
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] Format mov,mp4,m4a,3gp,3g2,mj2 probed with size=2048 and score=100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] ISO: File Type Major Brand: isom
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] Unknown dref type 0x206c7275 size 12
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] Processing st: 0, edit list 0 - media time: 1072, duration: 3621888
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] Offset DTS by 1072 to make first pts zero.
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] Setting codecpar->delay to 2 for stream st: 0
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] Unknown dref type 0x206c7275 size 12
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] Processing st: 1, edit list 0 - media time: 0, duration: 9990149
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] rfps: 29.916667 0.006250
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] rfps: 30.000000 0.000066
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] rfps: 60.000000 0.000265
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] rfps: 120.000000 0.001061
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] rfps: 240.000000 0.004244
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] rfps: 29.970030 0.000868
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] rfps: 59.940060 0.003473
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] Before avformat_find_stream_info() pos: 87425182 bytes read:315205 seeks:1 nb_streams:2
[h264 @ 0x5654cc3340c0] nal_unit_type: 7(SPS), nal_ref_idc: 3
[h264 @ 0x5654cc3340c0] nal_unit_type: 8(PPS), nal_ref_idc: 3
[h264 @ 0x5654cc3340c0] nal_unit_type: 7(SPS), nal_ref_idc: 3
[h264 @ 0x5654cc3340c0] nal_unit_type: 8(PPS), nal_ref_idc: 3
[h264 @ 0x5654cc3340c0] nal_unit_type: 6(SEI), nal_ref_idc: 0
[h264 @ 0x5654cc3340c0] nal_unit_type: 5(IDR), nal_ref_idc: 3
[h264 @ 0x5654cc3340c0] Format yuv420p chosen by get_format().
[h264 @ 0x5654cc3340c0] Reinit context to 1920x1088, pix_fmt: yuv420p
[h264 @ 0x5654cc3340c0] no picture 
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] All info found
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] After avformat_find_stream_info() pos: 53231 bytes read:393067 seeks:2 frames:4
video_index=0 
audio_index=1 
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/zhenghui/視頻/1080P.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
    description     : Packed by Bilibili XCoder v2.0.2
  Duration: 00:03:46.53, start: 0.000000, bitrate: 3087 kb/s
  Stream #0:0[0x1](und), 3, 1/16000: Video: h264 (High), 1 reference frame (avc1 / 0x31637661), yuv420p(progressive, left), 1920x1080 (1920x1088) [SAR 1:1 DAR 16:9], 0/1, 2951 kb/s, 30 fps, 30 tbr, 16k tbn (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
  Stream #0:1[0x2](und), 1, 1/44100: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]
media name =/home/zhenghui/視頻/1080P.mp4 
stream number: 2 
media average ratio: 3015 kbps
media total time: 0:3:46 
######## Video info: ######## 
stream index : 0 
fps:29.999914 fps
video codec : H264 
video_time : 00:03:46
######## Audio info: ######## 
stream index : 1 
stream sample_rate : 44100 Hz 
stream format : AV_SAMPLE_FMT_FLTP 
channels number:2 
audio codec:AAC 
audio_time : 00:03:46
[AVIOContext @ 0x5654cc33b7c0] Statistics: 393067 bytes read, 2 seeks
zhenghui@zh-pc:/data/project/VSCProject/ffmpegStudy$ 

四、實(shí)現(xiàn)源代碼?

視頻教程

#include <iostream>
#include <stdio.h>


#ifdef __cplusplus //表示是一個(gè)c++程序
extern "C"{
#endif

#include <libavdevice/avdevice.h>
#include <libavutil/frame.h>
#include <libavutil/imgutils.h>
#include <libavutil/log.h>
#include <libswscale/swscale.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>

#ifdef __cplusplus
}
#endif


//g++ -I ../include/ hello_world.cpp -o hello_world -L../lib/ -lavcodec -lavdevice -lavfilter -lavformat -lavutil
void decode()
{
    char *fileName = "/home/zhenghui/視頻/1080P.mp4"; 

    printf("decode video fileName=%s \n",fileName);

    int ret = 0;
    // 1、分配解復(fù)用器上下文
    AVFormatContext *ctx = NULL;
    ctx = avformat_alloc_context(); 

    // 2、根據(jù)url打開本地文件或網(wǎng)絡(luò)流
    ret = avformat_open_input(&ctx,fileName,NULL,NULL);

    if(ret < 0)
    {
        printf("打開%s失敗 ! \n",fileName);
        return;
    }

    // 3、讀取媒體的部分?jǐn)?shù)據(jù)包,獲取碼流信息
    ret = avformat_find_stream_info(ctx,NULL);
    if(ret < 0){
        printf("獲取碼流信息失敗 !\n");
        return;
    }

    // 3、獲取音頻流和視頻流的索引
    int video_index = -1;
    int audio_index = -1;

    video_index = av_find_best_stream(ctx,AVMEDIA_TYPE_VIDEO,-1,-1,NULL,0);
    printf("video_index=%d \n",video_index);

    audio_index = av_find_best_stream(ctx,AVMEDIA_TYPE_AUDIO,-1,-1,NULL,0);
    printf("audio_index=%d \n",audio_index);

    // 打印流信息
    av_dump_format(ctx,0,fileName,0);

    //打印媒體文件url
    printf("media name =%s \n",ctx->url);
    //打印流數(shù)量
    printf("stream number: %d \n",ctx->nb_streams);
    //打印碼率
    printf("media average ratio: %lld kbps\n",(int64_t)(ctx->bit_rate/1024));

    //打印時(shí)長
    int total_seconds,hour,minute,second;
    //ctx->duration的單位是微妙,先轉(zhuǎn)換成秒,再進(jìn)行轉(zhuǎn)換成其他的時(shí)間單位就比較簡單了
    total_seconds = (ctx->duration) / AV_TIME_BASE;
    hour = total_seconds / 3600;
    minute = (total_seconds % 3600) /60;
    second = (total_seconds % 60);

    // 文件總時(shí)長
    printf("media total time: %d:%d:%d \n",hour,minute,second);

    //老版本查找流索引
    for(int i = 0;i < ctx->nb_streams;i++){
        AVStream  *av_stream = ctx->streams[i];

        //判斷流是什么類型
        //視頻流
        if( AVMEDIA_TYPE_VIDEO == av_stream->codecpar->codec_type)
        {
            printf("######## Video info: ######## \n");
            // 流索引
            printf("stream index : %d \n",av_stream->index);
            //幀率
            printf("fps:%lf fps\n",av_q2d(av_stream->avg_frame_rate));

            if(AV_CODEC_ID_MPEG4 == av_stream->codecpar->codec_id)
            {
                printf("video codec : MPEG4 \n");
            }
            else if(AV_CODEC_ID_H264 == av_stream->codecpar->codec_id)
            {
                printf("video codec : H264 \n");
            }else 
            {
                printf("video codec other value=%d \n", av_stream->codecpar->codec_id);
            }

            //獲取視頻總時(shí)長
            if(AV_NOPTS_VALUE != av_stream->duration)
            {
                int video_time =av_stream->duration * av_q2d(av_stream->time_base);
                printf("video_time : %02d:%02d:%02d\n" ,
                (video_time / 3600),
                (video_time % 3600) / 60,
                (video_time % 60)
                );
            }
            else{
                printf("audio duration unknown ! \n");
            }
        }
        //音頻流
        else if(AVMEDIA_TYPE_AUDIO == av_stream->codecpar->codec_type)
        {
            printf("######## Audio info: ######## \n");
            // 流索引
            printf("stream index : %d \n",av_stream->index);

            // 音頻編解碼器的采樣率 單位Hz
            printf("stream sample_rate : %d Hz \n",av_stream->codecpar->sample_rate);

            //音頻采樣格式
            if(AV_SAMPLE_FMT_FLTP == av_stream->codecpar->format)
            {
                printf("stream format : AV_SAMPLE_FMT_FLTP \n");
            }
            else  if(AV_SAMPLE_FMT_S16P == av_stream->codecpar->format)
            {
                printf("stream format : AV_SAMPLE_FMT_S16P \n");
            }else{
                printf("stream format : other; value=%d \n",av_stream->codecpar->format);
            }

            // 音頻信道數(shù)目
            printf("channels number:%d \n",av_stream->codecpar->channels);

            //音頻壓縮編碼格式
            if(AV_CODEC_ID_AAC == av_stream->codecpar->codec_id)
            {
                printf("audio codec:AAC \n");
            }else if(AV_CODEC_ID_MP3 == av_stream->codecpar->codec_id)
            {
                printf("audio codec:MP3 \n");
            }else{
                printf("audio codec : other; value=%d \n",av_stream->codecpar->codec_id);
            }

            //獲取視頻總時(shí)長
            if(AV_NOPTS_VALUE != av_stream->duration)
            {
                int audio_time =av_stream->duration * av_q2d(av_stream->time_base);
                printf("audio_time : %02d:%02d:%02d\n" ,
                (audio_time / 3600),
                (audio_time % 3600) / 60,
                (audio_time % 60)
                );
            }
            else{
                printf("audio duration unknown ! \n");
            }

            audio_index = i;
        }

    }

    if(ctx)
        avformat_close_input(&ctx);
}

int main() {
    // 設(shè)置控制臺(tái)輸出級(jí)別
    av_log_set_level(AV_LOG_DEBUG);
    // // 打印輸出字符串Hello World !
    av_log(NULL, AV_LOG_DEBUG, "%s", "Hello World !\n");

    decode();
    return 0;
}
 

到此這篇關(guān)于C語言結(jié)合ffmpeg打印音視頻信息的文章就介紹到這了,更多相關(guān)C語言打印音視頻信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C語言直接插入排序算法介紹

    C語言直接插入排序算法介紹

    大家好,本篇文章主要講的是C語言直接插入排序算法介紹,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • C語言菜鳥基礎(chǔ)教程之Hello World

    C語言菜鳥基礎(chǔ)教程之Hello World

    C語言是一門通用計(jì)算機(jī)編程語言,應(yīng)用廣泛。C語言的設(shè)計(jì)目標(biāo)是提供一種能以簡易的方式編譯、處理低級(jí)存儲(chǔ)器、產(chǎn)生少量的機(jī)器碼以及不需要任何運(yùn)行環(huán)境支持便能運(yùn)行的編程語言。
    2017-10-10
  • vc++ 監(jiān)控指定路徑下文件變化實(shí)現(xiàn)代碼

    vc++ 監(jiān)控指定路徑下文件變化實(shí)現(xiàn)代碼

    這篇文章主要介紹了vc++ 監(jiān)控指定路徑下文件變化實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2019-04-04
  • C++數(shù)據(jù)精度問題的解決方案(對(duì)浮點(diǎn)數(shù)保存指定位小數(shù))

    C++數(shù)據(jù)精度問題的解決方案(對(duì)浮點(diǎn)數(shù)保存指定位小數(shù))

    對(duì)浮點(diǎn)數(shù)保存指定位小數(shù),怎么解決這個(gè)問題呢?如果有小伙伴對(duì)C++數(shù)據(jù)精度問題的解決方案感興趣的朋友一起看看吧
    2017-08-08
  • 詳解C語言之函數(shù)

    詳解C語言之函數(shù)

    本文是小結(jié)了一下C語言的函數(shù)語法,詳細(xì)介紹了C語言函數(shù)語法的概述、函數(shù)的定義、函數(shù)的返回值、函數(shù)調(diào)用等7個(gè)方面的內(nèi)容,非常詳細(xì),這里推薦給小伙伴們
    2021-11-11
  • C語言 遞歸解決青蛙跳臺(tái)階問題

    C語言 遞歸解決青蛙跳臺(tái)階問題

    遞歸做為一種算法在程序設(shè)計(jì)語言中廣泛應(yīng)用?;竞x&#8203;是指函數(shù)/過程/子程序在運(yùn)行過程序中直接或間接調(diào)用自身而產(chǎn)生的重入現(xiàn)象。在計(jì)算機(jī)編程里,遞歸指的是一個(gè)過程:函數(shù)不斷引用自身,直到引用的對(duì)象已知
    2021-11-11
  • C++ const引用、臨時(shí)變量 引用參數(shù)詳解

    C++ const引用、臨時(shí)變量 引用參數(shù)詳解

    下面小編就為大家?guī)硪黄狢++ const引用、臨時(shí)變量 引用參數(shù)詳解。小編覺得挺不錯(cuò)的現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01
  • 基于MATLAB神經(jīng)網(wǎng)絡(luò)圖像識(shí)別的高識(shí)別率代碼

    基于MATLAB神經(jīng)網(wǎng)絡(luò)圖像識(shí)別的高識(shí)別率代碼

    今天小編就為大家分享一篇關(guān)于基于MATLAB神經(jīng)網(wǎng)絡(luò)圖像識(shí)別的高識(shí)別率代碼,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • 詳談signed 關(guān)鍵字

    詳談signed 關(guān)鍵字

    c++中關(guān)鍵字有幾十個(gè),其中類型修飾關(guān)鍵字有l(wèi)ong, short, singed, unsigned。今天我們就來談一下經(jīng)常被大家忽視的signed關(guān)鍵字
    2015-01-01
  • C++編程小心指針被delete兩次

    C++編程小心指針被delete兩次

    這篇文章主要介紹了C++編程指針被delete兩次的嚴(yán)重后果,以實(shí)例闡述了C++指針使用中的誤區(qū)和注意點(diǎn),需要的朋友可以參考下
    2014-07-07

最新評(píng)論

武乡县| 平阴县| 房山区| 阳曲县| 定西市| 安陆市| 桐柏县| 抚远县| 西华县| 昭平县| 抚顺市| 聊城市| 循化| 辛集市| 天柱县| 西华县| 冷水江市| 浠水县| 观塘区| 阜新市| 东丰县| 唐河县| 锡林郭勒盟| 灵山县| 台南县| 东乌珠穆沁旗| 石渠县| 岫岩| 商都县| 正安县| 大庆市| 六安市| 无锡市| 保山市| 尚义县| 和平县| 泰安市| 开原市| 开封市| 朝阳县| 微山县|