当前位置:   article > 正文

【FFmpeg学习】视频变慢处理

【FFmpeg学习】视频变慢处理

视频慢动作处理是个比较常用的操作,可以在播放的时候处理,这里我们考虑把视频修改为慢动作,使用ffmpeg命令,可以这样

ffmpeg -i test.mp4 -vf "setpts=5*PTS" -an test_slow3.mp4

这里把视频放慢了5倍,生成的文件大小也变大了几倍。

怎么用程序来实现呢,参考一下GPT给出的code,

  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <cstring>
  5. extern "C" {
  6. #include <libavformat/avformat.h>
  7. #include <libavcodec/avcodec.h>
  8. #include <libavutil/opt.h>
  9. }
  10. // 慢速播放的速度因子
  11. const double SLOWDOWN_FACTOR = 2.0;
  12. int main(int argc, char* argv[]) {
  13. if (argc < 3) {
  14. std::cout << "Usage: " << argv[0] << " input_file output_file" << std::endl;
  15. return 1;
  16. }
  17. const std::string inputFileName = argv[1];
  18. const std::string outputFileName = argv[2];
  19. av_register_all();
  20. AVFormatContext* inputFormatContext = nullptr;
  21. if (avformat_open_input(&inputFormatContext, inputFileName.c_str(), nullptr, nullptr) != 0) {
  22. std::cerr << "Failed to open input file: " << inputFileName << std::endl;
  23. return 1;
  24. }
  25. if (avformat_find_stream_info(inputFormatContext, nullptr) < 0) {
  26. std::cerr << "Failed to retrieve input stream information" << std::endl;
  27. avformat_close_input(&inputFormatContext);
  28. return 1;
  29. }
  30. AVFormatContext* outputFormatContext = nullptr;
  31. if (avformat_alloc_output_context2(&outputFormatContext, nullptr, nullptr, outputFileName.c_str()) < 0) {
  32. std::cerr << "Failed to allocate output format context" << std::endl;
  33. avformat_close_input(&inputFormatContext);
  34. return 1;
  35. }
  36. for (unsigned int i = 0; i < inputFormatContext->nb_streams; ++i) {
  37. AVStream* inputStream = inputFormatContext->streams[i];
  38. AVStream* outputStream = avformat_new_stream(outputFormatContext, nullptr);
  39. if (!outputStream) {
  40. std::cerr << "Failed to allocate output stream" << std::endl;
  41. avformat_close_input(&inputFormatContext);
  42. avformat_free_context(outputFormatContext);
  43. return 1;
  44. }
  45. if (avcodec_parameters_copy(outputStream->codecpar, inputStream->codecpar) < 0) {
  46. std::cerr << "Failed to copy codec parameters" << std::endl;
  47. avformat_close_input(&inputFormatContext);
  48. avformat_free_context(outputFormatContext);
  49. return 1;
  50. }
  51. outputStream->time_base = inputStream->time_base;
  52. }
  53. if (!(outputFormatContext->oformat->flags & AVFMT_NOFILE)) {
  54. if (avio_open(&outputFormatContext->pb, outputFileName.c_str(), AVIO_FLAG_WRITE) < 0) {
  55. std::cerr << "Failed to open output file: " << outputFileName << std::endl;
  56. avformat_close_input(&inputFormatContext);
  57. avformat_free_context(outputFormatContext);
  58. return 1;
  59. }
  60. }
  61. if (avformat_write_header(outputFormatContext, nullptr) < 0) {
  62. std::cerr << "Failed to write output file header" << std::endl;
  63. avformat_close_input(&inputFormatContext);
  64. avformat_free_context(outputFormatContext);
  65. return 1;
  66. }
  67. AVPacket packet;
  68. while (av_read_frame(inputFormatContext, &packet) >= 0) {
  69. AVStream* outputStream = outputFormatContext->streams[packet.stream_index];
  70. // 慢速播放的时间戳调整
  71. packet.pts *= static_cast<int64_t>(SLOWDOWN_FACTOR);
  72. packet.dts *= static_cast<int64_t>(SLOWDOWN_FACTOR);
  73. packet.duration = static_cast<int>(packet.duration * SLOWDOWN_FACTOR);
  74. av_interleaved_write_frame(outputFormatContext, &packet);
  75. av_packet_unref(&packet);
  76. }
  77. av_write_trailer(outputFormatContext);
  78. avformat_close_input(&inputFormatContext);
  79. avformat_free_context(outputFormatContext);
  80. return 0;
  81. }

