ffmpeg - rtsp sample 테스트

Programming/C++ 2012.05.25 15:49 Posted by 파란크리스마스

출처 : Record RTSP stream with FFmpeg libavformat
Android NDK FFmpeg 컴파일 강좌 (1/4)

환경

wowza 2.2.4, ffmpeg-0.10.3, cygwin, windows 7

압축풀기

$ tar xvfz ffmpeg-0.10.3.tar.gz

configure

config.sh 파일

export TMPDIR=c:/ffmpegtmp

./configure --disable-doc \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--disable-avdevice \
--disable-devices \
--disable-filters \
--disable-yasm \
--enable-network \
--enable-protocol=tcp \
--enable-demuxer=rtsp \
--enable-decoder=h264 

$ ./config.sh

$ make

testmain.c

#include <stdio.h> 
#include <stdlib.h> 
#include <libavcodec/avcodec.h> 
#include <libavformat/avformat.h> 
#include <libavformat/avio.h> 
 
 
int main(int argc, char** argv) { 
 
    AVFormatContext* context = avformat_alloc_context(); 
    int video_stream_index; 
 
    av_register_all(); 
    avcodec_register_all(); 
    avformat_network_init(); 

  	//if(avformat_open_input(&context, "rtmp://192.168.0.40/vod/sample.mp4",NULL,NULL) != 0){     	
    //if(avformat_open_input(&context, "d:\\windows\\b.mp4",NULL,NULL) != 0){ 
 
    //open rtsp 
    if(avformat_open_input(&context, "rtsp://192.168.0.40/vod/mp4:sample.mp4",NULL,NULL) != 0){ 
        return EXIT_FAILURE; 
    } 
 
    if(avformat_find_stream_info(context,NULL) < 0){ 
        return EXIT_FAILURE; 
    } 
 
    //search video stream 
    int i;
    for(i =0;i<context->nb_streams;i++){ 
        if(context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) 
            video_stream_index = i; 
    } 
 
    AVPacket packet; 
    av_init_packet(&packet); 
 
    //open output file 
    AVOutputFormat* fmt = av_guess_format(NULL,"test2.avi",NULL); 
    AVFormatContext* oc = avformat_alloc_context(); 
    oc->oformat = fmt; 
    avio_open2(&oc->pb, "test.avi", AVIO_FLAG_WRITE,NULL,NULL); 
 
    AVStream* stream=NULL; 
    int cnt = 0; 
    //start reading packets from stream and write them to file 
 
    av_read_play(context);//play RTSP 
    while(av_read_frame(context,&packet)>=0 && cnt <100){//read 100 frames 
        if(packet.stream_index == video_stream_index){//packet is video                
            if(stream == NULL){//create stream in file 
                stream = avformat_new_stream(oc,context->streams[video_stream_index]->codec->codec); 
                avcodec_copy_context(stream->codec,context->streams[video_stream_index]->codec); 
                stream->sample_aspect_ratio = context->streams[video_stream_index]->codec->sample_aspect_ratio; 
                avformat_write_header(oc,NULL); 
            } 
            packet.stream_index = stream->id; 
 
            av_write_frame(oc,&packet); 
            cnt++; 
        } 
        av_free_packet(&packet); 
        av_init_packet(&packet); 
    } 
    av_read_pause(context); 
    av_write_trailer(oc); 
    avio_close(oc->pb); 
    avformat_free_context(oc); 
 
    return (EXIT_SUCCESS); 
} 

Makefile

TARGET = hello2

FFMPEGDIR = /home/bluesanta/ffmpeg-0.10.3

LIBDIR = /lib

CC 	= gcc
AR 	= ar
LD 	= ld
NM 	= nm
RANLIB 	= ranlib
STRIP = strip

INCLUDE = -I$(FFMPEGDIR)

CFLAGS = -Wall
LDFFMPEG = -L$(FFMPEGDIR)/libavformat -L$(FFMPEGDIR)/libavcodec -L$(FFMPEGDIR)/libavutil 
LDFLAGS = -L$(LIBDIR) $(LDFFMPEG) -lavformat -lavcodec -lavutil -lkernel32 -lcygwin -lm -lgcc -lc

# application file
APPSOURCES = testmain.c
APPOBJS = $(APPSOURCES:.c=.o)

# define the rule
.SUFFIXES:.c .o 

.c.o:
	@echo Compiling: $< 
	$(CC) -c $(CFLAGS)  $(INCLUDE) -o $@ $<

all: app

app: $(APPOBJS)
	@echo Linking: $(TARGET) 
	$(CC) -o $(TARGET) $(APPOBJS) $(LDFLAGS)
	$(STRIP) -s $(TARGET)

clean:
	@rm -vf $(APPOBJS) $(TARGET) 

컴파일, 실행

신고


 

티스토리 툴바