Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ffmpeg 4: Using the stream_loop parameter to loop the audio during a video ends up with an infinite loop

Summary

  1. Context
  2. The software I use
  3. The problem
  4. Results
    4.1. Actual Results

    4.2. Expected Results

  5. What did I try to fix the bug?

  6. How to reproduce this bug: minimal and testable example with the provided required data

  7. The question

  8. Sources


Context

I would want to set an audio WAV as the background sound of a video WEBM. The video can be shorter or longer than the audio. At the moment I add the audio over the video, I don't know the length of both streams. The audio must repeat until the video ends (the audio can be truncated if the video ends before the end of the last repetition of the audio).

The software I use

I use ffmpeg version 4.2.2-1ubuntu1~18.04.sav0.

The problem

ffmpeg seems to enter in an infinite loop when it proccesses in order to mix the audio and the video. Also, the length of the currently-generating-output-file (which contains both video and audio) is equal to the length of the audio, instead of the length of the video.

The problem seems to be triggered by this command line:

ffmpeg -i directory_1/video.webm -stream_loop -1 -fflags +shortest -max_interleave_delta 50000 -i directory_2/audio.wav directory_3/video_and_audio.webm

Results

Actual Results

Three things:

  1. The infinite loop of the ffmpeg process: I must manually stop the ffmpeg process

  2. The output video file with music (which is currently generating but output anyway): it contains both audio and video. But the length of the output file is equal to the length of the audio, instead of the length of the video.

  3. The following output logs:

ffmpeg version 4.2.2-1ubuntu1~18.04.sav0 Copyright (c) 2000-2019 the FFmpeg developers built with gcc 7 (Ubuntu 7.5.0-3ubuntu1~18.04)
configuration: --prefix=/usr --extra-version='1ubuntu1~18.04.sav0' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-nvenc --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared 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 libavresample 4. 0. 0 / 4. 0. 0 libswscale 5. 5.100 / 5. 5.100 libswresample 3. 5.100 / 3. 5.100 libpostproc 55. 5.100 / 55. 5.100 Input #0, matroska,webm, from 'youtubed/my_youtube_video.webm': Metadata: encoder : Chrome Duration: N/A, start: 0.000000, bitrate: N/A Stream #0:0(eng): Video: vp8, yuv420p(progressive), 3200x1608, SAR 1:1 DAR 400:201, 1k tbr, 1k tbn, 1k tbc (default) Metadata: alpha_mode : 1 Guessed Channel Layout for Input Stream #1.0 : stereo Input #1, wav, from 'tmp_music/original_music.wav':
Duration: 00:00:11.78, bitrate: 1411 kb/s Stream #1:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s Stream mapping: Stream #0:0 -> #0:0 (vp8 (native) -> vp9 (libvpx-vp9)) Stream #1:0 -> #0:1 (pcm_s16le (native) -> opus (libopus)) Press [q] to stop, [?] for help [libvpx-vp9 @ 0x5645268aed80] v1.8.2 [libopus @ 0x5645268b09c0] No bit rate set. Defaulting to 96000 bps. Output #0, webm, to 'youtubed/my_youtube_video_with_music.webm': Metadata: encoder : Lavf58.29.100 Stream #0:0(eng): Video: vp9 (libvpx-vp9), yuv420p(progressive), 3200x1608 [SAR 1:1 DAR 400:201], q=-1--1, 200 kb/s, 1k fps, 1k tbn, 1k tbc (default) Metadata: alpha_mode : 1 encoder : Lavc58.54.100 libvpx-vp9 Side data: cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1 Stream #0:1: Audio: opus (libopus), 48000 Hz, stereo, s16, 96 kb/s Metadata: encoder : Lavc58.54.100 libopus

Expected Results

  1. No infinite loop during the ffmpeg process

  2. Concerning the output logs, I don't know what it should look.

  3. The output file with the audio and the video should:

    3.1. If the video is longer than the audio, then the audio is repeated until it exactly fits the video. The audio can be truncated.

    3.2. If the video is shorter than the audio, then the audio is truncated and exactly fits the video.

    3.3. If both video and audio are of the same length, then the audio exactly fits the video.

How to reproduce this bug? (+ required data)

  1. Download the following files (resp. audio and video) (I must refresh these download links every 24 hours):

    1.1. https://a.uguu.se/dmgsmItjJMDq_audio.wav

    1.2. https://a.uguu.se/w3qHDlGq6mOW_video.webm

  2. Move them into the directory/directories of your choice.

  3. Open your CLI, move to the adequat directory and copy/paste/execute the instruction given in Part. The Problem (don't forget to eventually modify this instruction by indicating the adequat directories, according to step 2.).

  4. You'll face my problem.

What did I try to fix the bug?

Nothing, since I don't even understand why the bug occures.

The question

How to correct my command in order to mix these audio and video streams without any infinite loop during the ffmpeg process, keeping in mind that I don't know their length, and that audio must be repeated in order to fit the video, even if audio must be truncated (in the case of the last repetition of the audio file must be truncated because the video stream has just ended)?

Sources

The source is the command line you can find in Part. The problem.

like image 918
JarsOfJam-Scheduler Avatar asked Jan 31 '26 05:01

JarsOfJam-Scheduler


1 Answers

The placement of some of your options is wrong. All of the shortest related options belong in front of the output.

ffmpeg -i directory_1/video.webm -stream_loop -1 -i directory_2/audio.wav -c:v copy -shortest -fflags +shortest -max_interleave_delta 100M directory_3/video_and_audio.webm

There's no need to transcode the video unless you wish to.

like image 145
Gyan Avatar answered Feb 03 '26 08:02

Gyan