-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathdecode_video.cpp
More file actions
96 lines (76 loc) · 2.36 KB
/
decode_video.cpp
File metadata and controls
96 lines (76 loc) · 2.36 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 <memory>
#include "ffmpegcpp.h"
class PGMFileSink : public ffmpegcpp::VideoFrameSink
{
public:
PGMFileSink()
{
}
void WriteFrame(AVFrame* frame, AVRational* timeBase) override
{
++frameNumber;
printf("saving frame %3d\n", frameNumber);
fflush(stdout);
// write the first channel's color data to a PGM file.
// This raw image file can be opened with most image editing programs.
snprintf(fileNameBuffer, sizeof(fileNameBuffer), "pgm-%d.pgm", frameNumber);
pgm_save(frame->data[0], frame->linesize[0],
frame->width, frame->height, fileNameBuffer);
}
void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
char *filename)
{
FILE *f;
int i;
f = fopen(filename, "w");
fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
for (i = 0; i < ysize; i++)
fwrite(buf + i * wrap, 1, xsize, f);
fclose(f);
}
void Close() override
{
// nothing to do here.
}
bool IsPrimed() const override
{
// 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:
char fileNameBuffer[1024];
int frameNumber = 0;
};
int main()
{
// This example will decode a video stream from a container and output it as raw image data, one image per frame.
try
{
// Load this container file so we can extract video from it.
ffmpegcpp::Demuxer demuxer("samples/big_buck_bunny.mp4");
// Create a file sink that will just output the raw frame data in one PGM file per frame.
auto fileSink = std::make_unique<PGMFileSink>();
// tie the file sink to the best video stream in the input container.
demuxer.DecodeBestVideoStream(fileSink.get());
// 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();
}
}
catch (ffmpegcpp::FFmpegException e)
{
std::cerr << "Exception caught!" << '\n';
std::cerr << e.what() << '\n';
throw e;
}
std::cout << "Decoding complete!" << '\n';
std::cout << "Press any key to continue..." << '\n';
getchar();
}