|
| 1 | +import ffmpeg |
| 2 | +import os |
| 3 | + |
| 4 | +def change_video_format(input_file, output_file, format): |
| 5 | + print(format) |
| 6 | + input_stream = ffmpeg.input(input_file) |
| 7 | + output_stream = ffmpeg.output(input_stream, output_file, vcodec="libx264") |
| 8 | + ffmpeg.run(output_stream) |
| 9 | + |
| 10 | +def convert_video_to_audio(input_file, output_file): |
| 11 | + input_stream = ffmpeg.input(input_file) |
| 12 | + output_stream = ffmpeg.output(input_stream, output_file, acodec='mp3') |
| 13 | + ffmpeg.run(output_stream) |
| 14 | + |
| 15 | +def convert_video_to_frames(input_file, output_folder): |
| 16 | + input_stream = ffmpeg.input(input_file) |
| 17 | + ffmpeg.output(input_stream, f'{output_folder}/frame%04d.png').run() |
| 18 | + |
| 19 | +def cut_video(input_file, output_file, start_time, end_time): |
| 20 | + input_stream = ffmpeg.input(input_file, ss=start_time, to=end_time) |
| 21 | + output_stream = ffmpeg.output(input_stream, output_file, acodec='copy', vcodec='copy') |
| 22 | + ffmpeg.run(output_stream) |
| 23 | + |
| 24 | +def adjust_video_speed(input_file, output_file, speed): |
| 25 | + input_stream = ffmpeg.input(input_file) |
| 26 | + output_stream = ffmpeg.output(input_stream, output_file, filter_complex=f'[0]setpts={1/speed}*PTS', map='0') |
| 27 | + ffmpeg.run(output_stream) |
| 28 | + |
| 29 | +if __name__=="__main__": |
| 30 | + input_=input("Place The video in the same directory as video_editor.py\nand Input the video name (e.g. input_video.mp4): ") |
| 31 | + |
| 32 | + change_format_opt=input("Do you want to change the format of the video(y/n): ") |
| 33 | + if change_format_opt=='y' or change_format_opt=='Y': |
| 34 | + format=input("Input the valid output format (mov,mp4,m4a,3gp,3g2,mj2): ") |
| 35 | + print(f"output_vids/{input_[0:-4]}.{format}") |
| 36 | + change_video_format(input_, f"output_vids/{input_[0:-4]}.{format}", format) |
| 37 | + |
| 38 | + cvt_to_audio_opt=input("Do you want to convert the video to mp3(y/n): ") |
| 39 | + if cvt_to_audio_opt=='y' or cvt_to_audio_opt=='Y': |
| 40 | + convert_video_to_audio(input_, f"output_vids/{input_[0:-4]}.mp3") |
| 41 | + |
| 42 | + cvt_vid_to_frames=input("Do you want to extract frames from the video(y/n): ") |
| 43 | + if cvt_vid_to_frames=='y' or cvt_vid_to_frames=='Y': |
| 44 | + path = f"./{input_}_frames" |
| 45 | + os.mkdir(path) |
| 46 | + print(path) |
| 47 | + convert_video_to_frames(input_, path) |
| 48 | + |
| 49 | + cut_opt=input("Do you want to trim the video(y/n): ") |
| 50 | + if cut_opt=='y' or cut_opt=='Y': |
| 51 | + st=input("Enter the starting time(hh:mm:ss): ") |
| 52 | + et=input("Enter the ending time(hh:mm:ss): ") |
| 53 | + cut_video(input_, f"output_vids/trimmed_{input_}", st, et) |
| 54 | + |
| 55 | + adjust_speed_opt=input("Do you want to change the speed of video(y/n): ") |
| 56 | + if adjust_speed_opt=='y' or adjust_speed_opt=='Y': |
| 57 | + speed=float(input("Input the speed(0.5,1,1.5 etc): ")) |
| 58 | + adjust_video_speed(input_, f'output_vids/sped_up_{input_}', speed) |
0 commit comments