In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to print audio and video information with C language and ffmpeg. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
What can be obtained through this article
Through this activity:
1. Know how to calculate the playback time of an audio and video
2. Know the general flow of audio and video decoding, and then nothing more than expand the details of this process
3. Know how to get some key information of audio and video through C language or C++ programming language combined with ffmpeg, such as frame rate, etc.
Second, the realization train of thought
3. Zhenghui@zh-pc:/data/project/VSCProject/ffmpegStudy$ makemake allmake [1]: enter the directory "/ data/project/VSCProject/ffmpegStudy/src" Compiling main.cpp to main.o.. cp hello / data/project/VSCProject/ffmpegStudy/src/../# make [hello] finish!! Make [1]: leave the directory "/ 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/ video / 1080P.mp4 [NULL @ 0x5654cc332d80] Opening'/ home/zhenghui/ video / 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 [h364 @ 0x5654cc3340c0] nal_unit_type: 7 (SPS), nal_ref_idc: 3 [h364 @ 0x5654cc3340c0] nal_unit_type: 8 (PPS), nal_ref_idc: 3 [h364 @ 0x5654cc3340c0] nal_unit_type: 7 (SPS), nal_ref_idc: 3 [h364 @ 0x5654cc3340c0] nal_unit_type: 8 (PPS) Nal_ref_idc: 3 [h364 @ 0x5654cc3340c0] nal_unit_type: 6 (SEI), nal_ref_idc: 0 [h364 @ 0x5654cc3340c0] nal_unit_type: 5 (IDR), nal_ref_idc: 3 [h364 @ 0x5654cc3340c0] Format yuv420p chosen by get_format (). [h364 @ 0x5654cc3340c0] Reinit context to 1920x1088, pix_fmt: yuv420p [h364 @ 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:4video_index=0 audio_index=1 Input # 0, mov,mp4,m4a,3gp,3g2,mj2, from'/ home/zhenghui/ Video / 1080P.mp4video: Metadata: major_brand: isom minor_version: 512 compatible_brands: isomiso2avc1mp41 encoder: Lavf58.29.100 description: Packed by Bilibili XCoder v2.0.2 Duration: 00VO3VOUR 46.53, start: 0.000000 Bitrate: 3087 kb/s Stream # 0:0 [0x1] (und), 3,1go 16000: Video: h 364 (High), 1 reference frame (avc1 / 0x31637661), yuv420p (progressive, left), 1920x1080 (1920x1088) [SAR 1:1 DAR 16:9], 0 kb/s Stream 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,1ax 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/ Video / 1080P.mp4 stream number: 2 media average ratio: 3015 kbpsmedia total time: 0:3:46 # Video info: # stream index: 0 fps:29.999914 fpsvideo codec: H264 video_time: 00Vera 03VR 46mm # 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 seekszhenghui@zh-pc:/data/project/VSCProject/ffmpegStudy$ IV. Implementation source code
Video tutorial
# include # include # ifdef _ _ cplusplus / / indicates a C++ program extern "C" {# endif#include # ifdef _ cplusplus} # endif//g++-I.. / include/ hello_world.cpp-o hello_world-L../lib/-lavcodec-lavdevice-lavfilter-lavformat-lavutilvoid decode () {char * fileName= "/ home/zhenghui/ Video / 1080P.mp4"; printf ("decode video fileName=%s\ n", fileName) Int ret = 0; / / 1, assign demultiplexer context AVFormatContext * ctx = NULL; ctx = avformat_alloc_context (); / / 2, open local file or network stream according to url ret = avformat_open_input (& ctx,fileName,NULL,NULL); if (ret
< 0) { printf("打开%s失败 ! \n",fileName); return; } // 3、读取媒体的部分数据包,获取码流信息 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); / / number of print streams printf ("stream number:% d\ n", ctx- > nb_streams); / / print bitrate printf ("media average ratio:% lld kbps\ n", (int64_t) (ctx- > bit_rate/1024)); / / print duration int total_seconds,hour,minute,second / / the unit of ctx- > duration is subtle, so it is relatively simple to convert it to seconds and then to other units of time: total_seconds = (ctx- > duration) / AV_TIME_BASE; hour = total_seconds / 3600; minute = (total_seconds% 3600) / 60; second = (total_seconds% 60) / / the total file length printf ("media total time:% d:%d:%d\ n", hour,minute,second); / / the old lookup stream index for (int I = 0 * * I)
< ctx->Nb_streams;i++) {AVStream * av_stream = ctx- > streams [I]; / determine the type of video stream / / Video stream if (AVMEDIA_TYPE_VIDEO = = av_stream- > codecpar- > codec_type) {printf ("# Video info: #\ n") / / stream index printf ("stream index:% d\ n", av_stream- > index); / / frame rate 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);} / / get the total video duration if (AV_NOPTS_VALUE! = av_stream- > duration) {int video_time = av_stream- > duration * av_q2d (av_stream- > time_base) Printf ("video_time: d:d:d\ n", (video_time / 3600), (video_time% 3600) / 60, (video_time% 60);} else {printf ("audio duration unknown!\ n") }} / / Audio streaming else if (AVMEDIA_TYPE_AUDIO = = av_stream- > codecpar- > codec_type) {printf ("# Audio info: #\ n"); / / streaming index printf ("stream index:% d\ n", av_stream- > index) / / sampling rate of audio codec in Hz printf ("stream sample_rate:% d Hz\ n", av_stream- > codecpar- > sample_rate); / / audio sampling format 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);} / / number of audio channels printf (" channels number:%d\ n ", av_stream- > codecpar- > channels) / / Audio compression format 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);} / / get the total video duration if (AV_NOPTS_VALUE! = av_stream- > duration) {int audio_time = av_stream- > duration * av_q2d (av_stream- > time_base) Printf ("audio_time: d:d:d\ 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 () {/ / set console output level av_log_set_level (AV_LOG_DEBUG); / printout string Hello World! Av_log (NULL, AV_LOG_DEBUG, "% s", "Hello World!\ n"); decode (); return 0;} after reading the above, do you have any further understanding of how C language combined with ffmpeg prints audio and video information? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.