[英]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.