簡體   English   中英

iOS ffmpeg如何運行命令以修剪遠程URL視頻?

[英]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庫的代碼。

這是需要做的所有工作:

  1. 打開輸入文件並初始化一些ffmpeg上下文。
  2. 獲取視頻流並查找所需的時間戳。 這可能很復雜。 請參閱ffmpeg教程以獲取一些幫助,或檢查此內容以精確查找並處理麻煩的關鍵幀。
  3. 解碼一些幀。 直到幀匹配結束時間戳。
  4. 同時,使用上述方法,將幀編碼為一個新文件作為輸出。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM