Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eba8560

Browse files
committed
First commit
1 parent 82be898 commit eba8560

2 files changed

Lines changed: 95 additions & 0 deletions

File tree

sorter.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import os, shutil
2+
3+
dir = os.path.expanduser('~/Desktop/100MSDCF')
4+
setOffset = 0 # number for first set.
5+
files = os.listdir(dir)
6+
if len(files) == 0:
7+
print 'No files.'
8+
exit(0)
9+
10+
lastDate = None
11+
sets = []
12+
thisSet = []
13+
for file in sorted(files):
14+
filename = os.path.join(dir, file)
15+
if os.path.isdir(filename): continue
16+
if not '.JPG' in filename: continue
17+
# mtime is the only one that works, sadly the resolution is not high enough to detect when the camera buffer was full.
18+
thisDate = os.path.getmtime(filename)
19+
if lastDate:
20+
timeDeltaSec = thisDate - lastDate
21+
if timeDeltaSec < 3: # Apparently within a burst, some files will have a time difference of 2 seconds.
22+
thisSet.append(filename) # add file to this set; it was taken soon after the previous file.
23+
else:
24+
print 'Start of set: %s - %10.3f' % (file, timeDeltaSec)
25+
sets.append(thisSet) # store the set
26+
thisSet = [filename] # start a new one.
27+
else:
28+
thisSet.append(filename) # first time around; add file to set.
29+
lastDate = thisDate
30+
31+
if len(thisSet) > 0:
32+
# Trailing images got collected here. That's our last set.
33+
sets.append(thisSet)
34+
35+
for i in range(0, len(sets)):
36+
set = sets[i]
37+
setNum = i + setOffset
38+
print 'set %2d: %s to %s (%3d images)' % (setNum, os.path.basename(set[0]),
39+
os.path.basename(set[-1]), len(set))
40+
newDirName = os.path.join(dir, 'set_%02d' % setNum)
41+
os.mkdir(newDirName)
42+
for oldFile in set:
43+
# By putting a hyphen in the name, we can then use negative file numbers to encode sequences in reverse.
44+
newFile = os.path.join(newDirName, 'image-%s' % os.path.basename(oldFile)[len('DSC'):])
45+
print '\t%s --> %s' % (oldFile, newFile)
46+
shutil.move(oldFile, newFile)

video_maker.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

Comments
 (0)