Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used) 解决方法

本文解决了一个关于使用FFmpeg截取视频帧时出现的错误问题,详细解释了命令参数的作用,并分享了一个有效的解决方案。同时,还提供了多个FFmpeg常见错误及其解决方法,帮助读者避免类似的陷阱。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当我准备使用ffmpeg截取视频帧时出现了如题错误提示。

使用命令如下:

ffmpeg -ss 15.683 -i /storage/emulated/0/test.mp4 -y -f image2 -t 0.001 /storage/emulated/0/0.jpg

为了便于理解,在说解决方法之前,先解释一下这个命令的各参数用处:

-ss 15.683 :将视频指向 15.683 秒,也就是从 15.683 秒开始
-i /storage/emulated/0/test.mp4 :输入 test.mp4 文件
-y:强制覆盖文件(防止因为重名出错)
-f image2:文件格式
-t 0.001 :持续时间(在本例中就相当于只取原视频的第 15.683秒-15.684秒)
/storage/emulated/0/0.jpg:输出文件路径

输入该命令会概率性的输出空文件,并提示以下错误:

ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
built with gcc 4.8 (GCC)
      configuration: --target-os=linux --cross-prefix=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/vagrant/SourceCode/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/vagrant/SourceCode/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
      libavutil      55. 17.103 / 55. 17.103
      libavcodec     57. 24.102 / 57. 24.102
      libavformat    57. 25.100 / 57. 25.100
      libavdevice    57.  0.101 / 57.  0.101
      libavfilter     6. 31.100 /  6. 31.100
      libswscale      4.  0.100 /  4.  0.100
      libswresample   2.  0.101 /  2.  0.101
      libpostproc    54.  0.100 / 54.  0.100
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/emulated/0/DCIM/SportsCamera/YDTL0254.mp4':
      Metadata:
        major_brand     : avc1
        minor_version   : 0
        compatible_brands: avc1isom
        creation_time   : 2018-11-21 06:19:31
      Duration: 00:00:17.98, start: 0.000000, bitrate: 60305 kb/s
        Stream #0:0(eng): Video: h264 (Main) (avc1 / 0x31637661), yuvj420p(pc, bt709), 3840x2160, 60301 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)
        Metadata:
          creation_time   : 2018-11-21 06:19:31
          handler_name    :  XiaoYi AVC   
          encoder         : Ambarella AVC encoder
    Output #0, image2, to '/storage/emulated/0/Android/data/com.equationl.videoshotpro/cache/3.jpg':
      Metadata:
        major_brand     : avc1
        minor_version   : 0
        compatible_brands: avc1isom
        encoder         : Lavf57.25.100
        Stream #0:0(eng): Video: mjpeg, yuvj420p(pc), 3840x2160, q=2-31, 200 kb/s, 29.97 fps, 29.97 tbn, 29.97 tbc (default)
        Metadata:
          creation_time   : 2018-11-21 06:19:31
          handler_name    :  XiaoYi AVC   
          encoder         : Lavc57.24.102 mjpeg
        Side data:
          unknown side data type 10 (24 bytes)
    Stream mapping:
      Stream #0:0 -> #0:0 (h264 (native) -> mjpeg (native))
    Press [q] to stop, [?] for help
    frame=    0 fps=0.0 q=0.0 Lsize=N/A time=00:00:00.00 bitrate=N/A speed=   0x    
    video:0kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
    Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)

搜索了一圈没有发现类似的问题,而且令人费解的是,该问题是概率性出现,并且出现时间几乎找不到任何规律。

虽然网上没有发现有类似的问题,但是我却发现我的写法似乎有点奇怪:

一般来说,使用 ffmpeg 会首先使用 -i 参数输入文件再使用 -ss 参数将其指向需要的位置,但是这样写的话效率比较低(具体原因网上有很多解释,我就不多说了),所以我先使用 -ss 移动指针再 -i 输入文件,问题似乎就出在这了,我看见网上几乎所有人的写法都是 -ss 参数 和 -t 参数连在一起使用的,而我不知道为什么,居然把他们分开了...

