我最初使用 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 源代码中的示例非常适合学习如何执行此操作。
一些可能有用的代码:
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/