我对此有一些想法。然而,我在iOS上的exp已经非常有限,不确定我的想法是否是最好的方式:
据我所知,通常不可能在iOS上运行cmd工具。也许你必须编写一些链接到ffmpeg库的代码。
这里需要做的所有工作:
- 打开输入文件和init一些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
}
}
}