所以当我将其放在一起后,就正常了:

ffmpeg -ss 15.683 -t 0.001 -i /storage/emulated/0/test.mp4 -y -f image2 /storage/emulated/0/0.jpg

所以说,解决方案就是按照错误提示所说的,检查一下你的 -ss -t -frames 参数如果你有使用这些参数的话。

不过令我费解的是为什么 -ss 和 -t 分开后会概率性的出现输出空文件而不是直接100%输出错误呢?希望有大佬能不吝指教。

 

下面给大家附上几个ffmpeg常见错误和解决方法吧,让大家能少踩坑。(下文转自:https://trac.ffmpeg.org/wiki/Errors


Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument

When encountered this message is usually the last line in the console output. By itself is not very informative, but it is always accompanied by a more specific message that explains the actual issue, so scroll up for the actual error.


Invalid input file index: 2

The -map option is used to choose what inputs you want when default stream selection behavior does not fit your needs. This message can occur when the -map option is used to reference an input that does not exist. For example, if you have two inputs, but you're tyring to map a non-existing third input with -map 2 (note that ffmpeg starts counting from 0) then this error will appear.

Properly define your -map file index values. See the -map option documentation and FFmpeg Wiki: Map for more examples.


Stream map '0:a:0' matches no streams

Similar to the above error, but instead of an incorrect file index there is an incorrect stream index. For example, if the first input contains four video streams, but you use -map 0:v:4, then you are referencing a non-existing video stream (note that ffmpeg starts counting from 0).

Properly define your -map stream index values. See the -map option documentation and FFmpeg Wiki: Map for more examples. Alternatively, add a trailing ? to your map option, such as -map 0:v:4?, to make the map optional.


Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)

The -ss option allows you to skip to a certain point. This message is often seen when the -ss option value is greater than the duration of the input. For example, if -ss 30 is used for a 15 second input you may see this message.

Make sure your -ss, -t, -to, and/or -frames value does not exceed the input duration.


Unknown encoder 'foo'

Your ffmpeg build does not support the encoder you are trying to use.

  • If it is an external encoder, usually named with the "lib-" prefix, then the build must be compiled to support it. See ffmpeg -encoders for a list of encoders supported by your particular build.
  • You may be using an old, unsupoprted build. Development is very active, so it is recommended to use builds from the current git master branch. See the FFmpeg Downloadpage for options.
  • The issue could be a simple typo so check the spelling of the encoder name.

Use a current ffmpeg build. You may need to compile it to support your desired encoder. Alternatively, you may simply download a static build of ffmpeg–these builds usually support the most common encoders.


Trailing options were found on the commandline

This message is often overlooked by users and is caused by improper option placement. Placement of options matters, and trailing options are often ignored. Placement is as follows:

ffmpeg [global options] [input options] -i input [output options] output

The documentation will often specify if an option is global, input, and/or output.

Place your options in the correct location. Options before -i will be applied to the input, and options before the output name will be applied to the output. Options after the last output may be ignored.


No pixel format specified, yuv444p for H.264 encoding chosen.
Use -pix_fmt yuv420p for compatibility with outdated media players.

Your input and output may vary in supported pixel formats. ffmpeg will attempt to choose the "best" supported pixel format for your encoder. For libx264 it may use a pixel format that is not decodable by many non-FFmpeg based media players.

Adding -pix_fmt yuv420p or -vf format=yuv420p will ensure compatibility with dumb players.


[image2 @ 0x2e43320] Could not open file : images/output.png.tmp
av_interleaved_write_frame(): Input/output error

In this example the output was images/output.png, but the images directory did not exist.

Check that your directory exists and/or that the permissions are correct.


[image2 @ 0x3841320] Could not get frame filename number 2 from pattern 'output.png' (either set updatefirst or use a pattern like %03d within the filename pattern)
av_interleaved_write_frame(): Invalid argument

