title: How can I Convert raw file to an audio file (.wav) by FFmpeg in c++
body: <p>I want to covert mp4 format to wav format with different sample rate in my c++ application.
  First of all I have extracted audio from mp4 file by ffmpeg in c++, then i have converted that to a raw file, but I down not know how can I convert raw file to a wav file with different sample rate.
  How can I solve this?</p>
  
  <pre><code> #include "ffmpeg.h"
      int decode_packet(int *got_frame, int cached)
      {
          int ret = 0;
          int decoded = pkt.size;
          *got_frame = 0;
          if (pkt.stream_index == video_stream_idx) {
              /* decode video frame */
              ret = avcodec_decode_video2(video_dec_ctx, frame, got_frame, &amp;pkt);
              if (ret &lt; 0) {
      //            fprintf(stderr, "Error decoding video frame (%s)\n", av_err2str(ret));
                  return ret;
              }
              if (*got_frame) {
                  if (frame-&gt;width != width || frame-&gt;height != height ||
                      frame-&gt;format != pix_fmt) {
                      /* To handle this change, one could call av_image_alloc again and
                       * decode the following frames into another rawvideo file. */
      //                fprintf(stderr, "Error: Width, height and pixel format have to be "
      //                        "constant in a rawvideo file, but the width, height or "
      //                        "pixel format of the input video changed:\n"
      //                        "old: width = %d, height = %d, format = %s\n"
      //                        "new: width = %d, height = %d, format = %s\n",
      //                        width, height, av_get_pix_fmt_name(pix_fmt),
      //                        frame-&gt;width, frame-&gt;height,
      //                        av_get_pix_fmt_name(frame-&gt;format));
                      return -1;
                  }
                  printf("video_frame%s n:%d coded_n:%d\n",
                         cached ? "(cached)" : "",
                         video_frame_count++, frame-&gt;coded_picture_number);
                  /* copy decoded frame to destination buffer:
                   * this is required since rawvideo expects non aligned data */
                  av_image_copy(video_dst_data, video_dst_linesize,
                                (const uint8_t **)(frame-&gt;data), frame-&gt;linesize,
                                pix_fmt, width, height);
                  /* write to rawvideo file */
                  fwrite(video_dst_data[0], 1, video_dst_bufsize, video_dst_file);
              }
          } else if (pkt.stream_index == audio_stream_idx) {
              /* decode audio frame */
              ret = avcodec_decode_audio4(audio_dec_ctx, frame, got_frame, &amp;pkt);
              if (ret &lt; 0) {
      //            fprintf(stderr, "Error decoding audio frame (%s)\n", av_err2str(ret));
                  return ret;
              }
              /* Some audio decoders decode only part of the packet, and have to be
               * called again with the remainder of the packet data.
               * Sample: fate-suite/lossless-audio/luckynight-partial.shn
               * Also, some decoders might over-read the packet. */
              decoded = FFMIN(ret, pkt.size);
              if (*got_frame) {
                  size_t unpadded_linesize = frame-&gt;nb_samples * av_get_bytes_per_sample((AVSampleFormat)frame-&gt;format);
      //            printf("audio_frame%s n:%d nb_samples:%d pts:%s\n",
      //                   cached ? "(cached)" : "",
      //                   audio_frame_count++, frame-&gt;nb_samples,
      //                   av_ts2timestr(frame-&gt;pts, &amp;audio_dec_ctx-&gt;time_base));
                  /* Write the raw audio data samples of the first plane. This works
                   * fine for packed formats (e.g. AV_SAMPLE_FMT_S16). However,
                   * most audio decoders output planar audio, which uses a separate
                   * plane of audio samples for each channel (e.g. AV_SAMPLE_FMT_S16P).
                   * In other words, this code will write only the first audio channel
                   * in these cases.
                   * You should use libswresample or libavfilter to convert the frame
                   * to packed data. */
      //            fwrite(frame-&gt;extended_data[0], 1, unpadded_linesize, audio_dst_file);
                  //encode function
                  encode(cOut, frame, &amp;pktout, audio_dst_file);
  
  
      //            av_init_packet(&amp;pktout);
      //            pktout.data = NULL; // packet data will be allocated by the encoder
      //            pktout.size = 0;
      //            /* encode the samples */
      //            ret = avcodec_encode_audio2(cOut, &amp;pktout, frame, &amp;got_outputOut);
      //            if (ret &lt; 0) {
      //                fprintf(stderr, "Error encoding audio frame\n");
      //                exit(1);
      //            }
      //            if (got_outputOut) {
      //                fwrite(pktout.data, 1, pktout.size, audio_dst_file);
      //                av_free_packet(&amp;pktout);
      //            }
  
              }
          }
          /* If we use frame reference counting, we own the data and need
           * to de-reference it when we don't use it anymore */
          if (*got_frame &amp;&amp; refcount)
              av_frame_unref(frame);
          return decoded;
      }
  </code></pre>