| 1 | from PIL import Image, ImageDraw
|
|---|
| 2 | import os
|
|---|
| 3 | import shutil
|
|---|
| 4 | f = "/content/frames/"
|
|---|
| 5 | outFile = "/content/video.mp4"
|
|---|
| 6 | outFile_concat = "/content/video_concat.mp4"
|
|---|
| 7 |
|
|---|
| 8 | if os.path.exists(f):
|
|---|
| 9 | shutil.rmtree(f)
|
|---|
| 10 |
|
|---|
| 11 | os.makedirs(f)
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 | #Final video should always have around 1 sec. duration
|
|---|
| 15 | #Setting fps = 25 should not remove any frames from final output.
|
|---|
| 16 | #Setting a high fps like 100 will remove 3 in 4 frames and encode it as 100fps
|
|---|
| 17 | ffps = 120
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 | dur = 1 / ffps
|
|---|
| 21 |
|
|---|
| 22 | endTime = 0
|
|---|
| 23 | strFile = ""
|
|---|
| 24 |
|
|---|
| 25 | for i in range(1, ffps):
|
|---|
| 26 | img = Image.new('RGB', (100, 30), color = (73, 109, 137))
|
|---|
| 27 | d = ImageDraw.Draw(img)
|
|---|
| 28 | d.text((10,10), str(i).zfill(10), fill=(255,255,0))
|
|---|
| 29 | name = str(i).zfill(10) +'.png'
|
|---|
| 30 | img.save(f + "/"+ name)
|
|---|
| 31 | strFile += "file '"+ name+"'\nduration "+ "{:.5f}".format(dur) + "\n"
|
|---|
| 32 | endTime += dur
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 | filename = f + "/my_file.txt"
|
|---|
| 36 | myfile = open(filename, 'w')
|
|---|
| 37 | myfile.write(strFile)
|
|---|
| 38 | myfile.close()
|
|---|
| 39 |
|
|---|
| 40 | print("Final durations should be: " +str(endTime))
|
|---|
| 41 |
|
|---|
| 42 | # -r before "-f concat" : Override all durations from file. Its just like using a static fps
|
|---|
| 43 | # -vf "fps=X" : Fake final fps, it will keep very few frames.
|
|---|
| 44 | # -r after -i "{txtPath}" : It appear to work, but its get glitchy after 60fps.
|
|---|
| 45 | # -filter:v "fps={fps},setpts={speed}*PTS" : It encode correctly after the video is encoded at 25fps. So if there is a duration smaller than 25fps, it will delete those frames.
|
|---|
| 46 |
|
|---|
| 47 | speed = 1
|
|---|
| 48 |
|
|---|
| 49 | os.system('ffmpeg -y -framerate {fps} -i "{input}%10d.png" {muxer} {fmt} "{out}"'.format(fps=ffps, input=f, muxer="-c:v libx264 -crf 1", fmt="-pix_fmt yuv420p", out=outFile))
|
|---|
| 50 | os.system('ffmpeg -y -f concat -i "{txtPath}" {muxer} {fmt} -r {fps} "{out}"'.format(txtPath=filename, speed=speed, fps=ffps, muxer="-c:v libx264 -crf 1", fmt="-pix_fmt yuv420p", out=outFile_concat))
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | t1 = "/content/t1/"
|
|---|
| 54 | t2 = "/content/t2/"
|
|---|
| 55 |
|
|---|
| 56 | if os.path.exists(t1):
|
|---|
| 57 | shutil.rmtree(t1)
|
|---|
| 58 | if os.path.exists(t2):
|
|---|
| 59 | shutil.rmtree(t2)
|
|---|
| 60 |
|
|---|
| 61 | os.makedirs(t1)
|
|---|
| 62 | os.makedirs(t2)
|
|---|
| 63 |
|
|---|
| 64 | os.system('ffmpeg -i "{}" -vsync 0 -vf mpdecimate -qscale:v 1 "{}%10d.png"'.format(outFile, t1))
|
|---|
| 65 | os.system('ffmpeg -i "{}" -vsync 0 -vf mpdecimate -qscale:v 1 "{}%10d.png"'.format(outFile_concat, t2))
|
|---|
| 66 |
|
|---|
| 67 | #Show total frames from each encode.
|
|---|
| 68 | print("Frames in regular: " + str(len(os.listdir(t1))))
|
|---|
| 69 | print("Frames in concat: " + str(len(os.listdir(t2))))
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 | #Output data of each video
|
|---|
| 73 | !ffmpeg -i {outFile}
|
|---|
| 74 | !ffmpeg -i {outFile_concat}
|
|---|