iOS ffmpeg 如何运行命令来修剪远程 url 视频?

标签 ios xcode video ffmpeg avfoundation

我最初使用 AVFoundation库来修剪视频,但它有一个限制,它不能对远程 URL 执行此操作,并且仅适用于本地 URL。

所以经过进一步研究我发现ffmpeg可以包含在 Xcode 中的库iOS 项目。
我已经测试了以下命令来在命令行上修剪远程视频:

ffmpeg -y -ss 00:00:01.000 -i "http://i.imgur.com/gQghRNd.mp4" -t 00:00:02.000 -async 1 cut.mp4

这将修剪 .mp4从 1 秒到 3 秒标记。这可以通过我的 Mac 上的命令行完美运行。

我已经成功编译并包含 ffmpeg库到 xcode 项目,但不知道如何进一步进行。

现在我想弄清楚如何使用 ffmpeg 在 iOS 应用程序上运行此命令。图书馆。我怎样才能做到这一点?

如果您能指出一些有用的方向,我将不胜感激!如果我可以使用您的解决方案解决它,我将奖励赏金(在 2 天内给我选项)。

最佳答案

我对此有一些想法。但是,我在 iOS 上的 exp 非常有限,不确定我的想法是否是最好的方法:

据我所知,一般在 iOS 上运行 cmd 工具是不可能的。也许您必须编写一些链接到 ffmpeg 库的代码。

以下是需要做的所有工作:

  • 打开输入文件并初始化一些 ffmpeg 上下文。
  • 获取视频流并寻找您想要的时间戳。这可能很复杂。见 ffmpeg tutorial寻求帮助,或查看 this精确寻找和处理麻烦的关键帧。
  • 解码一些帧。直到帧匹配结束时间戳。
  • 同时,将帧编码为一个新文件作为输出。

  • ffmpeg 源代码中的示例非常适合学习如何执行此操作。

    一些可能有用的代码:
    av_register_all();
    avformat_network_init();
    
    AVFormatContext* fmt_ctx;
    avformat_open_input(&fmt_ctx, "http://i.imgur.com/gQghRNd.mp4", NULL, NULL);
    
    avformat_find_stream_info(fmt_ctx, NULL);
    
    AVCodec* dec;
    int video_stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);
    AVCodecContext* dec_ctx = avcodec_alloc_context3(NULL);
    avcodec_parameters_to_context(dec_ctx, fmt_ctx->streams[video_stream_index]->codecpar)
    // If there is audio you need, it should be decoded/encoded too.
    
    avcodec_open2(dec_ctx, dec, NULL);
    // decode initiation done
    
    av_seek_frame(fmt_ctx, video_stream_index, frame_target, AVSEEK_FLAG_FRAME);
    // or av_seek_frame(fmt_ctx, video_stream_index, timestamp_target, AVSEEK_FLAG_ANY)
    // and for most time, maybe you need AVSEEK_FLAG_BACKWARD, and skipping some following frames too.
    
    AVPacket packet;
    AVFrame* frame = av_frame_alloc();
    
    int got_frame, frame_decoded;
    while (av_read_frame(fmt_ctx, &packet) >= 0 && frame_decoded < second_needed * fps) {
        if (packet.stream_index == video_stream_index) {
            got_frame = 0;
            ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, &packet);
            // This is old ffmpeg decode/encode API, will be deprecated later, but still working now.
            if (got_frame) {
                // encode frame here
            }
        }
    }
    

    关于iOS ffmpeg 如何运行命令来修剪远程 url 视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38536830/

    相关文章:

    ios - 修复 90164/90046 错误后错误 ITMS-4238 冗余二进制文件上传

    ios - 如何在 iOS(自定义)中捕获视频?

    python - 如何打开两个omxplayer实例

    html - HTML5将YouTube视频嵌入到混合应用中,而无需连接

    Xcode 项目图标文件和 Organizer Archives 图稿

    objective-c - UINavigationBar 和新的 iOS 5+ 外观 API - 如何提供两个背景图像?

    ios - 是否可以在 SceneKit 中关闭抗锯齿以使像素艺术图像清晰?

    iphone - 如何从 iPhone 中的某一天获取日期?

    Xcode模拟器不断下载东西

    ios - UITableView 无法正确地为在屏幕外重新排序的单元格设置动画