2013-11-25 6 views
0

libavcodec을 사용하여 flv 비디오를 인코딩하려고합니다. 다음 코드는 MPEG 비디오를 생성하는 샘플 코드이며 잘 작동합니다. 그러나 코덱 ID를 AV_CODEC_ID_FLV1로 바꾸면 생성 된 비디오 파일을 재생할 수 없습니다.libavcodec 인코딩 flv 비디오가 작동하지 않습니다.

void simpleEncode(){ 
    AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_MPEG1VIDEO); 
    AVCodecContext *ctx = avcodec_alloc_context3(codec); 
    ctx->bit_rate = 400000; 
    ctx->width = 352; 
    ctx->height = 288; 
    AVRational time_base = {1,25}; 
    ctx->time_base = time_base; 
    ctx->gop_size = 10; 
    ctx->pix_fmt = AV_PIX_FMT_YUV420P; 

    avcodec_open2(ctx, codec, NULL); 

    AVFrame *frame = av_frame_alloc(); 
    av_image_alloc(frame->data, frame->linesize, ctx->width, ctx->height, ctx->pix_fmt, 32); 
    frame->format = ctx->pix_fmt; 
    frame->height = ctx->height; 
    frame->width = ctx->width; 

    AVPacket pkt; 
    int got_output; 
    FILE *f = fopen("test.mpg", "wb"); 
    for(int i=0; i<25; i++){ 
     av_init_packet(&pkt); 
     pkt.data = NULL; 
     pkt.size = 0; 

     for(int w=0; w<ctx->width; w++){ 
      for(int h=0; h<ctx->height; h++){ 
       frame->data[0][h*frame->linesize[0]+w]=i*10; 
      } 
     } 
     for(int w=0; w<ctx->width/2; w++){ 
      for(int h=0; h<ctx->height/2; h++){ 
       frame->data[1][h*frame->linesize[1]+w]=i*10; 
       frame->data[2][h*frame->linesize[2]+w]=i*10; 
      } 
     } 

     frame->pts=i; 
     avcodec_encode_video2(ctx, &pkt, frame, &got_output); 

     fwrite(pkt.data, 1, pkt.size, f); 
    } 
} 

답변

0

AV_CODEC_ID_FLV1의 이름이 매크로가 아닙니다. Sorenson H.263 코덱을 나타냅니다. 한 번에 FLV 컨테이너 형식의 기본 코덱 이었지만 더 이상 없습니다. 그것은 VP6와 현재 h.264로 대체되었습니다. 이 히스토리를 제외하고 flv 컨테이너 형식과는 아무런 관련이 없습니다.

mpeg1 스트림은 기본 스트림 형식이 유효한 컨테이너이기 때문에 이상한 점입니다. 이것은 h.263의 경우가 아닙니다. 패킷을 디스크에 쓰고 다시 재생할 수는 없습니다. ES를 컨테이너로 캡슐화해야합니다. 가장 쉬운 방법은 libavformat을 사용하는 것입니다.