forked from Raveler/ffmpeg-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode_audio.cpp
More file actions
96 lines (77 loc) · 2.24 KB
/
decode_audio.cpp
File metadata and controls
96 lines (77 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include "ffmpegcpp.h"
using namespace std;
using namespace ffmpegcpp;
class RawAudioFileSink : public AudioFrameSink, public FrameWriter
{
public:
RawAudioFileSink(const char* fileName)
{
file = fopen(fileName, "wb");
}
FrameSinkStream* CreateStream()
{
stream = new FrameSinkStream(this, 0);
return stream;
}
virtual void WriteFrame(int streamIndex, AVFrame* frame, StreamData* streamData)
{
// Just write out the samples channel by channel to a file.
int data_size = av_get_bytes_per_sample((AVSampleFormat)frame->format);
for (int i = 0; i < frame->nb_samples; i++)
{
for (int ch = 0; ch < frame->channels; ch++)
{
fwrite(frame->data[ch] + data_size * i, 1, data_size, file);
}
}
}
virtual void Close(int streamIndex)
{
fclose(file);
delete stream;
}
virtual bool IsPrimed()
{
// Return whether we have all information we need to start writing out data.
// Since we don't really need any data in this use case, we are always ready.
// A container might only be primed once it received at least one frame from each source
// it will be muxing together (see Muxer.cpp for how this would work then).
return true;
}
private:
FILE* file;
FrameSinkStream* stream;
};
int main()
{
// This example will decode an audio stream from a container and output it as raw audio data in the chosen format.
try
{
// Load this container file so we can extract audio from it.
Demuxer* demuxer = new Demuxer("samples/big_buck_bunny.mp4");
// Create a file sink that will just output the raw audio data.
RawAudioFileSink* fileSink = new RawAudioFileSink("rawaudio");
// tie the file sink to the best audio stream in the input container.
demuxer->DecodeBestAudioStream(fileSink);
// Prepare the output pipeline. This will push a small amount of frames to the file sink until it IsPrimed returns true.
demuxer->PreparePipeline();
// Push all the remaining frames through.
while (!demuxer->IsDone())
{
demuxer->Step();
}
// done
delete demuxer;
delete fileSink;
}
catch (FFmpegException e)
{
cerr << "Exception caught!" << endl;
cerr << e.what() << endl;
throw e;
}
cout << "Decoding complete!" << endl;
cout << "Press any key to continue..." << endl;
getchar();
}