2017-05-31 55 views
1

我正在使用FFMPEG庫修剪視頻文件。我這樣做都是作爲remux,沒有編碼或解碼。由remux FFMPEG修剪不寫關鍵幀

修剪當前可以與音頻一起正常工作,但修剪後的視頻數據顯示爲純色,並且像素的小方塊發生了變化。我相信這是因爲我沒有捕捉/編寫關鍵幀。這是我的理解,av_seek_frame將尋求一個關鍵幀,這似乎並非如此..

如果需要,我可以解碼,然後重新編碼只是我查找後第一個視頻幀?與重新編碼每一幀相比,這可能是更多的代碼,但速度是這裏的主要問題,而不是複雜性。

謝謝你的幫助。此外,如果我誤解了某些與視頻文件有關的問題,我對此表示歉意,但我仍然對此不太瞭解。

實施例輸出幀: enter image description here

代碼,適於從設置有FFMPEG的REMUX例如:

const char *out_filename = "aaa.mp4"; 

FILE *fp = fdopen(fd, "r"); 
fseek(fp, 0, SEEK_SET); 

if (fp) { 

    // Build an ffmpeg file 
    char path[512]; 
    sprintf(path, "pipe:%d", fileno(fp)); 

    // Turn on verbosity 
    av_log_set_level(AV_LOG_DEBUG); 
    av_log_set_callback(avLogCallback); 


    av_register_all(); 
    avcodec_register_all(); 


    AVOutputFormat *ofmt = NULL; 
    AVFormatContext *ifmt_ctx = avformat_alloc_context(), *ofmt_ctx = NULL; 
    AVPacket pkt; 
    int ret, i; 


    if ((ret = avformat_open_input(&ifmt_ctx, path, av_find_input_format("mp4"), NULL)) < 0) { 
     LOG("Could not open input file '%s'", path); 
     goto end; 
    } 

    if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) { 
     LOG("Failed to retrieve input stream information", ""); 
     goto end; 
    } 


    avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename); 
    if (!ofmt_ctx) { 
     LOG("Could not create output context\n"); 
     ret = AVERROR_UNKNOWN; 
     goto end; 
    } 

    ofmt = ofmt_ctx->oformat; 


    for (i = 0; i < ifmt_ctx->nb_streams; i++) { 
     AVStream *in_stream = ifmt_ctx->streams[i]; 
     AVStream *out_stream = avformat_new_stream(ofmt_ctx, NULL); 

     if (!out_stream) { 
      LOG("Failed allocating output stream\n"); 
      goto end; 
     } 

     ret = avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar); 
     if (ret < 0) { 
      LOG("Failed to copy context from input to output stream codec context\n"); 
      goto end; 
     } 
     out_stream->codecpar->codec_tag = 0; 
    } 

    if (!(ofmt->flags & AVFMT_NOFILE)) { 
     ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE); 
     if (ret < 0) { 
      LOG("Could not open output file '%s'", out_filename); 
      goto end; 
     } 
    } 

    ret = avformat_write_header(ofmt_ctx, NULL); 
    if (ret < 0) { 
     LOG("Error occurred when writing headers\n"); 
     goto end; 
    } 

    ret = av_seek_frame(ifmt_ctx, -1, from_seconds * AV_TIME_BASE, AVSEEK_FLAG_ANY); 
    if (ret < 0) { 
     LOG("Error seek\n"); 
     goto end; 
    } 

    int64_t *dts_start_from; 
    int64_t *pts_start_from; 
    dts_start_from = (int64_t *) malloc(sizeof(int64_t) * ifmt_ctx->nb_streams); 
    memset(dts_start_from, 0, sizeof(int64_t) * ifmt_ctx->nb_streams); 
    pts_start_from = (int64_t *) malloc(sizeof(int64_t) * ifmt_ctx->nb_streams); 
    memset(pts_start_from, 0, sizeof(int64_t) * ifmt_ctx->nb_streams); 

    while (1) { 
     AVStream *in_stream, *out_stream; 

     ret = av_read_frame(ifmt_ctx, &pkt); 
     LOG("while %d", ret); 
     LOG("Packet size: %d", pkt.size); 
     LOG("Packet stream: %d", pkt.stream_index); 
     if (ret < 0) 
      break; 

     in_stream = ifmt_ctx->streams[pkt.stream_index]; 
     out_stream = ofmt_ctx->streams[pkt.stream_index]; 

     if (av_q2d(in_stream->time_base) * pkt.pts > end_seconds) { 
      av_packet_unref(&pkt); 
      break; 
     } 

     if (dts_start_from[pkt.stream_index] == 0) { 
      dts_start_from[pkt.stream_index] = pkt.dts; 
      printf("dts_start_from: %s\n", av_ts_make_string((char[AV_TS_MAX_STRING_SIZE]){0},dts_start_from[pkt.stream_index])); 
     } 
     if (pts_start_from[pkt.stream_index] == 0) { 
      pts_start_from[pkt.stream_index] = pkt.pts; 
      printf("pts_start_from: %s\n", av_ts_make_string((char[AV_TS_MAX_STRING_SIZE]){0},pts_start_from[pkt.stream_index])); 
     } 

     /* copy packet */ 
     pkt.pts = ::av_rescale_q_rnd(pkt.pts - pts_start_from[pkt.stream_index], in_stream->time_base, out_stream->time_base, (AVRounding) (AV_ROUND_NEAR_INF | 
                                      AV_ROUND_PASS_MINMAX)); 
     pkt.dts = ::av_rescale_q_rnd(pkt.dts - dts_start_from[pkt.stream_index], in_stream->time_base, out_stream->time_base, (AVRounding) (AV_ROUND_NEAR_INF | 
                                      AV_ROUND_PASS_MINMAX)); 
     if (pkt.pts < 0) { 
      pkt.pts = 0; 
     } 
     if (pkt.dts < 0) { 
      pkt.dts = 0; 
     } 
     pkt.duration = (int) av_rescale_q((int64_t) pkt.duration, in_stream->time_base, out_stream->time_base); 
     pkt.pos = -1; 
     printf("\n"); 

     ret = av_interleaved_write_frame(ofmt_ctx, &pkt); 
     if (ret < 0) { 
      LOG("Error muxing packet\n"); 
      break; 
     } 
     av_packet_unref(&pkt); 
    } 
    free(dts_start_from); 
    free(pts_start_from); 

    av_write_trailer(ofmt_ctx); 

    end: 
    LOG("END"); 

    avformat_close_input(&ifmt_ctx); 

    // Close output 
    if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE)) 
     avio_closep(&ofmt_ctx->pb); 
    avformat_free_context(ofmt_ctx); 

    if (ret < 0 && ret != AVERROR_EOF) { 
     LOG("-- Error occurred: %s\n", av_err2str(ret)); 
     return 1; 
    } 
} 

