Ffmpeg报Error initializing filter ‘gltransition‘这个有问题解决方法

1:安装完ffmpeg后,检测是否安装ffmpeg

执行:ffmpeg

如下是说明没问题

root@hroot-virtual-machine:~/ffmpeg-4.2.2# ffmpeg
ffmpeg version 4.2.2 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 9 (Ubuntu 9.3.0-17ubuntu1~20.04)
  configuration: --enable-libx264 --enable-libx265 --enable-libvpx --enable-libfdk-aac --enable-libmp3lame --enable-libopus --enable-nonfree --enable-gpl --enable-opengl --enable-filter=gltransition --extra-libs='-lGLEW -lglfw -ldl'
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Use -h to get full help or, even better, run 'man ffmpeg'

检测是否安装配置gltransition

root@hroot-virtual-machine:~/ffmpeg-4.2.2# ffmpeg -v 0 -filters | grep gltransition
 T.. gltransition      VV->V      OpenGL blend transitions

测试gltransition是否运转成功

//创建一个文件存放素材
# cd /
# mkdir img
# cd img

root@hroot-virtual-machine:/img# ll
总用量 1248
drwxr-xr-x  2 root root   4096 7月   2 11:38 ./
drwxr-xr-x 21 root root   4096 7月   2 10:05 ../
-rw-r--r--  1 root root 185169 7月   2 10:05 1.jpg
-rw-r--r--  1 root root  67524 7月   2 10:05 2.jpg
-rw-r--r--  1 root root 149603 7月   2 10:05 3.jpg
-rw-r--r--  1 root root 188339 7月   2 10:05 4.jpg
-rw-r--r--  1 root root 176810 7月   2 10:05 5.jpg
-rw-r--r--  1 root root 487613 7月   2 10:53 bgm.m4a

查看我们下载的特效文件

root@hroot-virtual-machine:~/ffmpeg-gl-transition/gl-transitions/transitions# ll
总用量 320
drwxr-xr-x 2 root root 4096 7月   1 18:22 ./
drwxr-xr-x 6 root root 4096 7月   1 18:22 ../
-rw-r--r-- 1 root root  462 7月   1 18:22 angular.glsl
-rw-r--r-- 1 root root  752 7月   1 18:22 Bounce.glsl
-rw-r--r-- 1 root root 2593 7月   1 18:22 BowTieHorizontal.glsl
-rw-r--r-- 1 root root 2541 7月   1 18:22 BowTieVertical.glsl
-rw-r--r-- 1 root root 1908 7月   1 18:22 BowTieWithParameter.glsl
-rw-r--r-- 1 root root  245 7月   1 18:22 burn.glsl
-rw-r--r-- 1 root root  981 7月   1 18:22 ButterflyWaveScrawler.glsl
-rw-r--r-- 1 root root  485 7月   1 18:22 cannabisleaf.glsl
-rw-r--r-- 1 root root  550 7月   1 18:22 CircleCrop.glsl
-rw-r--r-- 1 root root  422 7月   1 18:22 circle.glsl
-rw-r--r-- 1 root root  408 7月   1 18:22 circleopen.glsl
-rw-r--r-- 1 root root  392 7月   1 18:22 colorphase.glsl
-rw-r--r-- 1 root root  364 7月   1 18:22 ColourDistance.glsl
-rw-r--r-- 1 root root  627 7月   1 18:22 CrazyParametricFun.glsl
-rw-r--r-- 1 root root  623 7月   1 18:22 crosshatch.glsl
-rw-r--r-- 1 root root  228 7月   1 18:22 crosswarp.glsl
-rw-r--r-- 1 root root 2396 7月   1 18:22 CrossZoom.glsl
-rw-r--r-- 1 root root 1738 7月   1 18:22 cube.glsl
-rw-r--r-- 1 root root  361 7月   1 18:22 directional-easing.glsl
-rw-r--r-- 1 root root  315 7月   1 18:22 Directional.glsl
-rw-r--r-- 1 root root  498 7月   1 18:22 directionalwarp.glsl
-rw-r--r-- 1 root root  577 7月   1 18:22 directionalwipe.glsl
-rw-r--r-- 1 root root  533 7月   1 18:22 displacement.glsl
-rw-r--r-- 1 root root 1627 7月   1 18:22 DoomScreenTransition.glsl
-rw-r--r-- 1 root root 1221 7月   1 18:22 doorway.glsl
-rw-r--r-- 1 root root  378 7月   1 18:22 Dreamy.glsl
-rw-r--r-- 1 root root 1163 7月   1 18:22 DreamyZoom.glsl
-rw-r--r-- 1 root root  413 7月   1 18:22 fadecolor.glsl
-rw-r--r-- 1 root root  136 7月   1 18:22 fade.glsl
-rw-r--r-- 1 root root  558 7月   1 18:22 fadegrayscale.glsl
-rw-r--r-- 1 root root 2231 7月   1 18:22 FilmBurn.glsl
-rw-r--r-- 1 root root  522 7月   1 18:22 flyeye.glsl
-rw-r--r-- 1 root root 2242 7月   1 18:22 GlitchDisplace.glsl
-rw-r--r-- 1 root root  607 7月   1 18:22 GlitchMemories.glsl
-rw-r--r-- 1 root root 2300 7月   1 18:22 GridFlip.glsl
-rw-r--r-- 1 root root  356 7月   1 18:22 heart.glsl
-rw-r--r-- 1 root root 1644 7月   1 18:22 hexagonalize.glsl
-rw-r--r-- 1 root root 7518 7月   1 18:22 InvertedPageCurl.glsl
-rw-r--r-- 1 root root  585 7月   1 18:22 kaleidoscope.glsl
-rw-r--r-- 1 root root  647 7月   1 18:22 LeftRight.glsl
-rw-r--r-- 1 root root  682 7月   1 18:22 LinearBlur.glsl
-rw-r--r-- 1 root root  189 7月   1 18:22 luma.glsl
-rw-r--r-- 1 root root 3812 7月   1 18:22 luminance_melt.glsl
-rw-r--r-- 1 root root  390 7月   1 18:22 morph.glsl
-rw-r--r-- 1 root root 1127 7月   1 18:22 Mosaic.glsl
-rw-r--r-- 1 root root  339 7月   1 18:22 multiply_blend.glsl
-rw-r--r-- 1 root root 1444 7月   1 18:22 perlin.glsl
-rw-r--r-- 1 root root  361 7月   1 18:22 pinwheel.glsl
-rw-r--r-- 1 root root  584 7月   1 18:22 pixelize.glsl
-rw-r--r-- 1 root root  464 7月   1 18:22 polar_function.glsl
-rw-r--r-- 1 root root  326 7月   1 18:22 PolkaDotsCurtain.glsl
-rw-r--r-- 1 root root  364 7月   1 18:22 Radial.glsl
-rw-r--r-- 1 root root  444 7月   1 18:22 randomNoisex.glsl
-rw-r--r-- 1 root root  399 7月   1 18:22 randomsquares.glsl
-rw-r--r-- 1 root root  378 7月   1 18:22 ripple.glsl
-rw-r--r-- 1 root root  878 7月   1 18:22 rotate_scale_fade.glsl
-rw-r--r-- 1 root root  348 7月   1 18:22 rotateTransition.glsl
-rw-r--r-- 1 root root  376 7月   1 18:22 SimpleZoom.glsl
-rw-r--r-- 1 root root  771 7月   1 18:22 squareswire.glsl
-rw-r--r-- 1 root root  455 7月   1 18:22 squeeze.glsl
-rw-r--r-- 1 root root 8015 7月   1 18:22 StereoViewer.glsl
-rw-r--r-- 1 root root 1539 7月   1 18:22 swap.glsl
-rw-r--r-- 1 root root  715 7月   1 18:22 Swirl.glsl
-rw-r--r-- 1 root root 4051 7月   1 18:22 tangentMotionBlur.glsl
-rw-r--r-- 1 root root  650 7月   1 18:22 TopBottom.glsl
-rw-r--r-- 1 root root  651 7月   1 18:22 TVStatic.glsl
-rw-r--r-- 1 root root 1399 7月   1 18:22 undulatingBurnOut.glsl
-rw-r--r-- 1 root root  429 7月   1 18:22 WaterDrop.glsl
-rw-r--r-- 1 root root  397 7月   1 18:22 wind.glsl
-rw-r--r-- 1 root root  274 7月   1 18:22 windowblinds.glsl
-rw-r--r-- 1 root root  299 7月   1 18:22 windowslice.glsl
-rw-r--r-- 1 root root  192 7月   1 18:22 wipeDown.glsl
-rw-r--r-- 1 root root  192 7月   1 18:22 wipeLeft.glsl
-rw-r--r-- 1 root root  192 7月   1 18:22 wipeRight.glsl
-rw-r--r-- 1 root root  192 7月   1 18:22 wipeUp.glsl
-rw-r--r-- 1 root root 1073 7月   1 18:22 ZoomInCircles.glsl

获取到绝对路径

root@hroot-virtual-machine:~/ffmpeg-gl-transition/gl-transitions/transitions# pwd
/root/ffmpeg-gl-transition/gl-transitions/transitions

--------然后回到素材文件夹哪里
cd /img

开始执行测试

ffmpeg \
-t 1 -loop 1 -i 1.jpg \
-t 1 -loop 1 -i 2.jpg \
-filter_complex \
"gltransition=offset=0.5:duration=0.5:source=/root/ffmpeg-gl-transition/gl-transitions/transitions/crosswarp.glsl" \
-y out.mp4

source:后面是特效文件的绝对路径

结果发现:

root@hroot-virtual-machine:/img# ffmpeg \
> -t 1 -loop 1 -i 1.jpg \
> -t 1 -loop 1 -i 2.jpg \
> -filter_complex \
> "gltransition=offset=0.5:duration=0.5:source=/root/ffmpeg-gl-transition/gl-transitions/transitions/crosswarp.glsl" \
> -y out.mp4
ffmpeg version 4.2.2 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 9 (Ubuntu 9.3.0-17ubuntu1~20.04)
  configuration: --enable-libx264 --enable-libx265 --enable-libvpx --enable-libfdk-aac --enable-libmp3lame --enable-libopus --enable-nonfree --enable-gpl --enable-opengl --enable-filter=gltransition --extra-libs='-lGLEW -lglfw -ldl'
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Input #0, image2, from '1.jpg':
  Duration: 00:00:00.04, start: 0.000000, bitrate: 37033 kb/s
    Stream #0:0: Video: mjpeg (Baseline), yuvj444p(pc, bt470bg/unknown/unknown), 400x400 [SAR 1:1 DAR 1:1], 25 fps, 25 tbr, 25 tbn, 25 tbc
Input #1, image2, from '2.jpg':
  Duration: 00:00:00.04, start: 0.000000, bitrate: 13504 kb/s
    Stream #1:0: Video: mjpeg (Baseline), yuvj444p(pc, bt470bg/unknown/unknown), 400x400 [SAR 1:1 DAR 1:1], 25 fps, 25 tbr, 25 tbn, 25 tbc
[AVFilterGraph @ 0x565112acce40] Error initializing filter 'gltransition' with args 'offset=0.5:duration=0.5:source=/root/ffmpeg-gl-transition/gl-transitions/transitions/crosswarp.glsl'
Error initializing complex filters.
Operation not permitted

报Error initializing filter 'gltransition'这个有问题

解决方法如下:

安装xvfb(安装过了直接跳过)

sudo apt-get -y install xvfb

然后启动xvfb(线程阻塞)

Xvfb :1 -screen 0 1280x1024x16

接着调用xvfb执行特效

env DISPLAY=:1  ffmpeg 命令

例如刚刚报错的那句:

env DISPLAY=:1 ffmpeg \
	-t 1 -loop 1 -i 1.jpg \
	-t 1 -loop 1 -i 2.jpg \
	-filter_complex \
	"gltransition=offset=0.5:duration=0.5:source=/root/ffmpeg-gl-transition/gl-transitions/transitions/crosswarp.glsl" \
	-y out1.mp4

执行完没有保存说明成功了,我们查看一下:

root@hroot-virtual-machine:/img# ll
	总用量 1336
	drwxr-xr-x  2 root root   4096 7月   2 11:49 ./
	drwxr-xr-x 21 root root   4096 7月   2 10:05 ../
	-rw-r--r--  1 root root 185169 7月   2 10:05 1.jpg
	-rw-r--r--  1 root root  67524 7月   2 10:05 2.jpg
	-rw-r--r--  1 root root 149603 7月   2 10:05 3.jpg
	-rw-r--r--  1 root root 188339 7月   2 10:05 4.jpg
	-rw-r--r--  1 root root 176810 7月   2 10:05 5.jpg
	-rw-r--r--  1 root root 487613 7月   2 10:53 bgm.m4a
	-rw-r--r--  1 root root  89138 7月   2 11:49 out1.mp4	(我们图片加特效合成的视频)

视频添加特效,没有EGL的一定要安装xvfb,用xvfb来添加特效

env DISPLAY=:1 执行命令一定要在前面加这个

### 回答1: 好的,下面是一个示例代码实现一个bitstream filter,可以处理H264和HEVC码流数据,产生各种类型的错误码流。具体实现如下: ```c #include <stdio.h> #include <stdint.h> #include "libavcodec/avcodec.h" #include "libavutil/common.h" #include "libavutil/opt.h" #include "libavutil/pixdesc.h" #include "libavutil/mem.h" typedef struct { const AVClass *class; int error_type; // 错误类型 int error_percent; // 错误率 int error_count; // 错误计数 uint32_t *crc_table; // CRC表 } MyBitstreamFilterContext; static uint32_t crc32_table[256]; static av_cold int init(AVBitStreamFilterContext *bsfc) { MyBitstreamFilterContext *ctx = bsfc->priv_data; av_log(bsfc, AV_LOG_VERBOSE, "Initializing bitstream filter.\n"); // 初始化CRC表 for (int i = 0; i < 256; i++) { uint32_t crc = i; for (int j = 0; j < 8; j++) crc = (crc >> 1) ^ (-(crc & 1u) & 0xEDB88320u); crc32_table[i] = crc; } ctx->crc_table = av_malloc(65536 * sizeof(uint32_t)); for (int i = 0; i < 65536; i++) { uint32_t crc = 0xFFFFFFFF; uint8_t *data = (uint8_t *)&i; for (int j = 0; j < 2; j++) crc = (crc >> 8) ^ crc32_table[(crc & 0xFF) ^ data[j]]; ctx->crc_table[i] = crc ^ 0xFFFFFFFF; } return 0; } static int filter(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int keyframe) { MyBitstreamFilterContext *ctx = bsfc->priv_data; if (ctx->error_type == 0) { // 不添加错误 *poutbuf = av_malloc(buf_size); memcpy(*poutbuf, buf, buf_size); *poutbuf_size = buf_size; return 0; } int err_count = 0; int err_max = buf_size * ctx->error_percent / 100; if (ctx->error_type == 1) { // 随机比特翻转 *poutbuf = av_malloc(buf_size); *poutbuf_size = buf_size; for (int i = 0; i < buf_size; i++) { (*poutbuf)[i] = buf[i] ^ (av_lfg_get(&avctx->internal->entropy) & 0xFF); if ((*poutbuf)[i] != buf[i]) err_count++; if (err_count >= err_max) break; } } else if (ctx->error_type == 2) { // 随机字节翻转 *poutbuf = av_malloc(buf_size); *poutbuf_size = buf_size; for (int i = 0; i < buf_size; i++) { (*poutbuf)[i] = buf[i]; if (av_lfg_get(&avctx->internal->entropy) % 256 == 0) { (*poutbuf)[i] = ~buf[i]; err_count++; } if (err_count >= err_max) break; } } else if (ctx->error_type == 3) { // 随机插入字节 *poutbuf = av_malloc(buf_size + err_max); *poutbuf_size = buf_size + err_max; int pos = 0; for (int i = 0; i < buf_size; i++) { (*poutbuf)[pos++] = buf[i]; if (av_lfg_get(&avctx->internal->entropy) % 256 == 0) { (*poutbuf)[pos++] = 0xFF; err_count++; } if (err_count >= err_max) break; } } else if (ctx->error_type == 4) { // 随机删除字节 *poutbuf = av_malloc(buf_size - err_max); *poutbuf_size = buf_size - err_max; int pos = 0; for (int i = 0; i < buf_size; i++) { if (av_lfg_get(&avctx->internal->entropy) % 256 != 0) { (*poutbuf)[pos++] = buf[i]; } else { err_count++; } if (err_count >= err_max) break; } } else if (ctx->error_type == 5) { // 随机更改NALU类型 uint8_t nal_unit_type; uint8_t *nal_start = (uint8_t *)buf; uint8_t *nal_end = nal_start + buf_size; *poutbuf = av_malloc(buf_size); *poutbuf_size = buf_size; while (nal_start < nal_end) { nal_unit_type = (*nal_start) & 0x1F; int err = av_lfg_get(&avctx->internal->entropy) % 6; if (err > 0 && err_count < err_max) { if (nal_unit_type == 0 || nal_unit_type == 1 || nal_unit_type == 2) { // 更改NALU类型为非IDR图像 (*poutbuf)[nal_start - buf] = ((*nal_start) & 0xE0) | 0x01; } else if (nal_unit_type == 3) { // 更改NALU类型为IDR图像 (*poutbuf)[nal_start - buf] = ((*nal_start) & 0xE0) | 0x05; } else if (nal_unit_type >= 6 && nal_unit_type <= 9) { // 更改NALU类型为SEI (*poutbuf)[nal_start - buf] = ((*nal_start) & 0xE0) | 0x06; } else if (nal_unit_type == 14) { // 更改NALU类型为SPS (*poutbuf)[nal_start - buf] = ((*nal_start) & 0xE0) | 0x07; } else if (nal_unit_type == 15) { // 更改NALU类型为PPS (*poutbuf)[nal_start - buf] = ((*nal_start) & 0xE0) | 0x08; } err_count++; } else { (*poutbuf)[nal_start - buf] = (*nal_start); } nal_start++; } } ctx->error_count += err_count; if (ctx->error_count >= buf_size * 10) { av_log(bsfc, AV_LOG_WARNING, "Too many errors generated.\n"); return AVERROR_INVALIDDATA; } return 0; } static av_cold void close(AVBitStreamFilterContext *bsfc) { MyBitstreamFilterContext *ctx = bsfc->priv_data; av_free(ctx->crc_table); av_log(bsfc, AV_LOG_VERBOSE, "Closing bitstream filter.\n"); } AVBitStreamFilter ff_my_bitstream_filter = { .name = "my_bsfilter", .filter = filter, .priv_data_size = sizeof(MyBitstreamFilterContext), .init = init, .close = close, }; ``` 这个bitstream filter可以根据传入的参数产生不同类型的错误码流。可以通过设置以下参数: - error_type:错误类型,0表示不添加错误,1表示随机比特翻转,2表示随机字节翻转,3表示随机插入字节,4表示随机删除字节,5表示随机更改NALU类型。 - error_percent:错误率,表示要产生的错误码流的比例。 - crc_check:是否要进行CRC校验。 使用方法如下: ```c AVCodecContext *codec_ctx; AVBitStreamFilterContext *bsfc; AVPacket pkt; MyBitstreamFilterContext *my_bsfc; // 初始化CodecContext和AVPacket bsfc = av_bitstream_filter_init("my_bsfilter"); my_bsfc = bsfc->priv_data; my_bsfc->error_type = 1; // 设置错误类型 my_bsfc->error_percent = 5; // 设置错误率 if (av_bitstream_filter_filter(bsfc, codec_ctx, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, pkt.flags & AV_PKT_FLAG_KEY)) { // 错误处理 } if (my_bsfc->crc_check) { uint32_t crc = 0xFFFFFFFF; for (int i = 0; i < pkt.size; i++) crc = (crc >> 8) ^ my_bsfc->crc_table[(crc & 0xFF) ^ pkt.data[i]]; crc ^= 0xFFFFFFFF; if (crc != 0) // CRC检验失败 } // 使用处理后的pkt进行后续操作 av_bitstream_filter_close(bsfc); ``` ### 回答2: 在FFmpeg中实现一个bitstream filter,可以用于处理H.264和HEVC码流数据,并产生各种类型的错误码流。下面是一个简单的示例代码: ```c #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/avutil.h> #include <libavutil/bitstream.h> typedef struct { AVBitStreamFilterContext *bsf; AVCodecParameters *codecpar; AVCodecContext *codecctx; } ErrorBitstreamFilter; static int filter_init(AVBSFContext *ctx) { ErrorBitstreamFilter *filter = ctx->priv_data; int ret = avcodec_parameters_copy(filter->codecpar, ctx->par_in); if (ret < 0) { return ret; } filter->codecctx = avcodec_alloc_context3(NULL); if (!filter->codecctx) { return AVERROR(ENOMEM); } avcodec_parameters_to_context(filter->codecctx, filter->codecpar); filter->bsf = av_bitstream_filter_init("error"); if (!filter->bsf) { return AVERROR(ENOMEM); } return 0; } static int filter_filter(AVBSFContext *ctx, AVPacket *pkt) { ErrorBitstreamFilter *filter = ctx->priv_data; AVPacket filtered_pkt = { 0 }; int ret = av_bitstream_filter_filter(filter->bsf, filter->codecctx, NULL, &filtered_pkt.data, &filtered_pkt.size, pkt->data, pkt->size, pkt->flags & AV_PKT_FLAG_KEY); if (ret < 0) { return ret; } av_packet_unref(pkt); av_packet_move_ref(pkt, &filtered_pkt); return 0; } static void filter_close(AVBSFContext *ctx) { ErrorBitstreamFilter *filter = ctx->priv_data; av_bitstream_filter_close(filter->bsf); avcodec_free_context(&filter->codecctx); } AVBitStreamFilter ff_error_bitstream_filter = { .name = "error", .priv_data_size = sizeof(ErrorBitstreamFilter), .init = filter_init, .filter = filter_filter, .close = filter_close, }; ``` 此代码定义了一个名为`error`的bitstream filter。它通过进行错误处理来产生不同类型的错误码流。使用此bitstream filter,可以将其应用于H.264和HEVC码流数据以生成错误码流。 要使用此bitstream filter,需要在FFmpeg的配置中包含此代码,并在引入相关头文件的情况下重新编译FFmpeg库。具体的使用方法可以参考FFmpeg的官方文档和示例程序。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值