ffmpeg 使用问题记录

  1. cv::Mat 转 AVFrame
 /* 这种写法存在内存泄漏 */
AVFrame *avframe = av_frame_alloc();
avframe->width = 1920;
avframe->height = 1080;
avframe->format = AV_PIX_FMT_NV12;
int cvLinesizes[1];
cvLinesizes[0] = frame_mat.step1();
SwsContext* conversion = sws_getContext(frame_mat.cols, frame_mat.rows, AV_PIX_FMT_BGR24, avframe->width, avframe->height, AV_PIX_FMT_NV12, SWS_FAST_BILINEAR, NULL, NULL, NULL);
av_image_alloc(avframe->data, avframe->linesize, avframe->width, avframe->height, AV_PIX_FMT_NV12, 1);
sws_scale(conversion, &frame_mat.data, cvLinesizes , 0, frame_mat.rows, avframe->data, avframe->linesize);
sws_freeContext(conversion);

// 内存释放无效
av_frame_free(&avframe);

/************************************* 正确写法 *****************************************/
AVFrame *avframe = av_frame_alloc();
avframe->width = 1920;
avframe->height = 1080;
avframe->format = AV_PIX_FMT_NV12;
int cvLinesizes[1];
cvLinesizes[0] = frame_mat.step1();

uint8_t *buffer = (uint8_t*)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_NV12, avframe->width, avframe->height, IMAGE_ALIGN));
av_image_fill_arrays(avframe->data, avframe->linesize, buffer, AV_PIX_FMT_NV12, avframe->width, avframe->height, IMAGE_ALIGN);
SwsContext* conversion = sws_getContext(frame_mat.cols, frame_mat.rows, AV_PIX_FMT_BGR24, avframe->width, avframe->height, AV_PIX_FMT_NV12, SWS_FAST_BILINEAR, NULL, NULL, NULL);
sws_scale(conversion, &frame_mat.data, cvLinesizes , 0, frame_mat.rows, avframe->data, avframe->linesize);
sws_freeContext(conversion);

// 手动释放 buffer
av_frame_free(&avframe);
av_free(buffer);
  1. Intel 硬件解码 qsv

调用方法 av_hwdevice_ctx_create 提示以下信息:
[AVHWDeviceContext @ 0x7fff24143500] Failed to initialise VAAPI connection: -1 (unknown libva error).

或者 ffmpeg 参数 h264_qsv 提示以下信息:
[h264_qsv @ 0x5624732fc180] Error initializing an internal MFX session: unsupported (-3)
[h264_qsv @ 0x5624732fc180] Error initializing an MFX session
[h264_qsv @ 0x5624732fc180] Error initializing the MFX video decoder: invalid handle (-6)
Error while decoding stream #0:0: Invalid argument

解决方式:缺少 Intel QuickSync support,需安装 intel-media-driver, intel-media-sdk

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在Java中使用FFmpeg进行推拉流涉及到音视频处理和流媒体操作。下面是一个简单的示例代码,展示了如何使用Java调用FFmpeg进行推拉流操作: 1. 首先,确保你已经安装了FFmpeg,并将其安装路径添加到系统的环境变量中。 2. 在Java项目中,你需要使用Java的ProcessBuilder类来执行FFmpeg命令。下面是一个简单的推流示例代码: ```java import java.io.IOException; public class FFmpegPushStreamExample { public static void main(String[] args) { String ffmpegPath = "ffmpeg的安装路径"; String inputUrl = "输入流URL"; String outputUrl = "输出流URL"; try { ProcessBuilder processBuilder = new ProcessBuilder( ffmpegPath, "-i", inputUrl, "-c:v", "copy", "-c:a", "copy", outputUrl ); processBuilder.inheritIO(); Process process = processBuilder.start(); process.waitFor(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } } ``` 在上述代码中,你需要将`ffmpegPath`替换为你的FFmpeg安装路径,将`inputUrl`替换为输入流URL,将`outputUrl`替换为输出流URL。这个示例使用了`copy`编解码器来快速拷贝输入流的音视频数据。 3. 如果你想要进行拉流操作,可以使用类似的方式执行FFmpeg命令。下面是一个简单的拉流示例代码: ```java import java.io.IOException; public class FFmpegPullStreamExample { public static void main(String[] args) { String ffmpegPath = "ffmpeg的安装路径"; String inputUrl = "输入流URL"; String outputFile = "输出文件路径"; try { ProcessBuilder processBuilder = new ProcessBuilder( ffmpegPath, "-i", inputUrl, "-c:v", "copy", "-c:a", "copy", outputFile ); processBuilder.inheritIO(); Process process = processBuilder.start(); process.waitFor(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } } ``` 在上述代码中,你需要将`ffmpegPath`替换为你的FFmpeg安装路径,将`inputUrl`替换为输入流URL,将`outputFile`替换为输出文件路径。这个示例同样使用了`copy`编解码器来快速拷贝输入流的音视频数据。 请注意,在实际项目中,你可能需要添加错误处理和日志记录等功能来增强代码的健壮性和可靠性。此外,还可以使用FFmpeg的其他参数和选项来满足具体的需求。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值