Transitions : python script for fade-in & fade-out with two slides - 2020
In the previous chapter, we managed to create the fade-in fade-out effects for one slide. Now, we may want to know how to make a slide show with the fade effect. First, in this section, we'll combine two slides together and generate the simplest slide show.
Regarding merging/joining/concatenating video files, we can have detailed info from How to concatenate (join, merge) media files.
We'll deal with this concatenation in later chapters.
We have following two files from I-frame:
yi109.png and yi112.png.The code below (fade.py) is to make two slides with fade-in and fade-out effect. The two slides are not concatenated yet. The steps of fade effects are described in the previous chapter(Transitions : fade-in & fade-out for 1 slide)
# fade.py # This code creates two slides with fade-in and fade-out import os import sys import subprocess import psutil class mytool: def __init__(self): self.count = 0 self.copied_file = ['./in001.png', './in002.png'] def add_fade_effect(self, filename): # make two frames : at the beginning and at the end # this is done by copying one I-Frame for a slide # then adding fades at both ends for f in self.copied_file: cmd = map(lambda x: '%s' %x, ['cp', filename, f]) subprocess.call(cmd) # make normal slide # ffmpeg -r 1/5 -i in%03d.png -c:v libx264 -r 30 -y -pix_fmt yuv420p slide.mp4 in_framerate = 1./5 out_framerate = 30 cmd = ['ffmpeg', '-r', in_framerate, '-i','in%03d.png','-c:v','libx264', '-r', out_framerate, '-y','-pix_fmt','yuv420p','slide.mp4'] cmd = map(lambda x: '%s' %x, cmd) subprocess.call(cmd) # add fade-in effect - from 0th to 30th frame #ffmpeg -i slide.mp4 -y -vf fade=in:0:30 slide_fade_in.mp4 cmd = ['ffmpeg', '-i','slide.mp4','-y','-vf','fade=in:0:30','slide_fade_in.mp4'] subprocess.call(cmd) # add fade-out effect to the slide that has fade-in effect already : 30 frames starting from 120th #ffmpeg -i slide_fade_in.mp4 -y -vf fade=out:120:30 slide_fade_in_out.mp4 cmd = ['ffmpeg', '-i','slide_fade_in.mp4','-y','-vf','fade=out:120:30', 'slide_fade_in_out.mp4'] subprocess.call(cmd) # rename the output to 'final#.mp4' slide_name = 'final'+str(self.count)+'.mp4' cmd = map(lambda x: '%s' %x, ['cp', 'slide_fade_in_out.mp4', slide_name]) subprocess.call(cmd) # remove the copied files for f in self.copied_file: cmd = map(lambda x: '%s' %x, ['rm','-f', f]) subprocess.call(cmd) self.count += 1 # get the list of file and feed one file at a time to "add_fade_effect()" def file_list(self, dir): basedir = dir subdir = [] slides = [] for item in os.listdir(dir): fullpath = os.path.join(basedir, item) if os.path.isdir(fullpath): subdir.append(fullpath) else: if item.endswith(".png"): slides.append(fullpath) for slide in slides: self.add_fade_effect(slide) if __name__ == "__main__": if len(sys.argv) <= 1: path = '.' else: path = sys.argv[1] m = mytool() m.file_list(path)
The code generated two final slides (final0.mp4 and final1.mp4) with the fade effects.
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization