E
E
Evgeniy Golovin2018-08-27 14:18:16
PHP
Evgeniy Golovin, 2018-08-27 14:18:16

ffmpeg loop video how?

The task is to glue 5 sec mp4 + minute mp3
The output naturally turns out 5 sec mp4 with music. I don’t understand how to loop a video but at the same time that mp3 does not loop.
I made a separate mp4 by looping it like this
printf \"file '%s'\n\" tmp.mp4 >> tmp/list.txt
. Next, the gluing itself

ffmpeg -f concat -i tmp/list.txt -c copy tmp/loop.mp4 -y

But then I glue mp3 and mp4
ffmpeg -i "loop.mp4" -i "file.mp3" -shortest "file_audio_video.mp4"

The problem is that a one-minute video connects approximately 10 seconds
. Question: how can you connect mp3 + mp4 in other ways by looping mp4?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Sokolov, 2018-08-27
@sergiks

Perhaps mp4 does not allow simple file concatenation, so when merging it is decoded and compressed again.
It's faster to decompress the video once into .tsa format that you can then simply glue together.
I did something similar to assemble mp3 + pictures into a static video.
More or less like this:

bash script
#  из mp4 сделать ts
TMPVIDEO="tmp_video.ts"
ffmpeg  \
  -i "short.mp4"  \
  -bsf:v h264_mp4toannexb  \
  -an  \
  -f mpegts  \
  -y -hide_banner  \
  "$TMPVIDEO"

# до этого определить, сколько раз повторить фрагмент,
# чтобы покрыть длину mp3
REPEATS=5
LONGVIDEO="long_video.ts"
N=1
while [ $N -le $REPEATS ]; do
  ffmpeg -i "concat:$TMPVIDEO|$TMPVIDEO" \
      -c:v copy \
      -y -hide_banner  \
      "$LONGVIDEO"

  local STATUS=$? ; if [ $STATUS -ne 0 ]; then return $STATUS; fi

  rm -f "$TMPVIDEO"
  mv "$LONGVIDEO" "$TMPVIDEO"
  local N=$[$N << 1]
  echo "N: $N"
done
# получили длинное видео с запасом

# подрезать длину, чтобы точно соответствовала
FFMPEG -i "$TMPVIDEO" \
  -c:v copy \
  -t $MUSIC_LENGTH \
  -y -hide_banner  \
  "$LONGVIDEO"

Then it remains to assemble a common file from audio and video. The mp4 audio is in AAC format. For speed, you can pre-compress mp3 to aac, at the same time measuring the exact duration.

V
Vapaamies, 2018-08-28
@vapaamies

ffmpeg has a parameter -loop, I used it in a comment . I tested that example before the answer, but now I'm too lazy. I'm just giving a hint.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question