def create_split(video_file_path): part_duration = 0 start_time = [0] end_time = [] old_filename = video_file_path.rsplit('\\', 1)[-1] old_extension = os.path.splitext(video_file_path)[1] new_filename = old_filename.replace(old_extension, '.mp4') video = (mp.VideoFileClip(video_file_path)) color = style.bcolors() while True: try: video_parts = int( input( f'Enter amount of video parts {color.OKBLUE}(e.g. 2){color.ENDC}: ' )) break except ValueError: os.system('cls') print(f'{color.FAIL}Input is invalid - numbers only!{color.ENDC}') continue time_per_part = video.duration / video_parts for _ in range(int(video_parts)): part_duration += time_per_part start_time.append(round(part_duration, 2)) end_time.append(round(part_duration, 2)) # the last value in the start_time array gets deleted because it's only needed for the end_time array del start_time[-1] for part in range(int(video_parts)): video = (mp.VideoFileClip(video_file_path).subclip( (start_time[int(part)]), (end_time[int(part)]))) while True: if os.path.isfile(f'assets/videos/part-{part + 1}-{new_filename}'): overwrite = input( f'File \'assets/videos/part-{part + 1}-{new_filename}\' already exists. Overwrite ? [y/N] ' ) if overwrite.upper() == 'Y': video.write_videofile( f'assets/videos/part-{part + 1}-{new_filename}') print(f'{color.OKGREEN}Overwriting - done{color.ENDC}') break elif overwrite.upper() == 'N': print(f'{color.FAIL}Not overwriting - exiting{color.ENDC}') break else: os.system('cls') print(f'{color.FAIL}Invalid input!{color.ENDC}') continue else: video.write_videofile( f'assets/videos/part-{part + 1}-{new_filename}') break # close the video to prevent 'OSError: [WinError 6] The handle is invalid' video.reader.close() video.audio.reader.close_proc()
def menu_option(): menu_numbers = list(range(1, 10)) color = style.bcolors() select_option = input(f''' ---{color.HEADER}MENU{color.ENDC}---------------------------------------- | Created by Yassin | | https://github.com/Y4SSIN/video-editor | ------------------------------------------------ ---{color.HEADER}VIDEO OPTIONS{color.ENDC}------------------------------- | 1. Create a gif out of the video | | 2. Add a watermark to the video | | 3. Export video frames to images | | 4. Split video into multiple parts | | 5. Concatenate videos | | 6. Convert video extension | ------------------------------------------------ ---{color.HEADER}AUDIO OPTIONS{color.ENDC}------------------------------- | 7. Increase or decrease audio volume | | 8. Replace audio | | 9. Export audio to mp3 | ----------------------------------------------- Enter preferred option {color.OKBLUE}(e.g. 1){color.ENDC}: ''') if select_option in str(menu_numbers): validate.check_path(select_option) else: os.system('cls') print(f'{color.FAIL}Select one of the options from the menu!{color.ENDC}') menu_option()
def create_frames(video_file_path): frame_time = 0 frame_number = 0 old_filename = video_file_path.rsplit('\\', 1)[-1] old_extension = os.path.splitext(video_file_path)[1] new_filename = old_filename.replace(old_extension, '.jpg') video = (mp.VideoFileClip(video_file_path)) color = style.bcolors() for frame in video.iter_frames(): # Create an image from a frame every 1/10 of a second. frame_time += 0.10 frame_number += 1 while True: if os.path.isfile(f'assets/frames/{frame_number}-{new_filename}'): overwrite = input( f'File \'assets/frames/{frame_number}-{new_filename}\' already exists. Overwrite ? [y/N] ' ) if overwrite.upper() == 'Y': video.save_frame( f'assets/frames/{frame_number}-{new_filename}', t=frame_time) print(f'{color.OKGREEN}Overwriting - done{color.ENDC}') print('Creating image from frame ' + str(round(frame_time, 2))) break elif overwrite.upper() == 'N': print(f'{color.FAIL}Not overwriting - exiting{color.ENDC}') break else: os.system('cls') print(f'{color.FAIL}Invalid input!{color.ENDC}') continue else: video.save_frame( f'assets/frames/{frame_number}-{new_filename}', t=frame_time) print('Creating image from frame ' + str(round(frame_time, 2))) break
def create_convertion(video_file_path): extension_support = ['mp4', 'mkv', 'mov'] old_filename = video_file_path.rsplit('\\', 1)[-1] old_extension = os.path.splitext(video_file_path)[1] new_filename = old_filename.replace(old_extension, '') color = style.bcolors() while True: new_extension = input(f''' Currently supported extensions mp4\nmkv\nmov Enter preferred extension {color.OKBLUE}(e.g. mp4){color.ENDC}: ''') if new_extension in extension_support: os.system( f'ffmpeg -i {video_file_path} -codec copy assets/videos/{new_filename}.{new_extension}' ) break else: os.system('cls') print( f'{color.FAIL}Choose one of the supported extensions!{color.ENDC}' ) continue
def create_watermark(video_file_path, watermark_file_path): size_support = ['small', 'medium', 'large'] position_support = ['bottom-right', 'bottom-left', 'top-right', 'top-left'] old_filename = video_file_path.rsplit('\\', 1)[-1] old_extension = os.path.splitext(video_file_path)[1] new_filename = old_filename.replace(old_extension, '.mp4') color = style.bcolors() while True: watermark_position = input(f''' Currently supported positions bottom-right\nbottom-left\ntop-right\ntop-left Enter preferred position {color.OKBLUE}(e.g. bottom-right){color.ENDC}: ''') if watermark_position in position_support: if watermark_position == 'bottom-right': first_position = 'right' second_position = 'bottom' elif watermark_position == 'bottom-left': first_position = 'left' second_position = 'bottom' elif watermark_position == 'top-right': first_position = 'right' second_position = 'top' elif watermark_position == 'top-left': first_position = 'left' second_position = 'top' break else: os.system('cls') print( f'{color.FAIL}Choose one of the supported positions!{color.ENDC}' ) continue while True: watermark_size = input(f''' Currently supported sizes small\nmedium\nlarge Enter preferred size {color.OKBLUE}(e.g. small){color.ENDC}: ''') if watermark_size in size_support: if watermark_size == 'small': watermark_size = 0.3 elif watermark_size == 'medium': watermark_size = 0.5 elif watermark_size == 'large': watermark_size = 0.7 break else: os.system('cls') print( f'{color.FAIL}Choose one of the supported sizes!{color.ENDC}') continue video = mp.VideoFileClip(video_file_path) watermark = (mp.ImageClip(watermark_file_path).set_duration( video.duration).resize(watermark_size).margin(left=8, right=8, top=8, bottom=8, opacity=0).set_pos( (first_position, second_position))) final_video = mp.CompositeVideoClip([video, watermark]) while True: if os.path.isfile(f'assets/videos/watermarked-{new_filename}'): overwrite = input( f'File \'assets/videos/watermarkd-{new_filename}\' already exists. Overwrite ? [y/N] ' ) if overwrite.upper() == 'Y': final_video.write_videofile( f'assets/videos/watermarked-{new_filename}') print(f'{color.OKGREEN}Overwriting - done{color.ENDC}') break elif overwrite.upper() == 'N': print(f'{color.FAIL}Not overwriting - exiting{color.ENDC}') break else: os.system('cls') print(f'{color.FAIL}Invalid input!{color.ENDC}') continue else: # quality can be raised by using the bitrate parameter e.g. bitrate="20000k" final_video.write_videofile( f'assets/videos/watermarked-{new_filename}') break
def create_gif(video_file_path): size_support = ['small', 'medium', 'large'] type_support = ['normal', 'loop'] old_filename = video_file_path.rsplit('\\', 1)[-1] old_extension = os.path.splitext(video_file_path)[1] new_filename = old_filename.replace(old_extension, '.gif') video = (mp.VideoFileClip(video_file_path)) color = style.bcolors() while True: try: start_time = int(input('Enter start time (seconds): ')) if start_time < 0 or start_time > int(video.duration): os.system('cls') print( f'{color.FAIL}Start time is out of video length range!{color.ENDC}' ) continue break except ValueError: os.system('cls') print(f'{color.FAIL}Input is invalid - numbers only!{color.ENDC}') continue while True: try: end_time = int(input('Enter end time (seconds): ')) if end_time < 0 or end_time > int(video.duration): os.system('cls') print( f'{color.FAIL}End time is out of video length range!{color.ENDC}' ) continue break except ValueError: os.system('cls') print(f'{color.FAIL}Input is invalid - numbers only!{color.ENDC}') continue while True: gif_size = input(f''' Currently supported sizes small\nmedium\nlarge Enter preferred size {color.OKBLUE}(e.g. small){color.ENDC}: ''') if gif_size in size_support: if gif_size == 'small': gif_size = 0.3 elif gif_size == 'medium': gif_size = 0.5 elif gif_size == 'large': gif_size = 0.7 break else: os.system('cls') print( f'{color.FAIL}Choose one of the supported sizes!{color.ENDC}') continue while True: gif_type = input(f''' Currently supported types normal\nloop Enter preferred type {color.OKBLUE}(e.g. normal){color.ENDC}: ''') if gif_type in type_support: if gif_type == 'normal': video = (mp.VideoFileClip(video_file_path).subclip( start_time, end_time).resize(gif_size)) new_filename = 'normal-' + new_filename elif gif_type == 'loop': video = (mp.VideoFileClip(video_file_path).subclip( start_time, end_time).resize(gif_size).fx(time_symetrize)) new_filename = 'loop-' + new_filename break else: os.system('cls') print( f'{color.FAIL}Choose one of the supported types!{color.ENDC}') continue final_gif = mp.CompositeVideoClip([video]) while True: if os.path.isfile(f'assets/gifs/{new_filename}'): overwrite = input( f'File \'assets/gifs/{new_filename}\' already exists. Overwrite ? [y/N] ' ) if overwrite.upper() == 'Y': final_gif.write_gif(f'assets/gifs/{new_filename}') print(f'{color.OKGREEN}Overwriting - done{color.ENDC}') break elif overwrite.upper() == 'N': print(f'{color.FAIL}Not overwriting - exiting{color.ENDC}') break else: os.system('cls') print(f'{color.FAIL}Invalid input!{color.ENDC}') continue else: final_gif.write_gif(f'assets/gifs/{new_filename}') break
def create_concatenation(): videos = [] all_video_file_paths = [] all_filenames = [] color = style.bcolors() while True: try: video_amount = int( input( f'Enter amount of videos {color.OKBLUE}(e.g. 2){color.ENDC}: ' )) break except ValueError: os.system('cls') print(f'{color.FAIL}Input is invalid - numbers only!{color.ENDC}') continue number = 0 for _ in range(int(video_amount)): number += 1 while True: video_file_path = input( f'Enter full path to video file {str(number)} {color.OKBLUE}(e.g. C:/Users/JohnDoe/Videos/example.mp4){color.ENDC}: ' ) if os.path.exists(video_file_path): if video_file_path.lower().endswith(('.mp4', '.mkv', '.mov')): all_video_file_paths.append(video_file_path) videos.append(mp.VideoFileClip(video_file_path)) print( f'{color.OKGREEN}Video file has been found!{color.ENDC}\n' ) break else: os.system('cls') print( f'{color.FAIL}File isn\'t a video extension! (e.g. mp4 mkv mov){color.ENDC}' ) continue else: os.system('cls') print( f'{color.FAIL}File doesn\'t exist in this directory!{color.ENDC}' ) continue for x in all_video_file_paths: old_filename = x.rsplit('\\', 1)[-1] old_extension = os.path.splitext(video_file_path)[1] new_filename = old_filename.replace(old_extension, '') all_filenames.append(new_filename) merged_filename = '-'.join(all_filenames) + '.mp4' final_clip = mp.concatenate_videoclips(videos, method='compose') while True: if os.path.isfile(f'assets/videos/merged-{merged_filename}'): overwrite = input( f'File \'assets/videos/marge-{new_filename}\' already exists. Overwrite ? [y/N] ' ) if overwrite.upper() == 'Y': final_clip.write_videofile( f'assets/videos/merged-{merged_filename}') print(f'{color.OKGREEN}Overwriting - done{color.ENDC}') break elif overwrite.upper() == 'N': print(f'{color.FAIL}Not overwriting - exiting{color.ENDC}') break else: os.system('cls') print(f'{color.FAIL}Invalid input!{color.ENDC}') continue else: final_clip.write_videofile( f'assets/videos/merged-{merged_filename}') break
def check_path(select_option): color = style.bcolors() option = selected.moptions() # Make sure that the user provides a video file. while True: # The concatenate function shouln't run this validation. if select_option != option.concatenate_video: video_file_path = input( f'Enter full path to video file {color.OKBLUE}(e.g. C:/Users/JohnDoe/Videos/example.mp4){color.ENDC}: ' ) if os.path.isfile(video_file_path): if video_file_path.lower().endswith(('.mp4', '.mkv', '.mov')): print( f'{color.OKGREEN}Video file has been found!{color.ENDC}\n' ) break else: os.system('cls') print( f'{color.FAIL}File isn\'t a video extension! (e.g. mp4 mkv mov){color.ENDC}' ) continue else: os.system('cls') print( f'{color.FAIL}Video file doesn\'t exist in this directory!{color.ENDC}' ) continue else: break # Make sure that the user provides an image file. if select_option == option.add_watermark: while True: watermark_file_path = input( f'Enter full path to watermark file {color.OKBLUE}(e.g. C:/Users/JohnDoe/Images/example.jpg){color.ENDC}: ' ) if os.path.isfile(watermark_file_path): if watermark_file_path.lower().endswith( ('.jpg', '.png', 'jpeg')): print( f'{color.OKGREEN}Watermark file has been found!{color.ENDC}\n' ) break else: os.system('cls') print( f'{color.FAIL}File isn\'t an image extension! (e.g. jpg png jpeg){color.ENDC}' ) continue else: os.system('cls') print( f'{color.FAIL}Watermark file doesn\'t exist in this directory!{color.ENDC}' ) continue # Make sure that the user provides an audio file. elif select_option == option.replace_audio: while True: audio_file_path = input( f'Enter full path to audio file {color.OKBLUE}(e.g. C:/Users/JohnDoe/Audio/example.mp3){color.ENDC}: ' ) if os.path.isfile(audio_file_path): if audio_file_path.lower().endswith(('.mp3')): print( f'{color.OKGREEN}Audio file has been found!{color.ENDC}\n' ) break else: os.system('cls') print( f'{color.FAIL}File isn\'t an audio extension! (e.g. mp3){color.ENDC}' ) continue else: os.system('cls') print( f'{color.FAIL}Audio file doesn\'t exist in this directory!{color.ENDC}' ) continue if select_option == option.create_gif: gif.create_gif(video_file_path) elif select_option == option.add_watermark: watermark.create_watermark(video_file_path, watermark_file_path) elif select_option == option.export_audio: frame.create_frames(video_file_path) elif select_option == option.split_video: split.create_split(video_file_path) elif select_option == option.concatenate_video: concatenate.create_concatenation() elif select_option == option.convert_video: convert.create_convertion(video_file_path) elif select_option == option.manipulate_video: manipulate.manipulate_audio(video_file_path) elif select_option == option.replace_audio: replace.replace_audio(video_file_path, audio_file_path) elif select_option == option.export_audio: export.export_audio(video_file_path)