16

AxisカメラからのRTSPストリームをFFmpeglibavformatで記録しようとしています。ファイルからビデオを取得して別のファイルに保存できます。これで問題ありません。しかし、カメラは奇妙なデータを送信し、FPSは100で、カメラは4フレームごとに送信するため、結果のFPSは約25です。結果は、100フレームのみの1時間のビデオです。

これが私のコードです

#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();

    //open rtsp
    if(avformat_open_input(&context, "rtsp://195.200.199.8/mpeg4/media.amp",NULL,NULL) != 0){
        return EXIT_FAILURE;
    }

    if(avformat_find_stream_info(context,NULL) < 0){
        return EXIT_FAILURE;
    }

    //search video stream
    for(int 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);
}

結果ファイルはここにあります:http://dl.dropbox.com/u/1243577/test.avi

アドバイスありがとうございます

4

4 に答える 4

7

これが私のやり方です。私が見つけたのは、H264 を受信したときにストリームのフレームレートが正しくないということでした。1/90000 タイムベースを送信します。着信ストリームからの新しいストリームの初期化をスキップし、特定のパラメーターをコピーするだけです。max_analyze_frames が正しく機能する場合、着信 r_frame_rate は正確である必要があります。

#include <stdio.h>
#include <stdlib.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <sys/time.h>

time_t get_time()
{
  struct timeval tv;

  gettimeofday( &tv, NULL );

  return tv.tv_sec; 
} 

int main( int argc, char* argv[] )
{
  AVFormatContext *ifcx = NULL;
  AVInputFormat *ifmt;
  AVCodecContext *iccx;
  AVCodec *icodec;
  AVStream *ist;
  int i_index;
  time_t timenow, timestart;
  int got_key_frame = 0;

  AVFormatContext *ofcx;
  AVOutputFormat *ofmt;
  AVCodecContext *occx;
  AVCodec *ocodec;
  AVStream *ost;
  int o_index;

  AVPacket pkt;

  int ix;

  const char *sProg = argv[ 0 ];
  const char *sFileInput;
  const char *sFileOutput;
  int bRunTime;

  if ( argc != 4 ) {
    printf( "Usage: %s url outfile runtime\n", sProg );
    return EXIT_FAILURE;
  } 
  sFileInput = argv[ 1 ];
  sFileOutput = argv[ 2 ];
  bRunTime = atoi( argv[ 3 ] );

  // Initialize library
  av_log_set_level( AV_LOG_DEBUG );
  av_register_all();
  avcodec_register_all(); 
  avformat_network_init();

  //
  // Input
  //

  //open rtsp
  if ( avformat_open_input( &ifcx, sFileInput, NULL, NULL) != 0 ) {
    printf( "ERROR: Cannot open input file\n" );
    return EXIT_FAILURE;
  }

  if ( avformat_find_stream_info( ifcx, NULL ) < 0 ) {
    printf( "ERROR: Cannot find stream info\n" );
    avformat_close_input( &ifcx );
    return EXIT_FAILURE;
  }

  snprintf( ifcx->filename, sizeof( ifcx->filename ), "%s", sFileInput );

  //search video stream
  i_index = -1;
  for ( ix = 0; ix < ifcx->nb_streams; ix++ ) {
    iccx = ifcx->streams[ ix ]->codec;
    if ( iccx->codec_type == AVMEDIA_TYPE_VIDEO ) {
      ist = ifcx->streams[ ix ];
      i_index = ix;
      break;
    }
  }
  if ( i_index < 0 ) {
    printf( "ERROR: Cannot find input video stream\n" );
    avformat_close_input( &ifcx );
    return EXIT_FAILURE;
  }

  //
  // Output
  //

  //open output file
  ofmt = av_guess_format( NULL, sFileOutput, NULL );
  ofcx = avformat_alloc_context();
  ofcx->oformat = ofmt;
  avio_open2( &ofcx->pb, sFileOutput, AVIO_FLAG_WRITE, NULL, NULL );

  // Create output stream
  //ost = avformat_new_stream( ofcx, (AVCodec *) iccx->codec );
  ost = avformat_new_stream( ofcx, NULL );
  avcodec_copy_context( ost->codec, iccx );

  ost->sample_aspect_ratio.num = iccx->sample_aspect_ratio.num;
  ost->sample_aspect_ratio.den = iccx->sample_aspect_ratio.den;

  // Assume r_frame_rate is accurate
  ost->r_frame_rate = ist->r_frame_rate;
  ost->avg_frame_rate = ost->r_frame_rate;
  ost->time_base = av_inv_q( ost->r_frame_rate );
  ost->codec->time_base = ost->time_base;

  avformat_write_header( ofcx, NULL );

  snprintf( ofcx->filename, sizeof( ofcx->filename ), "%s", sFileOutput );

  //start reading packets from stream and write them to file

  av_dump_format( ifcx, 0, ifcx->filename, 0 );
  av_dump_format( ofcx, 0, ofcx->filename, 1 );

  timestart = timenow = get_time();

  ix = 0;
  //av_read_play(context);//play RTSP (Shouldn't need this since it defaults to playing on connect)
  av_init_packet( &pkt );
  while ( av_read_frame( ifcx, &pkt ) >= 0 && timenow - timestart <= bRunTime ) {
    if ( pkt.stream_index == i_index ) { //packet is video               
      // Make sure we start on a key frame
      if ( timestart == timenow && ! ( pkt.flags & AV_PKT_FLAG_KEY ) ) {
        timestart = timenow = get_time();
        continue;
      }
      got_key_frame = 1;

      pkt.stream_index = ost->id;

      pkt.pts = ix++;
      pkt.dts = pkt.pts;

      av_interleaved_write_frame( ofcx, &pkt );
    }
    av_free_packet( &pkt );
    av_init_packet( &pkt );

    timenow = get_time();
  }
  av_read_pause( ifcx );
  av_write_trailer( ofcx );
  avio_close( ofcx->pb );
  avformat_free_context( ofcx );

  avformat_network_deinit();

  return EXIT_SUCCESS;
}
于 2012-11-19T16:13:53.603 に答える
4

