gpt4 book ai didi

ffmpeg - 如何用 avcodec_send_frame() 和 avcodec_receive_packet() 替换 avcodec_encode_audio2()/avcodec_encode_video2()?

转载 作者:行者123 更新时间:2023-12-04 22:52:14 28 4
gpt4 key购买 nike

link 中的复用示例中我正在尝试使用 avcodec_send_frame()avcodec_receive_packet()而不是 avcodec_encode_audio2()/avcodec_encode_video2()因为它们已被弃用。在

  352     ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet);
353 if (ret < 0) {
354 fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret));
355 exit(1);
356 }
357
358 if (got_packet) {
359 ret = write_frame(oc, &c->time_base, ost->st, &pkt);
360 if (ret < 0) {
361 fprintf(stderr, "Error while writing audio frame: %s\n",
362 av_err2str(ret));
363 exit(1);
364 }
365 }
366
367 return (frame || got_packet) ? 0 : 1;
并且在
  522     ret = avcodec_encode_video2(c, &pkt, frame, &got_packet);
523 if (ret < 0) {
524 fprintf(stderr, "Error encoding video frame: %s\n", av_err2str(ret));
525 exit(1);
526 }
527
528 if (got_packet) {
529 ret = write_frame(oc, &c->time_base, ost->st, &pkt);
530 } else {
531 ret = 0;
532 }
533
534 if (ret < 0) {
535 fprintf(stderr, "Error while writing video frame: %s\n", av_err2str(ret));
536 exit(1);
537 }
538
539 return (frame || got_packet) ? 0 : 1;
分配什么 got_packet使用 avcodec_send_frame() 时的变量和 avcodec_receive_packet()功能以及如果我这样做如何更改代码?到目前为止我已经尝试过了
ret = avcodec_send_frame(c, frame);
if (ret < 0) {
fprintf(stderr, "Error sending the frame to the audio encoder\n");
exit(1);
}
if (ret = 0) {
got_packet = avcodec_receive_packet(c, &pkt);
if (got_packet == AVERROR(EAGAIN) || got_packet == AVERROR_EOF){
fprintf(stderr, "Error receiving packet\n");
return -1;}
else if (got_packet < 0) {
fprintf(stderr, "Error encoding audio frame\n");
exit(1);
}
ret1 = write_frame(oc, &c->time_base, ost->st, &pkt);
if (ret1 < 0) {
fprintf(stderr, "Error while writing audio frame: %s\n",
av_err2str(ret1));
exit(1);
}
av_packet_unref(&pkt);
}
return (frame || got_packet) ? 0 : 1;
但不工作,我很难让它工作。

最佳答案

avcodec_encode_video2() , got_packet_ptr将是 0当输出数据包为空时。 got_packet_ptr将等于 1当输出数据包非空并且在排空模式下(在文件结束的情况下)NULL发送至avcodec_encode_video2()直到所有缓冲项都被刷新,之后它变为 0再次。
在新 API 中,返回值来自 avcodec_receive_packet()将等于 0当缓冲区已满或进入排空模式时,类似于got_packet_ptr等于 1 . avcodec_receive_packet()否则返回负错误代码 AVERROR(EAGAIN)这意味着输出在当前状态下不可用 - 用户必须尝试发送输入或 AVERROR_EOF即,编码器已完全刷新,将不再有输出数据包。
使用 muxing.c 中的新 API以下方式的示例对我有用:

ret = avcodec_send_frame(c, frame);
if (ret < 0) {
fprintf(stderr, "Error sending a frame for encoding\n");
exit(1);
}
while (ret >= 0) {
ret = avcodec_receive_packet(c, &pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF){
return (ret==AVERROR(EAGAIN)) ? 0:1;
}
else if (ret < 0) {
fprintf(stderr, "Error during encoding\n");
exit(1);
}
ret = write_frame(oc, &c->time_base, ost->st, &pkt);
if (ret < 0) {
fprintf(stderr, "Error while writing video frame: %s\n", av_err2str(ret));
exit(1);
}
av_packet_unref(&pkt);
}
return (frame) ? 0 : 1;

关于ffmpeg - 如何用 avcodec_send_frame() 和 avcodec_receive_packet() 替换 avcodec_encode_audio2()/avcodec_encode_video2()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59470927/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com