[英]iOS ffmpeg how to run a command to trim remote url video?
我最初使用AVFoundation
库来修剪视频,但是它有一个局限性,即它不能用于远程URL,而只能用于本地URL。
因此,在进一步研究之后,我发现ffmpeg
库可以包含在iOS的Xcode
项目中。 我已经测试了以下命令以在命令行上修剪远程视频:
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上的经验非常有限,不确定我的想法是否是最好的方法:
据我所知,通常无法在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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.