pastebin - collaborative debugging tool
kpaste.net RSS


ffmpeg examepl
Posted by Anonymous on Fri 15th Aug 2025 21:55
raw | new post

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <libavcodec/avcodec.h>
  4. #include <libavformat/avformat.h>
  5. #include <libavutil/avutil.h>
  6. #include <alsa/asoundlib.h>
  7.  
  8. void
  9. die(char *why)
  10. {
  11.         fprintf(stderr, "%s\n", why);
  12.         exit(1);
  13. }
  14.  
  15. int
  16. main(int argc, char *argv[])
  17. {
  18.         if (argc < 2)
  19.                 die("argc");
  20.  
  21.         printf("[INFO] %s\n", argv[1]);
  22.  
  23.         snd_pcm_t *handle;
  24.         if (snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0)
  25.                 die("snd_pcm_open");
  26.  
  27.  
  28.         struct AVFormatContext *formatContext;
  29.         if (avformat_open_input(&formatContext, argv[1], NULL, NULL) < 0)
  30.                 die("avformat_open_input");
  31.  
  32.         if (avformat_find_stream_info(formatContext, NULL) < 0) {
  33.                 die("avformat_find_stream_info");
  34.         }
  35.  
  36.         int streamId = -1;
  37.         streamId = av_find_best_stream(formatContext, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
  38.  
  39.         if (streamId == -1)
  40.                 die("streamId");
  41.  
  42.         const struct AVCodec *codec = avcodec_find_decoder(formatContext->streams[streamId]->codecpar->codec_id);
  43.         struct AVCodecContext *codecContext = avcodec_alloc_context3(codec);
  44.  
  45.         if (avcodec_parameters_to_context(codecContext, formatContext->streams[streamId]->codecpar)) {
  46.                 avcodec_free_context(&codecContext);
  47.                 die("avcodec_parameters_to_context");
  48.         }
  49.  
  50.         if (avcodec_open2(codecContext, codec, NULL) < 0) {
  51.                 avcodec_free_context(&codecContext);
  52.                 die("avcodec_open2");
  53.         }
  54.  
  55.         snd_pcm_set_params(handle,
  56.                         SND_PCM_FORMAT_S16_LE,
  57.                         SND_PCM_ACCESS_RW_INTERLEAVED,
  58.                         formatContext->streams[streamId]->codecpar->ch_layout.nb_channels,
  59.                         codecContext->sample_rate,
  60.                         1, 500000);
  61.  
  62.         AVPacket *packet = av_packet_alloc();
  63.         AVFrame *frame = av_frame_alloc();
  64.  
  65.         while (av_read_frame(formatContext, packet) >= 0)
  66.         {
  67.                 if (avcodec_send_packet(codecContext, packet) != 0)
  68.                         break;
  69.  
  70.                 while (!avcodec_receive_frame(codecContext, frame))
  71.                 {
  72.                         void *audio_data = frame->data[0];
  73.                         snd_pcm_sframes_t frames = frame->nb_samples;
  74.                         while (frames > 0)
  75.                         {
  76.                                 snd_pcm_sframes_t written = snd_pcm_writei(handle, audio_data, frames);
  77.                                 if (written < 0) {
  78.                                         break;
  79.                                 } else {
  80.                                         frames -= written;
  81.                                         audio_data += written * av_get_bytes_per_sample(frame->format) * formatContext->streams[streamId]->codecpar->ch_layout.nb_channels;
  82.                                 }
  83.                                 if (written < 0 && written != -EAGAIN)
  84.                                         goto end_playback;
  85.                         }
  86.                         int16_t *samples = (int16_t *)frame->data[0];
  87.                         for (int i = 0; i < frame->nb_samples; i++)
  88.                                 printf("%ud", samples[i]);
  89.                         puts("");
  90.                 }
  91.  
  92.                 av_packet_unref(packet);
  93.         }
  94.  
  95. end_playback:
  96.         av_packet_free(&packet);
  97.         av_frame_free(&frame);
  98.         avcodec_free_context(&codecContext);
  99.         avformat_close_input(&formatContext);
  100.         snd_pcm_close(handle);
  101.  
  102.         return 0;
  103. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at