当前位置:网站首页>Ffmpe a small demo to understand 80% of common APIs
Ffmpe a small demo to understand 80% of common APIs
2022-06-11 07:26:00 【Nwafu_ zyccc】
#include <stdio.h>
#include <libavformat/avformat.h>
int main(int argc, char **argv)
{
// Turn on network flow . Here, if you only need to read local media files , No need to use network functions , You don't have to add this sentence
// avformat_network_init();
const char *default_filename = "";
char *in_filename = NULL;
if(argv[1] == NULL)
{
in_filename = default_filename;
}
else
{
in_filename = argv[1];
}
printf("in_filename = %s\n", in_filename);
//AVFormatContext It is a structure that describes the composition and basic information of a media file or media stream
AVFormatContext *ifmt_ctx = NULL; // Input file demux
int videoindex = -1; // Video index
int audioindex = -1; // Audio index
// Open file , Mainly the detection protocol type , If it is a network file, create a network link
int ret = avformat_open_input(&ifmt_ctx, in_filename, NULL, NULL);
if (ret < 0) // If opening the media file fails , Reason for printing failure
{
char buf[1024] = {
0 };
av_strerror(ret, buf, sizeof(buf) - 1);
printf("open %s failed:%s\n", in_filename, buf);
goto failed;
}
ret = avformat_find_stream_info(ifmt_ctx, NULL);
if (ret < 0) // If opening the media file fails , Reason for printing failure
{
char buf[1024] = {
0 };
av_strerror(ret, buf, sizeof(buf) - 1);
printf("avformat_find_stream_info %s failed:%s\n", in_filename, buf);
goto failed;
}
// Media file opened successfully
printf_s("\n==== av_dump_format in_filename:%s ===\n", in_filename);
av_dump_format(ifmt_ctx, 0, in_filename, 0);
printf_s("\n==== av_dump_format finish =======\n\n");
// url: call avformat_open_input The path of the read media file / name
printf("media name:%s\n", ifmt_ctx->url);
// nb_streams: nb_streams Number of media streams
printf("stream number:%d\n", ifmt_ctx->nb_streams);
// bit_rate: Bit rate of media file , Unit is bps
printf("media average ratio:%lldkbps\n",(int64_t)(ifmt_ctx->bit_rate/1024));
// Time
int total_seconds, hour, minute, second;
// duration: Media file duration , Unit subtlety
total_seconds = (ifmt_ctx->duration) / AV_TIME_BASE; // 1000us = 1ms, 1000ms = 1 second
hour = total_seconds / 3600;
minute = (total_seconds % 3600) / 60;
second = (total_seconds % 60);
// Through the above operation , You can get the total length of media files
printf("total duration: %02d:%02d:%02d\n", hour, minute, second);
printf("\n");
/*
* The old version reads the information of media files, video and audio through traversal
* The new version of the FFmpeg Newly added function av_find_best_stream, The same effect can be achieved
*/
for (uint32_t i = 0; i < ifmt_ctx->nb_streams; i++)
{
AVStream *in_stream = ifmt_ctx->streams[i];// Audio stream 、 Video streaming 、 Subtitle stream
// If it's an audio stream , Then print the audio information
if (AVMEDIA_TYPE_AUDIO == in_stream->codecpar->codec_type)
{
printf("----- Audio info:\n");
// index: Each stream component is in ffmpeg After demultiplexing analysis, there is a unique index As identification
printf("index:%d\n", in_stream->index);
// sample_rate: Sampling rate of audio codec , Unit is Hz
printf("samplerate:%dHz\n", in_stream->codecpar->sample_rate);
// codecpar->format: Audio sampling format
if (AV_SAMPLE_FMT_FLTP == in_stream->codecpar->format)
{
printf("sampleformat:AV_SAMPLE_FMT_FLTP\n");
}
else if (AV_SAMPLE_FMT_S16P == in_stream->codecpar->format)
{
printf("sampleformat:AV_SAMPLE_FMT_S16P\n");
}
// channels: Number of audio channels
printf("channel number:%d\n", in_stream->codecpar->channels);
// codec_id: Audio compression coding format
if (AV_CODEC_ID_AAC == in_stream->codecpar->codec_id)
{
printf("audio codec:AAC\n");
}
else if (AV_CODEC_ID_MP3 == in_stream->codecpar->codec_id)
{
printf("audio codec:MP3\n");
}
else
{
printf("audio codec_id:%d\n", in_stream->codecpar->codec_id);
}
// Total audio duration , The unit is in seconds . Note that if you enlarge the unit to milliseconds or subtle , The total audio duration is not necessarily equal to the total video duration
if(in_stream->duration != AV_NOPTS_VALUE)
{
int duration_audio = (in_stream->duration) * av_q2d(in_stream->time_base);
// Convert the total audio duration into minutes and seconds and print it to the console
printf("audio duration: %02d:%02d:%02d\n",
duration_audio / 3600, (duration_audio % 3600) / 60, (duration_audio % 60));
}
else
{
printf("audio duration unknown");
}
printf("\n");
audioindex = i; // Get the index of audio
}
else if (AVMEDIA_TYPE_VIDEO == in_stream->codecpar->codec_type) // If it's a video stream , Then print the information of the video
{
printf("----- Video info:\n");
printf("index:%d\n", in_stream->index);
// avg_frame_rate: Video frame rate , Unit is fps, Indicates how many frames appear per second
printf("fps:%lffps\n", av_q2d(in_stream->avg_frame_rate));
if (AV_CODEC_ID_MPEG4 == in_stream->codecpar->codec_id) // Video compression coding format
{
printf("video codec:MPEG4\n");
}
else if (AV_CODEC_ID_H264 == in_stream->codecpar->codec_id) // Video compression coding format
{
printf("video codec:H264\n");
}
else
{
printf("video codec_id:%d\n", in_stream->codecpar->codec_id);
}
// Video frame width and frame height
printf("width:%d height:%d\n", in_stream->codecpar->width,
in_stream->codecpar->height);
// Total video duration , The unit is in seconds . Note that if you enlarge the unit to milliseconds or subtle , The total audio duration is not necessarily equal to the total video duration
if(in_stream->duration != AV_NOPTS_VALUE)
{
int duration_video = (in_stream->duration) * av_q2d(in_stream->time_base);
printf("video duration: %02d:%02d:%02d\n",
duration_video / 3600,
(duration_video % 3600) / 60,
(duration_video % 60)); // Convert the total video duration into minutes and seconds and print it to the console
}
else
{
printf("video duration unknown");
}
printf("\n");
videoindex = i;
}
}
AVPacket *pkt = av_packet_alloc();
int pkt_count = 0;
int print_max_count = 10;
printf("\n-----av_read_frame start\n");
while (1)
{
ret = av_read_frame(ifmt_ctx, pkt);
if (ret < 0)
{
printf("av_read_frame end\n");
break;
}
if(pkt_count++ < print_max_count)
{
if (pkt->stream_index == audioindex)
{
printf("audio pts: %lld\n", pkt->pts);
printf("audio dts: %lld\n", pkt->dts);
printf("audio size: %d\n", pkt->size);
printf("audio pos: %lld\n", pkt->pos);
printf("audio duration: %lf\n\n",
pkt->duration * av_q2d(ifmt_ctx->streams[audioindex]->time_base));
}
else if (pkt->stream_index == videoindex)
{
printf("video pts: %lld\n", pkt->pts);
printf("video dts: %lld\n", pkt->dts);
printf("video size: %d\n", pkt->size);
printf("video pos: %lld\n", pkt->pos);
printf("video duration: %lf\n\n",
pkt->duration * av_q2d(ifmt_ctx->streams[videoindex]->time_base));
}
else
{
printf("unknown stream_index:\n", pkt->stream_index);
}
}
av_packet_unref(pkt);
}
if(pkt)
av_packet_free(&pkt);
failed:
if(ifmt_ctx)
avformat_close_input(&ifmt_ctx);
getchar(); // Add this sentence , Prevent the program from exiting immediately after printing the information
return 0;
}
边栏推荐
- The gap between the parent box and the child box
- Matplotlib,设置坐标刻度大小,字体/设置图例大小及字体/设置纵横坐标名称及字体及大小
- MySQL设置管理员密码无法生效的案例一则
- pycharm出现error.DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
- What is the lifecycle of automated testing?
- Adventure of small X
- MS office level II wrong question record [8]
- [STL source code analysis] summary notes (7): ingenious deque
- 1、 Sqlserver2008 installation (with password), database creation, C form project test
- 资深OpenStacker - 彭博、Vexxhost升级为OpenInfra基金会黄金成员
猜你喜欢
![Error occurred in pycharm DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])](/img/1c/4013479ce1fc5b0ff2ebeb754f05a9.png)
Error occurred in pycharm DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
![[analysis of STL source code] summary notes (3): vector introduction](/img/70/faa40c273f6b3a6b124fb870058489.jpg)
[analysis of STL source code] summary notes (3): vector introduction

