K
K
kilrea2020-01-29 22:00:45
fmpeg
kilrea, 2020-01-29 22:00:45

How to merge multiple videos with ffmpeg without quality loss?

I want to make a video from pictures, then glue a few more video recordings with it.
Code for creating slideshow:

# для каждой картинки создаем видео 
# длительность всего слайдшоу равна dur

dur = 12

for num, pic in enumerate(os.listdir(path)):
  os.system('ffmpeg -loop 1 -i %s -r %s -vf scale=%s:%s -t %s -y %s.ts' % (path+pic, fps, w,h, dur/len(os.listdir(path)), num))

# соединяем временные файлы со слайдами в один
slides = [i for i in os.listdir('.')]
command = 'ffmpeg -i concat:"' + '|'.join(slides) + '" -vf scale={}:{} -framerate {} -y '.format(w,h,fps) + 'slideshow.mp4'
os.system(command)


After that, I glue it with the rest of the video:
videos = ['vids/begin.mp4', 'slideshow.mp4', 'vids.mp4']
command = 'ffmpeg -i concat:"' + '|'.join(videos) + '" -vf scale={}:{} -framerate {} -y '.format(w,h,fps) + 'result.mp4'
os.system(command)


The slideshow.mp4 file is in good quality, begin.mp4 and end.mp4 are also in good quality, but the final result.mp4 file is in poor quality. What is the reason and how to merge video without losing quality?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Taus, 2020-01-30
@Taus

One good way would be to use unfinalized media containers for intermediate files, as you do with .ts. They can be glued together without losing the quality of the final file. And wrap the final result in mp4. If you already have files *.mp4with the correct codec to merge, then convert them to ts:

ffmpeg -i INPUT.mp4 -codec copy -bsf:v h264_mp4toannexb OUTPUT.ts

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question