Hi Andrew, Thank you for the code, had a look through and I understand it now. I used the avformat_write_header, frame and trailer from libavformat to do it for me and now I have wav file outputted.
On Sun, Mar 14, 2010 at 3:54 AM, Andrew Hakman <[email protected]>wrote: > It's attached. Guess I never did change the main file yet (I was > writing some of the threads in separate files, but I thought I had > changed the main file as well - guess I didn't yet). > > Should do exactly what you want. > > Follow the wave header info on the link I provided previously to > figure out what's happening with writing the header, and right at the > end where I go back and fill in the length fields. > > Andrew > > On Sat, Mar 13, 2010 at 3:54 PM, Yusuke Kenta <[email protected]> wrote: > > Hi Andrew, > > > > Thank you for the reply. Now I understand why I am getting wav files but > has > > no information about it. It will be great if you can send some bits of > the > > code to help me understand how to write the header. > > > > Regards, > > > > Yusuke Kenta > > > > On Sat, Mar 13, 2010 at 10:11 PM, Andrew Hakman <[email protected] > >wrote: > > > >> Don't re-encode to "wav", just write out the header yourself, and dump > >> out the samples you get from decode_audio_3. > >> For a good explanation of the header specifics, see > >> https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ > >> > >> I had code that did just that a couple weeks ago, but it's in a > >> non-functional state right now as I'm breaking it into multiple > >> threads (and writing wav files wasn't the end goal, just an > >> intermediate step to see that things are working to that point). > >> Haven't had much time to work on that program recently, but I could > >> probably extract some bits of it and send it to you if you thought it > >> would help. > >> > >> Andrew > >> > >> On Sat, Mar 13, 2010 at 9:09 AM, Edward Hau <[email protected]> wrote: > >> > Hi, > >> > > >> > I am using Martin Böhme example from: > >> > http://www.inb.uni-luebeck.de/~boehme/using_libavcodec.html<http://www.inb.uni-luebeck.de/%7Eboehme/using_libavcodec.html> > <http://www.inb.uni-luebeck.de/%7Eboehme/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 > >> > > >> _______________________________________________ > >> libav-user mailing list > >> [email protected] > >> https://lists.mplayerhq.hu/mailman/listinfo/libav-user > >> > > _______________________________________________ > > libav-user mailing list > > [email protected] > > https://lists.mplayerhq.hu/mailman/listinfo/libav-user > > > > _______________________________________________ > libav-user mailing list > [email protected] > https://lists.mplayerhq.hu/mailman/listinfo/libav-user > > _______________________________________________ libav-user mailing list [email protected] https://lists.mplayerhq.hu/mailman/listinfo/libav-user