2022低压电工操作证考试题模拟考试平台操作
![[analysis of STL source code] summary note (4): behind the scenes hero allocator](/img/b9/cf53fd8f933042ff65844d61eca55e.jpg)
[analysis of STL source code] summary note (4): behind the scenes hero allocator

【编译原理】05-语法制导的语义计算——基于翻译模式的语义计算
![[Oracle database] mammy tutorial day02 use of database management tool sqlplus](/img/f2/8f6f74a62427ebfb4c805c1e9b3352.png)
[Oracle database] mammy tutorial day02 use of database management tool sqlplus

2、 User login and registration

Leetcode-104. Maximum Depth of Binary Tree

webserver
![[STL source code analysis] summary notes (7): ingenious deque](/img/da/8ec42bfdbbf1b5bd1c2e396c2213e2.jpg)
[STL source code analysis] summary notes (7): ingenious deque
随机推荐
Education expert wangzhongze solves students' problems with one move
[并发进阶]——线程池总结
QT interface nested movement based on qscrollarea
Building a full-featured NAS server with raspberry pie (06): built-in file synchronization tool for penetration
Library management system 2- demand analysis
CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码
资深OpenStacker - 彭博、Vexxhost升级为OpenInfra基金会黄金成员
软件测试周刊(第75期):唯有平视,才能看见真实的自己。
Pointer to a two-dimensional array
【CF】 A. New Year Candles
Analyse du contrat du modèle de taux composé
Adventure of small X
CMAP of Matplotlib
es5和es6的学习小记
2022 low voltage electrician test questions and online simulation test
Interview question 02.06 Palindrome linked list
Paper size and book size
Senior openstacker - Bloomberg, vexxhost upgraded to the Gold member of openinfra Foundation
matplotlib的cmap
【CF#262 (Div. 2)】 A. Vasya and Socks