版本比较
在desmos中播放视频的教程
正文
增加 46 行删除 48 行
61 行未改动62636465666667686969707172737475757677787980818283787980818283848586858687888990919293909495929697949899969798100101102103104105106107108109110111112101102113114115105116117118119120121122123112124125126127128115129130117131119132133121134135136137138139140128129130131141142143144133134145146147148149138150151140141152153154155156157154 行未改动全部展开31231331431530430530630730830931031131231331431531631731631731831976 行未改动61 行未改动 if (argc < 3) { std::cerr << "用法: " << argv[0] << " <视频路径> <输出文件路径>" << std::endl; return -1; }
std::string videoPath = argv[1]; std::string outputPath = argv[2];
cv::VideoCapture cap(videoPath); if (!cap.isOpened()) { std::cerr << "无法打开视频文件" << std::endl; return -1; }
double originalFps = cap.get(cv::CAP_PROP_FPS); int targetFps = 24; // 计算帧间隔:每 originalFps/targetFps 帧取一帧 int frameInterval = static_cast<int>(std::round(originalFps / targetFps)); if (frameInterval < 1) frameInterval = 1; const int width = 32; const int height = 32; int totalFrames = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_COUNT)); // 计算目标帧数(四舍五入) int targetFrames = static_cast<int>(totalFrames * targetFps / originalFps + 0.5); std::cout << "原始帧率: " << originalFps << ", 总帧数: " << totalFrames << ", 目标帧数: " << targetFrames << std::endl;
const int width = 64; const int height = 36; const int pixelsPerFrame = width * height; // 1024 // 用于累积数据的向量(可选预分配,但视频总帧数未知)
std::vector<uint8_t> allR; std::vector<uint8_t> allG; std::vector<uint8_t> allB; allR.reserve(targetFrames * pixelsPerFrame); allG.reserve(targetFrames * pixelsPerFrame); allB.reserve(targetFrames * pixelsPerFrame);
cv::Mat frame, resized, rgbFrame; int frameCount = 0; int savedCount = 0; int prevJ = -1;
std::cout << "开始处理视频..." << std::endl; while (cap.read(frame)) { if (frameCount % frameInterval == 0) {
for (int i = 0; i < totalFrames; ++i) { if (!cap.read(frame)) { std::cerr << "警告:提前结束于帧 " << i << std::endl; break; }
// 计算当前原始帧对应的目标帧索引 int j = static_cast<int>(static_cast<long long>(i) * targetFrames / totalFrames); if (j != prevJ) { // 需要保存这一帧 cv::resize(frame, resized, cv::Size(width, height)); cv::cvtColor(resized, rgbFrame, cv::COLOR_BGR2RGB); // 将每个通道展平并添加到向量
std::vector<cv::Mat> channels(3); cv::split(rgbFrame, channels);
// 将通道数据追加到向量 allR.insert(allR.end(), channels[0].reshape(1, 1).ptr<uint8_t>(0), channels[0].reshape(1, 1).ptr<uint8_t>(0) + pixelsPerFrame); allG.insert(allG.end(), channels[1].reshape(1, 1).ptr<uint8_t>(0), channels[1].reshape(1, 1).ptr<uint8_t>(0) + pixelsPerFrame); allB.insert(allB.end(), channels[2].reshape(1, 1).ptr<uint8_t>(0), channels[2].reshape(1, 1).ptr<uint8_t>(0) + pixelsPerFrame);
savedCount++; prevJ = j;
if (savedCount % 100 == 0) std::cout << "已处理 " << savedCount << " 帧..." << std::endl; std::cout << "已保存 " << savedCount << " 帧..." << std::endl; } frameCount++; }
cap.release();
// 写入二进制文件 std::ofstream out(outputPath, std::ios::binary); if (!out.is_open()) { std::cerr << "无法创建输出文件" << std::endl; return -1; } // 写入帧数、每帧像素数,便于 Python 解析 int totalFrames = savedCount; out.write(reinterpret_cast<const char*>(&totalFrames), sizeof(totalFrames));
// 写入帧数和每帧像素数 out.write(reinterpret_cast<const char*>(&savedCount), sizeof(savedCount)); out.write(reinterpret_cast<const char*>(&pixelsPerFrame), sizeof(pixelsPerFrame)); // 写入三个通道的数据(连续存储)
// 写入三个通道的数据 out.write(reinterpret_cast<const char*>(allR.data()), allR.size() * sizeof(uint8_t)); out.write(reinterpret_cast<const char*>(allG.data()), allG.size() * sizeof(uint8_t)); out.write(reinterpret_cast<const char*>(allB.data()), allB.size() * sizeof(uint8_t));
out.close(); std::cout << "处理完成,共 " << totalFrames << " 帧,数据已保存至 " << outputPath << std::endl;
std::cout << "处理完成,实际保存 " << savedCount << " 帧,数据已保存至 " << outputPath << std::endl; return 0;}```下面两个是python的154 行未改动全部展开
total_time = time.time() - start_time print(f"\n截图完成,保存至 {SCREENSHOTS_DIR},总耗时 {total_time:.2f} 秒,平均 {total_time/frame_count*1000:.1f} ms/帧")
# 合成视频(可选) print("正在合成视频...") shot_paths = sorted(SCREENSHOTS_DIR.glob("*.png")) if shot_paths: first = cv2.imread(str(shot_paths[0])) h, w = first.shape[:2] fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter(str(OUTPUT_DIR / "output_video.mp4"), fourcc, FPS, (w, h)) for path in shot_paths: img = cv2.imread(str(path)) out.write(img) out.release() print(f"视频已保存至 {OUTPUT_DIR / 'output_video.mp4'}")
if __name__ == "__main__": main()````video.py`:76 行未改动