-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecode_video.cpp
More file actions
61 lines (51 loc) · 1.43 KB
/
Copy pathdecode_video.cpp
File metadata and controls
61 lines (51 loc) · 1.43 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
#include <iostream>
#include "ffmpeg-cpp.hpp"
#include "Utils.hpp"
#define NUMBER_OF_FRAME 100
int main(int argc, char **argv)
{
if (argc < 2)
{
std::cout << "Usage: ./decode_video <filename>" << std::endl;
return 1;
}
FFmpegFormatCtx::Ptr format_ctx = std::make_shared<FFmpegFormatCtx>();
FFmpegCodecCtx::Ptr codec_ctx = std::make_shared<FFmpegCodecCtx>();
FFmpegPacket::Ptr packet;
if (format_ctx->Open(argv[1]) == false)
{
std::cout << "File open error" << std::endl;
return 1;
}
auto video_streams_idx = format_ctx->FindVideoStreamIDX();
if (video_streams_idx.empty())
{
std::cout << "No video stream in file" << std::endl;
return 1;
}
auto codec_id = format_ctx->GetCodecID(video_streams_idx[0]);
codec_ctx->Open(codec_id);
int frame_counter = 0;
while (true)
{
if (format_ctx->ReadPacket(packet) != 0)
{
std::cout << "Error reading packet" << std::endl;
return 1;
}
if (packet->GetStreamIdx() == video_streams_idx[0])
{
auto frame = codec_ctx->Decode(packet);
if (frame)
{
if (frame_counter == NUMBER_OF_FRAME)
{
SaveAvFrame(frame, "decoded_frame.yuv");
break;
}
frame_counter++;
}
}
}
return 0;
}