我正在使用FFMPEG库修剪视频文件。我把它作为一个remux,没有编码或解码。
剪裁当前可以正常使用音频,但剪裁后的视频数据显示为纯色,小像素的像素会发生变化。我相信这是因为我没有捕捉/写一个关键帧。据我所知,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;
}
}
答案 0 :(得分:1)
对于帧粒度重新加工(不只是从关键帧开始),至少你需要编码一个关键帧来替换你开始的非关键帧,但是如果流使用B帧(常见的是mp4与h264 /等。)然后它可能不够。最安全的方法是重新编码从您想要开始的帧到下一个关键帧(但不包括)的所有内容,然后只需从现有关键帧开始重新复制所有内容。