xcal
基于 C++23 的现代图形渲染引擎
载入中...
搜索中...
未找到
avcode.cc
浏览该文件的文档.
1#include <cmath>
2#include <iostream>
3#include <vector>
5
6using namespace xcal::render::codec;
7// 生成简单的颜色渐变测试帧
8std::vector<char> generateGradientFrame(int width, int height, int frame_num) {
9 std::vector<char> frame_data(width * height * 4);
10
11 for (int y = 0; y < height; y++) {
12 for (int x = 0; x < width; x++) {
13 int index = (y * width + x) * 4;
14
15 // 创建颜色渐变效果
16 float time = frame_num / 30.0f; // 每30帧一个周期
17
18 // RGB颜色计算
19 frame_data[index] =
20 static_cast<char>(128 + 127 * sin(x * 0.1f + time)); // R
21 frame_data[index + 1] =
22 static_cast<char>(128 + 127 * sin(y * 0.1f + time)); // G
23 frame_data[index + 2] = static_cast<char>(
24 128 + 127 * sin((x + y) * 0.05f + time)); // B
25 frame_data[index + 3] = 255u; // Alpha通道
26 }
27 }
28
29 return frame_data;
30}
31
32int main() {
33 const int width = 1280;
34 const int height = 720;
35 const int frame_rate = 30;
36 const int total_frames = 300; // 10秒视频
37
38 std::cout << "Creating gradient animation video..." << std::endl;
39
40 try {
41 FfmpegCodec encoder("gradient_video.mp4", width, height, frame_rate);
42
43 for (int i = 0; i < total_frames; i++) {
44 auto frame_data = generateGradientFrame(width, height, i);
45
46 if (!encoder.append_frame(frame_data)) {
47 std::cerr << "Failed to append frame " << i << std::endl;
48 break;
49 }
50
51 // 显示进度
52 if (i % 30 == 0) {
53 float progress = (i * 100.0f) / total_frames;
54 std::cout << "Progress: " << progress << "% (" << i << "/"
55 << total_frames << " frames)" << std::endl;
56 }
57 }
58
59 if (!encoder.finish()) {
60 std::cerr << "Failed to finish encoding" << std::endl;
61 return 1;
62 }
63
64 std::cout << "Video created successfully: gradient_video.mp4"
65 << std::endl;
66 std::cout << "Total frames encoded: " << encoder.encoded_frames()
67 << std::endl;
68
69 } catch (const std::exception& e) {
70 std::cerr << "Error: " << e.what() << std::endl;
71 return 1;
72 }
73
74 return 0;
75}
std::vector< char > generateGradientFrame(int width, int height, int frame_num)
Definition avcode.cc:8
int main()
Definition avcode.cc:32
bool_t append_frame(const std::vector< char > &rgba_data) override