2013-12-11 5 views
0

비디오 스트림을 mp4 파일로 저장하는 방법에 대해서는 상당히 혼란 스럽습니다. 나는 ffmpeg를 사용하고있다. 나에게 문제를 설명하자 : 나는 avformat_open_input (와 RTSP (H.264 스트림)를 통해 네트워크 카메라에 연결네트워크 카메라 (RTSP)에서 mp4 파일로 프레임 저장

  1. )를 avformat_find_stream_info(), av_read_play(), 나는 av_read_frame와 프레임 수().
  2. av_read_frame()이있는 프레임을 얻을 때마다 해당 AVPacket을 순환 버퍼에 저장합니다.
  3. 내 응용 프로그램의 일부 지점에서이 순환 버퍼의 범위가 선택됩니다. 나는 시작하는 데 사용되는 키 프레임을 찾는다.
  4. 키 프레임에서 시작하는 AVPacket의 목록이 있으면 코드에서 설명한대로 헤더, 프레임 및 꼬리를 씁니다.

문제는 VLC, Windows Media Player 또는 다른 것을 사용하여 볼 때 생성되는 mp4 비디오에 인공물이 있다는 것입니다.

또한 패킷의 pts는 연속적이지 않고 dts는 연속적이라는 것을 깨달았습니다. 나는 B 프레임에 대해 알고 있지만 이것이 내 문제인가?

// Prepare the output 
AVFormatContext* oc = avformat_alloc_context(); 
oc->oformat = av_guess_format(NULL, "video.mp4", NULL); 

// Must write header, packets, and trailing 
avio_open2(&oc->pb, "video.mp4", AVIO_FLAG_WRITE, NULL, NULL); 

// Write header 
AVStream* stream = avformat_new_stream(oc, (AVCodec*) 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); 

// FOR EACH FRAME... 
... av_write_frame(oc, circular[k]); ... 

// Write trailer and close the file 
av_write_trailer(oc); 
avcodec_close(stream->codec); 
avio_close(oc->pb); 
avformat_free_context(oc); 

+0

참고로 rtmpdump를 보거나 (심지어 librtmp를 직접 사용해도됩니다) - http://rtmpdump.mplayerhq.hu/ – benjymous

답변

-1

첫째, 너무 감사합니다 카메라로 작업 할 때, (전송 프로토콜로 TCP)을 TCP를 통한 RTP를 통해 작업하는 것이 좋습니다. , 패킷이 오는 시작 후 첫 번째 키 프레임을 기다리는이 순간부터 파일에 기록하기 시작 : 두 번째

AVDictionary *ifmtdict; 
av_dict_set(&ifmtdict, "rtsp_transport", "tcp", 0); 
... 
avformat_open_input (..., &ifmtdict); 

: 는이 기능을 사용하려면.

+0

또한 많은 IP 카메라에서 많은 비디오 스트림을 작성하고 있습니다. 모든 좋은 :) 내 영어로 죄송합니다. – Eugene