|
| 1 | +from moviepy.editor import VideoFileClip |
| 2 | +from tkinter.filedialog import * |
| 3 | + |
| 4 | + |
| 5 | +# ---------------function to get the time frames for the GIF---------- |
| 6 | +def gif_time(): |
| 7 | + print( |
| 8 | + "\nEnter time in the format: minutes seconds (Example: 2 2.5 --> 2 minutes 2.5 seconds)" |
| 9 | + ) |
| 10 | + start = list(map(float, input("GIF start time: --> ").split())) |
| 11 | + end = list(map(float, input("GIF end time: --> ").split())) |
| 12 | + if len(start) == 1: |
| 13 | + start.append(0) |
| 14 | + if len(end) == 1: |
| 15 | + end.append(0) |
| 16 | + |
| 17 | + return start, end |
| 18 | + |
| 19 | + |
| 20 | +try: |
| 21 | + video = askopenfilename() |
| 22 | + |
| 23 | + while not video: |
| 24 | + video = askopenfilename() |
| 25 | + |
| 26 | + # ---------calls the function------------ |
| 27 | + clip_start, clip_end = gif_time() |
| 28 | + |
| 29 | + # -------condition to check if start time in minutes > end time------ |
| 30 | + while clip_start[0] > clip_end[0]: |
| 31 | + print("\nYou have entered start time > end time, which is not supported\n") |
| 32 | + clip_start, clip_end = gif_time() |
| 33 | + |
| 34 | + # -------2nd condition to check if start time in seconds> end time------ |
| 35 | + while clip_start[0] == clip_end[0] and clip_start[1] > clip_end[1]: |
| 36 | + print("\nYou have entered start time > end time, which is not supported\n") |
| 37 | + clip_start, clip_end = gif_time() |
| 38 | + |
| 39 | + # -------condition to check if start time is equal to end time------ |
| 40 | + while "".join(str(clip_start)) == "".join(str(clip_end)): |
| 41 | + print("\nYou have entered start time == end time, which is not supported\n") |
| 42 | + clip_start, clip_end = gif_time() |
| 43 | + |
| 44 | + # ----------Get the size of the GIF image from user---------------- |
| 45 | + resize = float(input("\nGIF size ( > 0 and < 1 preferred )(Example: 0.2): --> ")) |
| 46 | + |
| 47 | + print("\nProcessing...") |
| 48 | + |
| 49 | + # ----------convert video to GIF with the given time frames---------------- |
| 50 | + clip = ( |
| 51 | + VideoFileClip(video) |
| 52 | + .subclip((clip_start[0], clip_start[1]), (clip_end[0], clip_end[1])) |
| 53 | + .resize(resize) |
| 54 | + ) |
| 55 | + |
| 56 | + clip.write_gif("mygif.gif") |
| 57 | + |
| 58 | + print( |
| 59 | + """ |
| 60 | + *************************************** |
| 61 | + mygif.gif created (check the .gif file) |
| 62 | + ***************************************""" |
| 63 | + ) |
| 64 | + |
| 65 | +# -------------print exceptions----------------- |
| 66 | +except Exception as e: |
| 67 | + print("\nError occurred: ", e) |
| 68 | + |
| 69 | +# -------------Keyboard exception----------------- |
| 70 | +except KeyboardInterrupt: |
| 71 | + print("\nError occurred: KeyboardInterrupt") |
0 commit comments