|
| 1 | +import os, glob, math |
| 2 | + |
| 3 | +dir = os.path.expanduser('~/Desktop/100MSDCF') |
| 4 | + |
| 5 | +quality = 15 # MP4 quality; lower is better |
| 6 | +fps = 24 |
| 7 | +boomerangLengthSeconds = 15 # final video length, minimum. |
| 8 | +sets = sorted(glob.glob('%s/set_*' % dir)) |
| 9 | +for set in sets: |
| 10 | + if not os.path.isdir(set): continue |
| 11 | + files = sorted(glob.glob('%s/*.JPG' % set)) |
| 12 | + setName = os.path.basename(set) |
| 13 | + print setName |
| 14 | + # Start at the second frame, because the reverse clip will end at the first frame. |
| 15 | + startNum = int(os.path.basename(files[1])[len('image-'):-len('.JPG')]) |
| 16 | + videoFileForward = os.path.join(dir, '%s_forward.mp4' % setName) |
| 17 | + ffmpeg = "ffmpeg -y -start_number %d -r %d -i '%s/image-%%05d.JPG' -vf scale=1920:1280 -vcodec libx264 " \ |
| 18 | + "-crf %d -pix_fmt yuv420p %s" % (startNum, fps, set, quality, videoFileForward) |
| 19 | + print 'Forward' |
| 20 | + os.system(ffmpeg) |
| 21 | + |
| 22 | + # Reverse clip starts at the second to last frame, because the forward clip ends at the last frame. |
| 23 | + startNum = int(os.path.basename(files[-2])[len('image'):-len('.JPG')]) |
| 24 | + videoFileReverse = os.path.join(dir, '%s_reverse.mp4' % setName) |
| 25 | + ffmpeg = "ffmpeg -y -start_number %d -r %d -i '%s/image%%05d.JPG' -vf scale=1920:1280 -vcodec libx264 " \ |
| 26 | + "-crf %d -pix_fmt yuv420p %s" % (startNum, fps, set, quality, videoFileReverse) |
| 27 | + print 'Reverse' |
| 28 | + os.system(ffmpeg) |
| 29 | + |
| 30 | + # How long is our sequence? |
| 31 | + numFrames = len(files) |
| 32 | + lengthSeconds = numFrames / float(fps) * 2 # 2 for forward and reverse |
| 33 | + numPairs = int(math.ceil(boomerangLengthSeconds / lengthSeconds)) |
| 34 | + |
| 35 | + print 'Pair is %.2f seconds long. Will require %d pairs to hit %d seconds.' % ( |
| 36 | + lengthSeconds, numPairs, boomerangLengthSeconds |
| 37 | + ) |
| 38 | + # Make the concat list. |
| 39 | + with open('/tmp/concat.txt', 'w') as f: |
| 40 | + for i in range(0, numPairs): |
| 41 | + f.write('file %s\n' % videoFileForward) |
| 42 | + f.write('file %s\n' % videoFileReverse) |
| 43 | + |
| 44 | + videoFile = os.path.join(dir, '%s_boomerang.mp4' % setName) |
| 45 | + |
| 46 | + ffmpeg = "ffmpeg -y -safe 0 -f concat -i '/tmp/concat.txt' -c copy %s" % (videoFile) |
| 47 | + print 'Concat' |
| 48 | + print ffmpeg |
| 49 | + os.system(ffmpeg) |
0 commit comments