MCPcopy Create free account
hub / github.com/FFmpeg/FFmpeg / audio_encode_example

Function audio_encode_example

libavcodec/api-example.c:48–111  ·  view source on GitHub ↗

* Audio encoding example */

Source from the content-addressed store, hash-verified

46 * Audio encoding example
47 */
48static void audio_encode_example(const char *filename)
49{
50 AVCodec *codec;
51 AVCodecContext *c= NULL;
52 int frame_size, i, j, out_size, outbuf_size;
53 FILE *f;
54 short *samples;
55 float t, tincr;
56 uint8_t *outbuf;
57
58 printf("Audio encoding\n");
59
60 /* find the MP2 encoder */
61 codec = avcodec_find_encoder(CODEC_ID_MP2);
62 if (!codec) {
63 fprintf(stderr, "codec not found\n");
64 exit(1);
65 }
66
67 c= avcodec_alloc_context();
68
69 /* put sample parameters */
70 c->bit_rate = 64000;
71 c->sample_rate = 44100;
72 c->channels = 2;
73
74 /* open it */
75 if (avcodec_open(c, codec) < 0) {
76 fprintf(stderr, "could not open codec\n");
77 exit(1);
78 }
79
80 /* the codec gives us the frame size, in samples */
81 frame_size = c->frame_size;
82 samples = malloc(frame_size * 2 * c->channels);
83 outbuf_size = 10000;
84 outbuf = malloc(outbuf_size);
85
86 f = fopen(filename, "wb");
87 if (!f) {
88 fprintf(stderr, "could not open %s\n", filename);
89 exit(1);
90 }
91
92 /* encode a single tone sound */
93 t = 0;
94 tincr = 2 * M_PI * 440.0 / c->sample_rate;
95 for(i=0;i<200;i++) {
96 for(j=0;j<frame_size;j++) {
97 samples[2*j] = (int)(sin(t) * 10000);
98 samples[2*j+1] = samples[2*j];
99 t += tincr;
100 }
101 /* encode the samples */
102 out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
103 fwrite(outbuf, 1, out_size, f);
104 }
105 fclose(f);

Callers 1

mainFunction · 0.85

Calls 6

avcodec_find_encoderFunction · 0.85
avcodec_alloc_contextFunction · 0.85
avcodec_openFunction · 0.85
avcodec_encode_audioFunction · 0.85
avcodec_closeFunction · 0.85
av_freeFunction · 0.85

Tested by

no test coverage detected