我正在使用 FFMPEG 库来修剪视频文件。我将这一切作为 remux 来完成,没有编码或解码。
修剪目前可以正确处理音频,但修剪后的视频数据显示为纯色,小方 block 的像素会发生变化。我相信这是因为我没有捕捉/写入关键帧。据我了解,av_seek_frame将寻找关键帧,但情况似乎并非如此。
如果需要,我可以解码然后重新编码查找后读取的第一个视频帧吗?这可能比重新编码每一帧需要更多的代码,但速度是这里的主要问题,而不是复杂性。
感谢您的帮助。如果我误解了与视频文件有关的内容,我也深表歉意,因为我对此还很陌生。
代码,改编自 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;
}
}
最佳答案
对于帧粒度重新混合(不仅仅是从关键帧开始),至少您需要编码一个关键帧来替换您开始的非关键帧,但如果流使用 B 帧(常见于mp4 和 h264/等)那么它可能还不够。最安全的方法是重新编码从您想要开始的帧到(但不包括)下一个关键帧的所有内容,然后简单地重新复制从现有关键帧开始的所有内容。
关于c - remux 的 FFMPEG 修剪不会写入关键帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44291321/