This usually occurs because the output name is incorrect or some option was omitted.

  • If outputting a single image you need to include -frames:v 1.
  • If outputting a series of images you need to use the proper naming pattern as described in the image muxer documentation. For example, output_%03d.png will make a series named output_001.png, output_002.png, output_003.png, etc.
  • If outputting a single image that is continuously overwritten with new images, add -update 1.

[mp4 @ 0x563cd3fa4700] Could not find tag for codec foo in stream #0, codec not currently supported in container

As the message says, the particular codec is not supported in your output container format.

Change the codec and/or output container format.


Streamcopy requested for output stream 0:0, which is fed from a complex filtergraph. Filtering and streamcopy cannot be used together

You can't stream copy (-c copy, -codec copy, etc) an output from a filter.

Remove this option or change copy to your desired encoder.

[libwebp encoder @ 0x1d463c0] WebPAnimEncoderAssemble() failed with error: 1 [vost#5:0/libwebp_anim @ 0x1d45240] [enc:libwebp_anim @ 0x1d45f00] Error submitting video frame to the encoder [vost#5:0/libwebp_anim @ 0x1d45240] [enc:libwebp_anim @ 0x1d45f00] Error flushing encoder: Cannot allocate memory [vost#5:0/libwebp_anim @ 0x1d45240] Task finished with error code: -12 (Cannot allocate memory) [vost#5:0/libwebp_anim @ 0x1d45240] Terminating thread with return code -12 (Cannot allocate memory) Output #7, webp, to '/mnt/data/encoded/snapshots/1944067372482666498/008.webp': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf62.0.100 Stream #7:0(und): Video: webp, yuv420p(progressive), 856x456 [SAR 1:1 DAR 107:57], q=2-31, 200 kb/s, 24 fps, 1k tbn (default) Metadata: encoder : Lavc62.0.101 libwebp_anim handler_name : VideoHandler vendor_id : [0][0][0][0] [libwebp encoder @ 0x1d4eec0] WebPAnimEncoderAssemble() failed with error: 1 [vost#7:0/libwebp_anim @ 0x1d4e140] [enc:libwebp_anim @ 0x1d4a2c0] Error submitting video frame to the encoder [vost#7:0/libwebp_anim @ 0x1d4e140] [enc:libwebp_anim @ 0x1d4a2c0] Error flushing encoder: Cannot allocate memory [vost#7:0/libwebp_anim @ 0x1d4e140] Task finished with error code: -12 (Cannot allocate memory) [vost#7:0/libwebp_anim @ 0x1d4e140] Terminating thread with return code -12 (Cannot allocate memory) [out#0/webp @ 0x19a8280] video:12KiB audio:0KiB subtitle:0KiB other streams:0KiB global headers:0KiB muxing overhead: 0.000000% [out#1/webp @ 0x1bdd040] video:34KiB audio:0KiB subtitle:0KiB other streams:0KiB global headers:0KiB muxing overhead: 0.000000% [out#2/webp @ 0x1bdef80] video:17KiB audio:0KiB subtitle:0KiB other streams:0KiB global headers:0KiB muxing overhead: 0.000000% [out#3/webp @ 0x1d3a900] video:17KiB audio:0KiB subtitle:0KiB other streams:0KiB global headers:0KiB muxing overhead: 0.000000% [out#4/webp @ 0x1d3fa80] video:11KiB audio:0KiB subtitle:0KiB other streams:0KiB global headers:0KiB muxing overhead: 0.000000% [out#5/webp @ 0x1d44480] video:0KiB audio:0KiB subtitle:0KiB other streams:0KiB global headers:0KiB muxing overhead: unknown [out#5/webp @ 0x1d44480] Output file is empty, nothing was encoded(check -ss / -t / -frames parameters if used) [out#6/webp @ 0x1d48cc0] video:0KiB audio:0KiB subtitle:0KiB other streams:0KiB global headers:0KiB muxing overhead: unknown [out#6/webp @ 0x1d48cc0] Output file is empty, nothing was encoded(check -ss / -t / -frames parameters if used) [out#7/webp @ 0x1d4d180] video:0KiB audio:0KiB subtitle:0KiB other streams:0KiB global headers:0KiB muxing overhead: unknown [out#7/webp @ 0x1d4d180] Output file is empty, nothing was encoded(check -ss / -t / -frames parameters if used) frame= 1 fps=0.0 q=-0.0 Lq=-0.0 q=-0.0 q=-0.0 q=-0.0 q=0.0 q=0.0 q=0.0 size= 12KiB time=N/A bitrate=N/A speed=N/A
最新发布
07-16
<think>我们正在处理一个关于libwebp编码器在视频转码或动画生成时出现的内存分配错误和编码失败问题。具体错误是“libwebp_anim encoder error submitting video frame”和“memory allocation failure during encoding”。 这些错误通常与内存不足、编码参数设置不当或输入数据问题有关。 我们将按照以下步骤进行问题排查和解决: 1. **检查系统内存**:内存分配失败可能是由于系统内存不足导致的。确保在编码过程中有足够的内存可用。如果处理大型视频,考虑增加可用内存或减少同时运行的内存密集型程序。 2. **调整编码参数**: - **降低分辨率**:高分辨率视频需要更多内存。尝试降低视频分辨率。 - **减少帧率**:高帧率会增加处理负担,适当降低帧率。 - **调整关键帧间隔**:关键帧(I帧)间隔过小会导致内存需求增加,适当增大关键帧间隔。 - **使用更高效的编码设置**:例如,调整质量参数(-q选项),较低的质量设置可能减少内存需求。 3. **检查输入数据**: - 确保输入视频的格式和帧数据是有效的。损坏的输入文件可能导致编码器处理异常。 - 如果输入是来自网络流,确保网络连接稳定,并且数据完整。 4. **更新库版本**:确保使用的libwebp库是最新版本,因为老版本可能存在已知的内存管理问题。 5. **分块处理**:对于非常大的视频,考虑将视频分成较小的片段分别编码,然后再合并。 6. **使用其他工具辅助**:如果上述方法无效,可以尝试先用其他工具(如ffmpeg)将视频转换为图像序列,然后使用libwebp提供的工具(如webpmux)将图像序列合成为WebP动画。 7. **代码检查**:如果是在自己的代码中调用libwebp库,检查是否正确使用了动画编码API。确保对每一帧都正确调用了`WebPAnimEncoderAdd`,并且在最后调用了`WebPAnimEncoderAssemble`。同时,确保正确释放所有资源。 下面是一个使用libwebp进行动画编码的示例代码片段(注意:这只是一个简化的示例,实际使用中需要处理更多细节): ```c #include <webp/mux.h> #include <webp/encode.h> // 创建编码器 WebPAnimEncoder* enc = WebPAnimEncoderNew(width, height, &options); // 添加帧 for (int i = 0; i < num_frames; i++) { WebPConfig config; WebPConfigInit(&config); // 设置配置参数(例如质量等) config.quality = 75; WebPPicture frame; if (!WebPPictureInit(&frame)) { // 错误处理 } frame.width = width; frame.height = height; // 设置帧数据(这里假设rgb_data是当前帧的RGB数据) WebPPictureImportRGB(&frame, rgb_data, rgb_stride); // 设置时间戳(毫秒) int timestamp = i * frame_duration; if (!WebPAnimEncoderAdd(enc, &frame, timestamp, &config)) { // 处理添加帧失败 WebPPictureFree(&frame); break; } WebPPictureFree(&frame); } // 完成编码 WebPData webp_data; WebPDataInit(&webp_data); if (!WebPAnimEncoderAssemble(enc, &webp_data)) { // 处理组装失败 } // 释放编码器 WebPAnimEncoderDelete(enc); ``` 在代码中,需要特别注意: - 每一帧都要正确初始化`WebPPicture`并释放。 - 时间戳是递增的。 - 配置参数(如质量)可以根据需要调整。 8. **查看错误日志**:如果库提供了更详细的错误信息,请查看并分析错误日志,以便更精确地定位问题。 9. **资源限制**:如果是在移动设备或嵌入式设备上运行,注意系统可能对单个进程的内存使用有限制,可能需要调整系统设置或优化代码。 10. **联系社区**:如果问题仍然无法解决,可以考虑在libwebp的社区或issue跟踪系统中搜索类似问题或提交新问题。 通过以上步骤,应该能够解决大部分内存分配错误和提交视频帧失败的问题。 §§相关问题§§ 1. 如何调整libwebp的编码参数以优化内存使用? 2. 除了内存不足,还有哪些原因会导致libwebp编码失败? 3. 如何将多个WebP图像合成为一个动画?</think>### libwebp编码器内存分配错误与帧提交失败问题分析 当使用libwebp编码器进行视频转码或动画生成时遇到"memory allocation failure during encoding"和"error submitting video frame"错误,通常由以下原因导致: #### 主要原因及解决方案 1. **内存分配失败** - **根本原因**:编码高分辨率/长视频时超出进程内存限制 - **解决方案**: - 降低分辨率:尝试缩小帧尺寸(如从4K降至1080p) - 分块处理:将长视频切割为小片段分别编码 - 优化配置: ```c WebPConfig config; WebPConfigInit(&config); config.thread_level = 1; // 限制线程数减少内存 config.low_memory = 1; // 启用低内存模式 ``` 2. **帧提交错误** - **常见触发场景**: - 输入帧格式与编码器不兼容(如YUV420 vs RGB) - 时间戳非单调递增($t_{n} ≤ t_{n-1}$) - 帧数据包含非法像素值 - **修复步骤**: ```c // 检查帧格式一致性 if (frame.colorspace != prev_frame.colorspace) { // 转换色彩空间或重建编码器 } // 确保时间戳递增 if (timestamp <= last_timestamp) { timestamp = last_timestamp + 1; // 强制递增 } ``` 3. **资源泄漏** - **诊断工具**:使用Valgrind检测未释放资源 - **关键释放点**: ```c WebPAnimEncoderDelete(enc); // 释放编码器 WebPPictureFree(&frame); // 释放帧内存 WebPMuxDelete(mux); // 释放复用器 ``` #### 完整工作流示例 ```c #include <webp/mux.h> int encode_webp_animation(Frame* frames, int count) { WebPAnimEncoderOptions enc_options; WebPAnimEncoderOptionsInit(&enc_options); enc_options.anim_params.loop_count = 0; // 无限循环 WebPAnimEncoder* enc = WebPAnimEncoderNew(width, height, &enc_options); for (int i = 0; i < count; ++i) { WebPConfig config; WebPConfigInit(&config); config.quality = 75; // 平衡质量与内存 WebPPicture frame; WebPPictureInit(&frame); frame.width = width; frame.height = height; WebPPictureImportRGB(&frame, frames[i].data, RGB_STRIDE); // 提交帧(时间戳毫秒单位) if (!WebPAnimEncoderAdd(enc, &frame, i * 100, &config)) { // 处理错误[^1] } WebPPictureFree(&frame); } WebPData webp_data; WebPDataInit(&webp_data); WebPAnimEncoderAssemble(enc, &webp_data); // 保存webp_data.bytes... WebPAnimEncoderDelete(enc); return SUCCESS; } ``` #### 预防性措施 1. **输入验证**: - 检查帧尺寸一致性:满足 $W_{\text{max}} \times H_{\text{max}} \times 4 \leq \text{可用内存}$ - 验证色彩空间:强制转换为RGB/A或YUV420 2. **内存监控**: ```c #include <sys/resource.h> struct rusage usage; getrusage(RUSAGE_SELF, &usage); if (usage.ru_maxrss > SAFE_THRESHOLD) { // 触发降级处理 } ``` 3. **渐进编码**: - 使用`WebPAnimEncoderAdd`的增量模式 - 每10帧调用`WebPAnimEncoderGetError`检查状态 > **重要提示**:最新版libwebp (v1.3.2+) 修复了多线程环境下的内存竞争问题,建议优先升级[^2]。对于Android平台需额外检查媒体准备状态,避免因网络问题导致资源加载失败[^1]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值