Answers.Body: <p>python script:</p>
  <pre><code>#!/usr/bin/python3
  import os,subprocess,math
  def get_length(filename):
    result=subprocess.run([&quot;ffprobe&quot;, &quot;-v&quot;, &quot;error&quot;, &quot;-show_entries&quot;,
                          &quot;format=duration&quot;, &quot;-of&quot;,
                          &quot;default=nw=1:nk=1&quot;, filename],
      stdout=subprocess.PIPE,
      stderr=subprocess.STDOUT)
    return float(result.stdout)
  
  LST=[]
  DIR=&quot;/mnt/public/upload/video/&quot;
  for f in os.listdir(DIR):
    if (f.endswith(&quot;.mp4&quot;)):
      print(f)
      LST.append(f)
  
  FLV=''
  FLA=''
  OFS=0
  XFD=0.25
  CNT=0
  
  INP=[]
  f=DIR+LST[0]
  INP.append(f' -i &quot;{f}&quot;')
  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 &lt; len(LST)-2:
      FLA+=&quot;;&quot;
    f=DIR+LST[i+1]
    INP.append(f' -i &quot;{f}&quot;')
  
  s='ffmpeg'
  for t in INP:
    s=s+t
  s=s+f' -filter_complex &quot;{FLV}{FLA}&quot; -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')
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-01T06:05:55.247Z
Answers.LastActivityDate: 2023-12-01T06:26:29.727Z
Answers.LastEditDate: 2023-12-01T06:26:29.727Z
Answers.UserDisplayName: Баяр Гончикжапов
Body: <p>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 <strong>two</strong> challenges:</p>
  <p><strong>Applying xfade to multiple videos:</strong> I want to extend this process to apply the xfade transition between all videos in the folder and save it as a single video.</p>
  <p><strong>Audio issue after transition:</strong> 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?</p>
  <p>This is the command i am currently using:</p>
  <pre><code>ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex &quot;xfade=transition=fade:duration=0.25:offset=4.5&quot; -c:v libx264 -c:a aac -strict experimental output.mp4
  </code></pre>
  
Tags: <ffmpeg>
Title: How do i add xfade transition to a batch of videos using ffmpeg?
UserDisplayName: Qasim Rao
ThreadText: # How do i add xfade transition to a batch of videos using ffmpeg?
  
  (Asked by: username_0 on 2023-11-29)
  
  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
  
  ```
  
  **Tags:** ffmpeg
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-12-01. Score: 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')
  
  ```
  
  
  ---
  
  