Zulko / moviepy

Video editing with Python
https://zulko.github.io/moviepy/
MIT License
12.07k stars 1.51k forks source link

Inconsistent duration for composite output #2148

Open ncugit-sec opened 3 months ago

ncugit-sec commented 3 months ago

The generated audio appears to be padded with the last few points of the source audio, even if no operations are performed on it.

Expected Behavior

CompositeAudioClip or CompositeVideoClip should generate output with consistent duration if no operation related to duration editting is made.

Actual Behavior

The duration of CompositeAudioClip generate a longer duration than input file.

Steps to Reproduce the Problem

https://github.com/Zulko/moviepy/assets/53465444/2a590af9-325a-45a3-a1bd-03c49d6ed57b

# using above audio file
from moviepy.editor import *

audio = AudioFileClip('random.mp4')
audio.write_audiofile('tmp.mp3')

Generated duration are longer than source audio.

$ ffprobe random.mp3 
Input #0, mp3, from 'random.mp3':
  Metadata:
    encoder         : Lavf58.76.100
  Duration: 00:00:05.07, start: 0.025057, bitrate: 128 kb/s
  Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 128 kb/s
    Metadata:
      encoder         : Lavc58.13
$ ffprobe tmp.mp3 
Input #0, mp3, from 'tmp.mp3':
  Metadata:
    encoder         : Lavf58.29.100
  Duration: 00:00:05.12, start: 0.025057, bitrate: 128 kb/s
  Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 128 kb/s
    Metadata:
      encoder         : Lavc58.54

Specifications

Cyberghost999 commented 2 months ago

It is due to incorrect reporting of audio length by ffmpeg. This causes moviepy to read frame outside of the audio which results in artifact being added towards the end of the audio. I fixed this issue by taking subclip of the audio file by audio_clip = audio_clip.subclip(0, audio_clip.duration - 0.05) or you can define the audio path while writing the final clip. You can also refer #1936 for alternate solutions.