video - 如何为 mp4 容器的视频流指定偏移量和查找位置?

标签 video ffmpeg mp4 video-encoding ffprobe

似乎我可以拥有其中一个( -ss-itsoffset ),但不能同时拥有两者,否则它们会相互取消。

我使用以下命令创建 output.mp4:

ffmpeg -i input.mp3 -itsoffset 4 -t 4 -ss 3 -i input.mp4 -c:v copy output.mp4

使用此命令,我希望音频立即开始播放,视频在 4 秒后开始播放,在视频 3 秒后开始播放。

但是,当我播放 input.mp4 时,视频开始只比音频晚一秒(而不是 4 秒)。我想它做了一个“偏移 - 搜索 = 1”类型的事情。

如何防止搜索影响偏移量?

干杯!

最佳答案

"I expect the audio to start playing right away, and the video to start playing 4 seconds later,
(seeked at 3 seconds into the video).

How can it be done without re-encoding?"


尝试使用:
ffmpeg -ss 00:00:03 -itsoffset 4 -i video.mp4 -ss 00:00:00 -i audio.mp3 -c:v copy output.mp4
命令的含义(以及它们的输入/顺序也很重要)...-ss 00:00:03 = 搜索 3 秒的输入 video.mp4设置为输出的起点视频 追踪。-itsoffset 4 = 在输入播放为输出之前设置延迟。静止帧 4 秒。-i video.mp4 = 设置 视频 此时输入。-ss 00:00:00 = 寻找 0 秒的输入 audio.mp4作为输出的起点音频追踪。-i audio.mp3 = 设置 音频此时输入...(请参阅下面的音频旁注)。-c:v copy =(避免重新编码 视频 轨道)。output.mp4 = 设置输出 a/v 容器 .
旁注:
关于视频:
一个 MPEG 视频只能 寻找关键帧 (也称为 i-frames)。如果您设置 -ss 3,事情可能无法按预期进行。同时还使用 -c:v copy ,并且在 -ss 时没有找到关键帧.
3 秒时可能缺少关键帧可能会解释您的情况:

"When I play input.mp4, the video starts only one second later than the audio
(as opposed to 4 seconds)"


这甚至是 警告 在文档中(参见:FFmpeg wiki page - Seeking)。

Seeking while doing a codec copy

Using -ss as input option together with -c:v copy might not be accurate since ffmpeg is forced to only use/split on i-frames. Though it will —if possible adjust the start time of the stream to a negative value to compensate for that.

Basically, if you specify "second 157" and there is no key frame until second 159, it will include two seconds of audio (with no video) at the start, then will start from the first key frame.

So be careful when splitting and doing codec copy
.


唯一的解决方案是继续测试/调整视频-ss找到次优折衷的值(value)。
  • 考虑使用 HOURS:MM:SS.MILLISECONDS最接近预期的时间格式 3 秒。示例:-ss 00:00:02.895

  • 关于音频:
    MP4 通常有一个 AAC 音轨。请注意,由于您的输入音频是 MP3 格式,因此 FFmpeg 会自动将其重新编码为 AAC 格式。
  • 为避免音频格式重新编码:使用 -c:a copy 强制 MP3 (可以在上面的 -c:v copy 之后添加)。
  • 关于video - 如何为 mp4 容器的视频流指定偏移量和查找位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55147398/

    相关文章:

    video - ffmpeg - mpeg4 (H.264) 时间戳和图片质量

    Android:MediaCodec:在 Nexus 上生成的 480x480 视频效果不佳,而 640x640 效果很好

    ffmpeg - %configure - 未知选项 --build

    ffmpeg - 如何使用 avformat (ffmpeg) 写入 Icecast 挂载点?

    macos - VLC:如何创建正版MP4(不是M4V)文件?

    ios - 如何播放服务器上的在线视频

    php - 从 gif 转换的 mp4 未在 Web 浏览器中显示

    python-3.x - 如何使 cv2.videoCapture.read() 更快?

    streaming - mp4 视频未在现场播放

    c# - 无需重新编码即可切割 mp4 h264 视频文件