2013-10-20 4 views
0
내가 .mp3.m4a 파일 시도
int main(int argc, char **argv) 
{ 
    av_register_all(); 
    avcodec_register_all(); 

    char *filename = argv[1]; 
    char *outfilename = argv[2]; 

    FILE *outfile; 

    AVCodec *codec; 
    AVCodecContext *c= NULL; 
    AVPacket avpkt; 
    AVFrame *frame = av_frame_alloc(); 

    printf("Decode audio file %s to %s\n", filename, outfilename); 

    outfile = fopen(outfilename, "wb"); 
    if (!outfile) { 
     fprintf(stderr, "Could not write to %s\n", outfilename); 
     av_free(c); 
     exit(1); 
    } 

    AVFormatContext *format_context = NULL; 
    avformat_open_input(&format_context, filename, NULL, NULL); 
    printf("Opened format input\n"); 

    int find_result = avformat_find_stream_info(format_context, NULL); 
    if (find_result < 0) { 
     fprintf(stderr, "Cannot find stream info\n"); 
     avformat_close_input(&format_context); 
     exit(-1); 
    } 

    int audio_stream_idx = av_find_best_stream(format_context, AVMEDIA_TYPE_AUDIO, -1, -1, &codec, 0); 

    if (audio_stream_idx < 0) { 
     fprintf(stderr,"Couldn't find stream information\n"); 
     exit(-1); 
    } 

    // Get a pointer to the codec context for the audio stream 
    c = format_context->streams[audio_stream_idx]->codec; 
    av_opt_set_int(c, "refcounted_frames", 1, 0); 

    if (avcodec_open2(c, codec, NULL) < 0) { 
     fprintf(stderr, "Could not open codec\n"); 
     exit(-1); 
    } 

    // read the audio frames 
    int ret, got_frame; 
    while (1) { 
     if ((ret = av_read_frame(format_context, &avpkt)) < 0) 
      break; 
     if (avpkt.stream_index == audio_stream_idx) { 
      avcodec_get_frame_defaults(frame); 
      got_frame = 0; 
      ret = avcodec_decode_audio4(c, frame, &got_frame, &avpkt); 
      if (ret < 0) { 
       fprintf(stderr, "Error decoding audio\n"); 
       continue; 
      } 

      if (got_frame) { 
       // write to disk 
       fwrite(frame->extended_data[0], 1, frame->linesize[0], outfile); 
      } 
     } 
     av_free_packet(&avpkt); 
    } 

    fclose(outfile); 

    printf("Finished\n"); 
    if (c) 
     avcodec_close(c); 
    avformat_close_input(&format_context); 
    av_frame_free(&frame); 
} 

오디오 파일디코딩 M4A 및 덤핑 PCM 데이터는 노이즈

를 디코딩하기 (위해 libavcodec에 주어진 예에서 변형) 아래의 코드를 사용하고

돌려 준다; .mp3 파일은 정상적으로 작동하지만 .m4a 파일은 작동하지 않습니다. 어떤 도움이 필요합니까?

+0

가장 먼저 확인해야 할 것은 FFmpeg 빌드에 MPEG-4 파서와 AAC 디코더가 들어 있는지 확인하는 것입니다. –

+0

@RomanR. 그렇습니다. 코드를 개선 했으므로 한 채널을 올바르게 출력 할 수 있습니다. 그러나 다른 채널은 여전히 ​​정적 잡음입니다 .. – lynnard

+0

@lynnard. 나는 똑같은 문제를 겪고 있으며 아직도 그것에 고심하고있다. mp3 오디오는 재생할 수 있지만 aac은 재생할 수 없습니다. 나는 모든 코덱과 파서로 ffmpeg를 재구성했지만 여전히 성공하지 못했다. 당신이나 당신의 시체가 이것에 대한 해결책을 찾았습니까? –

답변

2

대부분 aac 파일은 FLOAT 형식, 즉 AV_SAMPLE_FMT_FLTP 인 반면, libao과 같은 라이브러리는 오디오를 정수 형식, 즉 AV_SAMPLE_FORMAT_S16으로 만 재생할 수 있습니다.

따라서 음악을 다시 샘플링하고 적절하게 형식을 변환하려면 libavresample을 사용해야합니다.