回答

1

對於幀粒度remuxing(不只是開始在一個關鍵幀),至少是您需要對關鍵幀進行編碼以替換您開始使用的非關鍵幀,但是如果該流使用B幀(對於使用h264/etc的mp4通用),那麼它可能不夠用。最安全的方法是重新編碼你想要開始的幀,直到(但不包括)下一個關鍵幀,然後簡單重複 - 從現有關鍵幀開始複製所有內容。

+0

所以爲了確認我明白,在av_seek_frame調用之後,我應該重新編碼所有收到的視頻數據包,直到我得到一個關鍵幀,此時我可以恢復remux複製?謝謝。 – DweebsUnited

+0

我不知道'av_seek_frame'如何在當前的ffmpeg上工作,所以我不想給你錯誤的建議。如果它通過尋找上一個關鍵幀並解碼到輸出丟失的期望幀,然後我認爲你是對的。如果沒有,您可能需要手動回溯到上一個關鍵幀並丟棄已解碼的幀,直到找到所需的幀。 –

+0

所以,事實證明,這個視頻文件只是沒有任何IDR幀,這意味着我正在remuxing正確。在包含IDR幀的視頻上運行它完美無缺。 TIL擴展我的測試集,而不相信一個樣本。感謝您的幫助。 – DweebsUnited