0

I'm currently trying to concatenate multiple videos in a folder using FFmpeg. I've successfully applied a xfade transition between two videos using the following command: However, I'm facing two challenges:

Applying xfade to multiple videos: I want to extend this process to apply the xfade transition between all videos in the folder and save it as a single video.

Audio issue after transition: While the visual transition works seamlessly, I've noticed that the audio is muted as soon as the transition finishes in the output video. Is there a modification I can make to the FFmpeg command to ensure that the audio remains synchronized with the video throughout the output?

This is the command i am currently using:

ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "xfade=transition=fade:duration=0.25:offset=4.5" -c:v libx264 -c:a aac -strict experimental output.mp4

1 Answer 1

0

python script:

#!/usr/bin/python3
import os,subprocess,math
def get_length(filename):
  result=subprocess.run(["ffprobe", "-v", "error", "-show_entries",
                        "format=duration", "-of",
                        "default=nw=1:nk=1", filename],
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT)
  return float(result.stdout)

LST=[]
DIR="/mnt/public/upload/video/"
for f in os.listdir(DIR):
  if (f.endswith(".mp4")):
    print(f)
    LST.append(f)

FLV=''
FLA=''
OFS=0
XFD=0.25
CNT=0

INP=[]
f=DIR+LST[0]
INP.append(f' -i "{f}"')
PDV='[0:v]'
PDA='[0:a]'

for i in range(len(LST)-1):
  OFS=OFS+get_length(f)-XFD
  FLV+=f'{PDV}'
  FLA+=f'{PDA}'
  CNT=i+1
  PDV=f'[v{CNT}]'
  PDA=f'[a{CNT}]'
  FLV+=f'[{CNT}:v]xfade=transition=pixelize:offset={OFS}:duration={XFD}{PDV};'
  FLA+=f'[{CNT}:a]acrossfade=d={XFD}{PDA}'
  if i < len(LST)-2:
    FLA+=";"
  f=DIR+LST[i+1]
  INP.append(f' -i "{f}"')

s='ffmpeg'
for t in INP:
  s=s+t
s=s+f' -filter_complex "{FLV}{FLA}" -map {PDV} -map {PDA} -c:v h264_nvenc -cq 18 -c:a aac -q:a 4 -map_metadata -1 /tmp/output.mp4 -y -hide_banner'
print(s)
os.system(s)
os.system(f'mpv /tmp/output.mp4')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.