Hi,

I am using Martin Böhme example from:
http://www.inb.uni-luebeck.de/~boehme/using_libavcodec.html to open the
video file and read it then implement libavcodec codec to get audio stream.

I have several attempts so far but no luck and keeping errors due to my lack
of understand how to process and save audio as wav (using defaults from
libavcodec) from video.

I am using Microsoft Visual studio 2008 and here the coding so far:

#include <libavcodec\avcodec.h>
#include <libavformat\avformat.h>

#define INBUF_SIZE 4096

int main(int argc, char *argv[]) {
    FILE            *outputFile;
    unsigned int    i, audioStream;
    int                frame_size, out_size, enout_size, enoutbuf_size, len;
    short            *samples;
    AVCodecContext    *pCodecCtx, *audioEn;
    AVCodec            *pCodec, *aCodec;
    AVFormatContext    *pFormatCtx;
    CHAR            filename[1027];
    uint8_t            *outbuf, *enoutbuf;
    AVPacket        packet;

    av_register_all();

if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0) return -1;
if(av_find_stream_info(pFormatCtx)<0)return -1;
dump_format(pFormatCtx, 0, filename, false);

    //Find audio stream
    for(i=0; i < pFormatCtx->nb_streams; i++){
        if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO)
        {
            audioStream=i;
            break;
        }
    }

    pCodecCtx=pFormatCtx->streams[audioStream]->codec;

// Find the decoder for the audio stream
    pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
    if(pCodec==NULL){
       fprintf(stderr, "Cannot find decoder");
        return -1; // Codec not found
    }

    if(avcodec_open(pCodecCtx,pCodec)<0) return -1;

    outbuf = (uint8_t*)malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);

    aCodec=avcodec_find_encoder(CODEC_ID_PCM_S16LE);
    if(pCodec==NULL){
        fprintf(stderr, "Cannot find decoder");
        return -1; // Codec not found
    }

    //setting up encoder
    audioEn = avcodec_alloc_context();
    audioEn->bit_rate = 1411000;
    audioEn->sample_rate = 44100;
    audioEn->channels = 2;
    audioEn->sample_fmt = SAMPLE_FMT_S16;

    if(avcodec_open(audioEn,aCodec)<0){
        fprintf(stderr, "Could not open encoder codec");
        std::cin.get();
    }

    frame_size = audioEn->frame_size;
    samples = (short*)malloc(frame_size * 2 * audioEn->channels);
    enoutbuf_size = 10000;
    enoutbuf = (uint8_t*)malloc(enoutbuf_size);

    outputFile = fopen(".\\temp1.wav", "wb");
    if (!outputFile) {
        fprintf(stderr, "could not open %s\n", outputFile);
        std::cin.get();
        exit(1);
    }

    while(av_read_frame(pFormatCtx, &packet)>=0){
        if(packet.stream_index==audioStream){
            for(;;){
                if (packet.size == 0)
                    break;

                while(packet.size >0){
                    out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
                    // Decode audio frame
                    len = avcodec_decode_audio3(pCodecCtx, (short *)outbuf,
&out_size, &packet);
                    if (len < 0) {
                        fprintf(stderr, "Error while decoding\n");
                        exit(1);
                    }
                    if (out_size > 0) {
                        enout_size=avcodec_encode_audio(audioEn, enoutbuf,
enoutbuf_size, samples);
                        fwrite(enoutbuf, 1, enout_size, outputFile);
                    }
                    packet.size -= len;
                    packet.data += len;
                }
            }
        av_free_packet(&packet);
        }
    }
    fclose(outputFile);
    free(outbuf);

    // Close the codec
    avcodec_close(pCodecCtx);

    // Close the video file
    av_close_input_file(pFormatCtx);
}

Regards,

Yusuke Kenta
_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user

Reply via email to