İsmail Ata KURT

24 May, 2011

libavcodec ve libavformat ile video bilgilerini öğrenmek

Posted by: Xcoder In: C/C++| Programlama

libavcodec,libavformat  free software/open source LGPL-lisanslı ffmpeg tarafından kullanılan codec çözme / işleme kütüphanesidir.

Ubuntu – Debian altında örnek kodu derleyebilmek için gerekli paketler :


$ sudo apt-get install libavcodec-dev libavformat-dev

Derlemek için :

g++ avf.cpp -I/usr/include/libavcodec -I/usr/include/libavformat -lavformat -lavcodec -L/usr/lib/libavformat.so -L/usr/lib/libavcodec.so -o avf

Örnek Kullanım :

./avf test.mp4

Çıktı :

Video codec       : mp42

Width       : 1920

Height      : 1080

Duration    : 300

Audio codec   : aac

Audio channels    : 2

Audio samplerate  : 44100

Audio bitrate     : 128

#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif

extern "C" {
#include <avcodec.h>
#include <avformat.h>
}

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

//g++ avf.cpp -I/usr/include/libavcodec -I/usr/include/libavformat -lavformat -lavcodec -L/usr/lib/libavformat.so -L/usr/lib/libavcodec.so -o avf

using namespace std;

int main(int argc, char *argv[])
{
	av_log_set_level(0);
	av_register_all();
	AVFormatContext *pFormatCtx;
	const char      *filename=argv[1];

	// Open video file
	if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0)
	{
	   cout < < "can't open file" << endl;
	   exit(-1);
	}	

	// Retrieve stream information
	if(av_find_stream_info(pFormatCtx)<0)
	{
	  cout << "can't get stream info" << endl; 	  exit(-1); 	} 	//dump_format(pFormatCtx, 0, filename, false); 	int            i, videoStream; 	AVCodecContext *pCodecCtx; //http://ffmpeg.org/doxygen/0.6/structAVCodecContext.html 	AVCodecContext *pCodecCtx2; 	 	pCodecCtx=pFormatCtx->streams[0]->codec;
	pCodecCtx2=pFormatCtx->streams[1]->codec;

	AVCodec* vCodec;//http://ffmpeg.org/doxygen/0.6/structAVCodec.html
	AVCodec* aCodec;
	vCodec=avcodec_find_decoder(pCodecCtx->codec_id);
	aCodec=avcodec_find_decoder(pCodecCtx2->codec_id);

	cout < < "Video codec       : " << vCodec->name < < endl;
	cout << "Width    	  : " << pCodecCtx->width < < endl;
	cout << "Height   	  : " << pCodecCtx->height < < endl;
	cout << "Duration 	  : " << pFormatCtx->duration < < endl;
	//cout << "codec id	  : " << (int)pCodecCtx2 << endl;
	cout << "Audio codec	  : " << aCodec->name < < endl;
	cout << "Audio channels    : " << pCodecCtx2->channels < < endl;
	cout << "Audio samplerate  : " <<  pCodecCtx2->sample_rate < < endl;
	cout << "Audio bitrate     : " <<  pCodecCtx2->bit_rate < < endl;

	avcodec_close(pCodecCtx);
	avcodec_close(pCodecCtx2);

	av_close_input_file(pFormatCtx);
	return 0;
}

Örnek kod

1 Response to "libavcodec ve libavformat ile video bilgilerini öğrenmek"

1 | user32

Ağustos 9th, 2011 at 2:26 am

Avatar

Bilgi ve emek için teşekkürler.

Comment Form

ID);?>

  • javiostr@gmail.com: Uygulama derlenirken u.data bulunamadı diyor bu sorunu nasıl hallederim??
  • alper: Hocam bu zemberek serveri windows ta yani localimde nası çalıştırabilirim ? exec komutu ile run.sh dosyasındaki komutları çalıştırmayı den
  • alper: Hocam onu şimdi indirdim. Localde wamp kurulu exec ile run.sh dosyası içindeki komutu çalıştırdım fakat yine bağlanamadı hatası alıyorum :