そのようにPTS値を増やすだけではいけないと思います。タイムベースがちょうどいい場合はまれにうまくいくかもしれませんが、一般的なケースではうまくいきません。

これを変更する必要があります:

pkt.pts = ix++;
pkt.dts = pkt.pts;

これに:

pkt.pts = av_rescale_q(pkt.pts, ifcx->streams[0]->codec->time_base, ofcx->streams[0]->time_base);
pkt.dts = av_rescale_q(pkt.dts, ifcx->streams[0]->codec->time_base, ofcx->streams[0]->time_base);

これは、パケットの PTS/DTS を、入力ストリームのコーデックで使用される単位から出力ストリームの単位に変換することです。

また、一部のストリームにはフレームごとに複数のティックがあるため、ビデオが 2 倍の速度で実行される場合は、上記の行のすぐ下でこれが必要になる場合があります。

pkt.pts *= ifcx->streams[0]->codec->ticks_per_frame;
pkt.dts *= ifcx->streams[0]->codec->ticks_per_frame;
于 2013-12-19T10:34:27.447 に答える
1

最新の H.264 エンコーダーでの私の経験では、ffmpeg によって返される期間は単なる「提案」であり、PTS に「ジッター」があることがわかりました。フレーム レートまたは継続時間を決定する唯一の正確な方法は、PTS 値を使用して自分で測定することです。

30fps で実行されている H.264 エンコーダーの場合、持続時間は常に3000/90000 として報告されますが、測定された持続時間は通常 +/- 1 ですが、あるフレームでは 3000+25、次のフレームでは 3000-25 と定期的にジャンプします。反対の偏差を持つ隣接するフレームに注目し、合計時間を維持しながら 2 番目のフレームの PTS を調整することで、記録のためにこれを滑らかにしています。

これにより、クロックのずれを反映して、時折 (計算された) 持続時間が 30001 または 2999 のストリームが得られます。

29.97fps ストリームを記録する場合、av_read_frame() は常に 3000 の継続時間を返しますが、公称の計算された継続時間は 3003 (29.97 に対して正しい) であり、上記と同じジッターとドリフトがあります。

私の場合は、タイミングをクリーンアップするためにステート マシンを構築しただけです。これが誰かに役立つことを願っています。

于 2017-01-29T22:13:59.753 に答える
0

最近、同じことをしていました。FPS は、送信されたカメラの 2 倍でした。その理由は AVstream->codec->ticks_per_frame フィールドにあり、2 に設定されていました。私のソースはプログレッシブで、あなたのソースがインターリーブされていた場合、それがさらに 2 倍の理由であり、4 倍の異なる FPS が得られる可能性があります。90000 Hz は、RTSP 経由で送信されるビデオ ストリームのデフォルトのタイムベースです。タイムベースは FPS とは解像度が異なります。たとえば、タイムベースが 90000 Hz の場合、タイムスタンプが 30000 のフレームは 1/3 秒で表示されます。タイムベースは、出力中に AVstream 構造体に入れる必要がありますが、AVFormatContext には実際の FPS 値が必要です。

于 2012-02-13T03:24:20.873 に答える