본문 바로가기

Multimedia/FFmpeg

FFMPEG : get duration

ffmpeg에서 미디어의 재생 시간을 읽는 방법을 소개한다.

 

거두절미하고 기본 코드는 아래와 같다. 

 

AVFormatContext*  pFC;

int               ret;

 

pFC = avformat_alloc_context();

 

ret  = avformat_open_input(&pFC, filename, NULL, NULL);

if (ret < 0) {

    // fail .

}

 

printf("duration %ld", pFC->duration);

 

 
자 중요한 건.. 이제부터다. 
실제로 open 함수 이후에 정보를 읽을 수 있을 것으로 생각되지만. 
몇몇 contents에서는 duration 값을 읽었을 때 0으로 나오는 경우가 있다.
 
이럴 때는 avformat_find_stream_info() 함수를 호출해야한다. 
다시 말하면 "그냥 무조건 호출하는게 좋다" 이다. 
 
이 함수가 뭐 하는 녀석이냐 하면 avformat.h 파일에 나와있듯이 
 * Read packets of a media file to get stream information. This
 * is useful for file formats with no headers such as MPEG. This
 * function also computes the real framerate in case of MPEG-2 repeat
 * frame mode.
 * The logical file position is not changed by this function;
 * examined packets may be buffered for later processing.
packet을 읽고 미디어의 정보를 수집하는 역할을 한다. 보통 header에 여러 정보들이 들어 있어서 그것을 
파싱만 하면 정보를 얻을 수도 있지만. 그렇지 않은 경우에는 이 함수를 호출해서 media의 정보를 알아내야한다.
다시말하면 실제 packet을 이용해 media정보를 구한다는 말이다.
 
암튼 위 함수를 코드에 넣어보면 
 
AVFormatContext*  pFC;

int               ret;

 

pFC = avformat_alloc_context();

 

ret  = avformat_open_input(&pFC, filename, NULL, NULL);

if (ret < 0) {

    // fail .

}

 

ret  = 

avformat_find_stream_info(pFC, NULL);if (ret < 0) {    // fail}

 

printf("duration %ld", pFC->duration);

이와 같이 추가했다. 
다음 duration을 읽으면 정상적으로 얻을 수 있는것을 확인할  수 있다.

 

'Multimedia > FFmpeg' 카테고리의 다른 글

FFMPEG 이용한 H.264 Decoding  (0) 2013.12.18