From d640bb6ba336ae19b2d8411acd13ca9c9540638b Mon Sep 17 00:00:00 2001 From: Montana Flynn Date: Tue, 18 Oct 2016 12:33:05 -0700 Subject: [PATCH 1/4] Add main CLI function and library function for getting segment times --- main.py | 20 ++++++++++++++++ video2gif/__init__.py | 56 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..0450a58 --- /dev/null +++ b/main.py @@ -0,0 +1,20 @@ +# Import needed modules +import video2gif +import optparse +from moviepy.editor import VideoFileClip + +def parser(): + parser = optparse.OptionParser() + parser.add_option("-v", "--video", default="./videos/FrG4TEcSuRg.mp4", help="Which video to process") + parser.add_option("-d", "--duration", default=3, help="Duration of the segments", type="int") + parser.add_option("-t", "--top", default=5, help="How many top segments to get", type="int") + parser.add_option("-b", "--bottom", default=0, help="How many bottom segments to get", type="int") + return parser.parse_args() + +def main(): + args, opts = parser() + scored_segments = get_scored_segments(args.video, args.duration, args.top, args.bottom) + print(scored_segments) + +if __name__ == "__main__": + main() diff --git a/video2gif/__init__.py b/video2gif/__init__.py index d1aa19d..b1ffbfe 100644 --- a/video2gif/__init__.py +++ b/video2gif/__init__.py @@ -175,3 +175,59 @@ def generate_gifs(out_dir, segment2scores, video, video_id, top_k=6, bottom_k=0) nr -= 1 return good_gifs,bad_gifs + + +def generate_gif_times(video, segment2scores, top_k=6, bottom_k=0): + ''' + @param out_dir: directory where the GIFs are written to + @param segment2scores: a dict with segments (start frame, end frame) as keys and the segment score as value + @param video: a VideoFileClip object + @param video_id: the identifier of the video (used for naming the GIFs) + @return: + ''' + segment2scores = segment2scores.copy() + print("found segscors", len(segment2scores)) + + nr=0 + top_k=min(top_k, len(segment2scores)) + good_gifs=[] + for segment in sorted(segment2scores, key=lambda x: -segment2scores.get(x))[0:top_k]: + good_gifs.append((segment[0]/float(video.fps), segment[1]/float(video.fps),)) + nr += 1 + + bottom_k=min(bottom_k, len(segment2scores)) + bad_gifs=[] + nr=len(segment2scores) + for segment in sorted(segment2scores, key=segment2scores.get)[0:bottom_k]: + bad_gifs.append((segment[0]/float(video.fps), segment[1]/float(video.fps),)) + nr -= 1 + return good_gifs,bad_gifs + + +# Define function +def get_scored_segments( video_path, duration = 3, top_k = 5, bottom_k = 0 ): + ''' + @param video_path: video to run gif segment scoring on + @param duration: duration of segments + @param top_k: count of top gifs to return + @param bottom_k: count of bottom gifs to return + @return: (good_gifs, bad_gifs) + ''' + + # Get scoring function + score_function = video2gif.get_prediction_function() + + # Take the example video + video = VideoFileClip(video_path) + + # Build the segments + segments = [(start, int(start+video.fps*duration)) for start in range(0,int(video.duration*video.fps),int(video.fps*duration))] + + # Score the segments + scores=video2gif.get_scores(score_function, segments, video, stride=8) + + # Generate GIFs from the top scoring segments + good_gifs,bad_gifs = video2gif.generate_gif_times(video, scores, top_k, bottom_k) + + # Return the good and bad gifs + return good_gifs, bad_gifs From 7a5a8d0adffcee4d18a3270a3a07bdb1d87a8865 Mon Sep 17 00:00:00 2001 From: Montana Flynn Date: Tue, 18 Oct 2016 12:41:57 -0700 Subject: [PATCH 2/4] Rename video flag to source --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 0450a58..c8f0ad9 100644 --- a/main.py +++ b/main.py @@ -5,7 +5,7 @@ def parser(): parser = optparse.OptionParser() - parser.add_option("-v", "--video", default="./videos/FrG4TEcSuRg.mp4", help="Which video to process") + parser.add_option("-s", "--source", default="./videos/FrG4TEcSuRg.mp4", help="Which video to process") parser.add_option("-d", "--duration", default=3, help="Duration of the segments", type="int") parser.add_option("-t", "--top", default=5, help="How many top segments to get", type="int") parser.add_option("-b", "--bottom", default=0, help="How many bottom segments to get", type="int") From eed55881cce88b7bf08695ce78496169fc5cd70a Mon Sep 17 00:00:00 2001 From: Montana Flynn Date: Tue, 18 Oct 2016 12:48:46 -0700 Subject: [PATCH 3/4] Change list of tuples to list of dicts --- video2gif/__init__.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/video2gif/__init__.py b/video2gif/__init__.py index b1ffbfe..881dcf4 100644 --- a/video2gif/__init__.py +++ b/video2gif/__init__.py @@ -192,16 +192,19 @@ def generate_gif_times(video, segment2scores, top_k=6, bottom_k=0): top_k=min(top_k, len(segment2scores)) good_gifs=[] for segment in sorted(segment2scores, key=lambda x: -segment2scores.get(x))[0:top_k]: - good_gifs.append((segment[0]/float(video.fps), segment[1]/float(video.fps),)) + segment_times = dict(start=segment[0]/float(video.fps), end=segment[1]/float(video.fps)) + good_gifs.append(segment_times) nr += 1 bottom_k=min(bottom_k, len(segment2scores)) bad_gifs=[] nr=len(segment2scores) for segment in sorted(segment2scores, key=segment2scores.get)[0:bottom_k]: - bad_gifs.append((segment[0]/float(video.fps), segment[1]/float(video.fps),)) + segment_times = dict(start=segment[0]/float(video.fps), end=segment[1]/float(video.fps)) + bad_gifs.append(segment_times) nr -= 1 - return good_gifs,bad_gifs + + return good_gifs, bad_gifs # Define function @@ -211,7 +214,7 @@ def get_scored_segments( video_path, duration = 3, top_k = 5, bottom_k = 0 ): @param duration: duration of segments @param top_k: count of top gifs to return @param bottom_k: count of bottom gifs to return - @return: (good_gifs, bad_gifs) + @return: @object(good, bad) ''' # Get scoring function @@ -224,10 +227,10 @@ def get_scored_segments( video_path, duration = 3, top_k = 5, bottom_k = 0 ): segments = [(start, int(start+video.fps*duration)) for start in range(0,int(video.duration*video.fps),int(video.fps*duration))] # Score the segments - scores=video2gif.get_scores(score_function, segments, video, stride=8) + scores= get_scores(score_function, segments, video, stride=8) # Generate GIFs from the top scoring segments - good_gifs,bad_gifs = video2gif.generate_gif_times(video, scores, top_k, bottom_k) + good_gifs, bad_gifs = generate_gif_times(video, scores, top_k, bottom_k) # Return the good and bad gifs - return good_gifs, bad_gifs + return dict(good=good_gifs, bad=bad_gifs) From 2453d85181a64087c9b2629c48cb018f2aac99c9 Mon Sep 17 00:00:00 2001 From: Montana Flynn Date: Tue, 18 Oct 2016 12:49:10 -0700 Subject: [PATCH 4/4] Change output to JSON format --- main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index c8f0ad9..593588e 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ # Import needed modules import video2gif import optparse +import json from moviepy.editor import VideoFileClip def parser(): @@ -14,7 +15,7 @@ def parser(): def main(): args, opts = parser() scored_segments = get_scored_segments(args.video, args.duration, args.top, args.bottom) - print(scored_segments) + print(json.dumps(scored_segments, indent=4)) if __name__ == "__main__": main()