You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

std::system调用ffmpeg转无脸jpg为mp4卡住,求解决方法或替代方案

你遇到的停滞问题和输入图片是否包含人脸无关,核心原因是ffmpeg默认开启覆盖确认交互:当输出路径下已存在同名mp4文件时,ffmpeg会阻塞等待用户输入y/n确认是否覆盖,你测试时含人脸的图片对应mp4大概率是首次生成无重名,所以正常执行,不含人脸的图片对应mp4已存在,就卡在了std::system调用的子进程交互环节。

解决方案1:直接修改ffmpeg命令修复阻塞问题

在ffmpeg命令开头添加-y参数,启用自动覆盖输出文件逻辑,无需用户交互,同时修复原代码的缓冲区溢出隐患,修改后的代码如下:

std::string Img2Vid(int i) {
  std::string input_path = FLAGS_input_video_path + "/" + std::to_string(i) + ".jpg";
  std::string output_path = FLAGS_input_video_path + "/" + std::to_string(i) + ".mp4";
  // 追加-y参数自动覆盖输出,删除冗余的固定长度char数组避免路径过长导致溢出
  std::string s = "ffmpeg -y -loop 1 -f image2 -t 0.07 -i " + input_path + " " + output_path;
  std::cout << output_path << std::endl;
  std::system(s.c_str());
  return output_path;
}

解决方案2:其他JPG转MP4实现方案

如果不想依赖外部ffmpeg进程调用,可以选择原生开发方案:

  • 使用FFmpeg原生API(libavcodec、libavformat)直接在程序内完成编码封装,无需启动子进程,稳定性更高,适合生产环境使用
  • 使用OpenCV的VideoWriter模块,代码实现更简单,支持跨平台:先读取单张JPG图片,按设定的时长写入对应帧数的mp4文件即可
  • Windows平台可选择Media Foundation接口、Linux平台可选择GStreamer完成转码需求

内容的提问来源于stack exchange,提问作者StevePark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 12:18:03