这个代码执行后并没有实现变慢,参考一下,进行修改后可以实现慢动作处理,如下

  1. int slow() {
  2. std::string filename = "test.mp4"; // 输入MP4文件名
  3. std::string outputFilename = "test_slow.mp4"; // 输出图片文件名
  4. AVFormatContext* ofmt_ctx = nullptr;
  5. avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, outputFilename.c_str());
  6. const AVOutputFormat* ofmt = ofmt_ctx->oformat;
  7. AVFormatContext* formatContext = nullptr;
  8. if (avformat_open_input(&formatContext, filename.c_str(), nullptr, nullptr) != 0) {
  9. std::cerr << "Error opening input file" << std::endl;
  10. return -1;
  11. }
  12. if (avformat_find_stream_info(formatContext, nullptr) < 0) {
  13. std::cerr << "Error finding stream information" << std::endl;
  14. avformat_close_input(&formatContext);
  15. return -1;
  16. }
  17. const AVCodec* codec = nullptr;
  18. int videoStreamIndex = -1;
  19. for (unsigned int i = 0; i < formatContext->nb_streams; i++) {
  20. if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  21. videoStreamIndex = i;
  22. codec = avcodec_find_decoder(formatContext->streams[i]->codecpar->codec_id);
  23. //
  24. AVStream* out_stream = avformat_new_stream(ofmt_ctx, NULL);
  25. avcodec_parameters_copy(out_stream->codecpar, formatContext->streams[i]->codecpar);
  26. out_stream->codecpar->codec_tag = 0;
  27. break;
  28. }
  29. }
  30. avio_open(&ofmt_ctx->pb, outputFilename.c_str(), AVIO_FLAG_WRITE);
  31. avformat_write_header(ofmt_ctx, NULL);
  32. AVPacket packet;
  33. av_init_packet(&packet);
  34. // 计算目标时间戳
  35. int64_t targetTimestamp = targetSecond * AV_TIME_BASE;
  36. // 查找目标时间戳所对应的帧
  37. AVFrame* frame = av_frame_alloc();
  38. bool foundTargetFrame = false;
  39. int count = 0;
  40. while (av_read_frame(formatContext, &packet) >= 0) {
  41. if (packet.stream_index == videoStreamIndex) {
  42. AVStream* inputStream = formatContext->streams[packet.stream_index];
  43. AVStream* outputStream = ofmt_ctx->streams[packet.stream_index];
  44. // 计算新的时间戳
  45. cout << "1, pts=" << packet.pts << endl;
  46. packet.pts = av_rescale_q_rnd(packet.pts, inputStream->time_base, outputStream->time_base, AV_ROUND_NEAR_INF);
  47. packet.dts = av_rescale_q_rnd(packet.dts, inputStream->time_base, outputStream->time_base, AV_ROUND_NEAR_INF);
  48. packet.duration = av_rescale_q(packet.duration, inputStream->time_base, outputStream->time_base);
  49. packet.pos = -1;
  50. cout << "2, pts=" << packet.pts << endl;
  51. // 慢速播放的时间戳调整
  52. packet.pts *= 5;
  53. packet.dts *= 5;
  54. packet.duration *= 5;
  55. // av_log(NULL, AV_LOG_INFO, "...av_write_frame(ofmt_ctx, packet);\n");
  56. int ret = av_write_frame(ofmt_ctx, &packet);
  57. if (ret < 0) {
  58. std::cerr << "Error av_write_frame" << std::endl;
  59. }
  60. count++;
  61. }
  62. av_packet_unref(&packet);
  63. }
  64. av_write_trailer(ofmt_ctx);
  65. return 1;
  66. }

通过pts的修改来实现

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/92416
推荐阅读
相关标签
  

闽ICP备14008679号