def get_io(parser): if(not parser.inp): inp = input('Please enter input video file path : ') if(not os.path.exists(inp)): Colour.print('File diesnot exist',Colour.RED) sys.exit(0) else: inp = parser.inp if(not parser.output): out = ''.join(inp.split('.')[:-1]) + '_output.' + inp.split('.')[-1] else: out = parser.output if(os.path.exists(out)): Colour.print('[Warning] '+out+' file already exists',Colour.YELLOW) return inp,out
def get_resizer(parser): resizer = ' ' if(not parser.action == 'resize'): return resizer if(parser.quality > 150): Colour.print('maximum value of quality is 150',Colour.RED) sys.exit(0) if(parser.quality < 0): Colour.print('quality cant be negative',Colour.RED) sys.exit(0) qual = parser.quality/100 qual = 51 - qual*33 ''' actual crf (libx264) scale is from 0 to 51. normally 0 - 18 increase size so they are useless we will choose range from 18 to 51 ''' resizer = ' -crf ' + str(qual) + ' ' return resizer
def get_trimmer(parser): trimmer = ' ' if(not parser.action == 'trim'): return trimmer if(not parser.start_time): st = input('Please enter start time in format hh:mm:ss or mm:ss : ') else: st = parser.start_time st = Time.get_time(st) if(not st): Colour.print('Wrong format for start-time',Colour.RED) sys.exit(0) if(not parser.time and not parser.end_time): t = input('Please enter time duration in format hh:mm:ss or mm:ss : ') elif(not parser.time): t = parser.end_time t = Time.get_time(t) if(not t): Colour.print('Wrong format for end_time',Colour.RED) sys.exit(0) t = Time.get_relative_time(st,t) if(not t): Colour.print('end_time should be greater than start_time',Colour.RED) sys.exit(0) else: t = parser.time t = Time.get_time(t) if(not t): Colour.print('Wrong format for time duration',Colour.RED) sys.exit(0) trimmer = " -ss " +str(st)+ " -t " +str(t) return trimmer
def audio_cutter(inp,trimmer,out): exec_command = 'ffmpeg -i ' + str(inp) + trimmer + codec + out Colour.print(exec_command,Colour.GREEN) os.system(exec_command)
def video_resizer(inp,resizer,out): video_codec = "" # senseless to say 'change quality video and copy video, both at same time' audio_codec = " -c:a copy " exec_command = 'ffmpeg -i ' + str(inp) + resizer + video_codec + audio_codec + out Colour.print(exec_command,Colour.GREEN) os.system(exec_command)
def video_cropper(inp,trimmer,out): video_codec = "" # senseless to say 'crop video and copy video, both at same time' audio_codec = " -c:a copy " exec_command = 'ffmpeg -i ' + str(inp) + filters + video_codec + audio_codec + out Colour.print(exec_command,Colour.GREEN) os.system(exec_command)
def video_trimmer(inp,trimmer,out): video_codec = " -c:v copy " audio_codec = " -c:a copy " exec_command = 'ffmpeg -i ' + str(inp) + trimmer + video_codec + audio_codec + out Colour.print(exec_command,Colour.GREEN) os.system(exec_command)
def verify_dependencies(): try: sp.call(['ffmpeg','--help'],stdout=sp.PIPE,stderr=sp.STDOUT) except: Colour.print('Please install ffmpeg',Colour.RED) sys.exit(0)
medipack inp.mp4 qual -q 50 -o output.mp4 for audio, please specify output name using -o and extension should be mp3 medipack inp.mp4 trim -s 0:30 -e 1:40 -o output.mp3 ''' if (__name__ == "__main__"): Util.verify_dependencies() parser = Args.get_parser() inp, out = Util.get_io(parser) # audio if (out.split('.')[-1] == 'mp3'): if (parser.action in ['crop', 'resize']): message = parser.action + ' not supported for mp3 output' Colour.print(message, Colour.RED) sys.exit(0) elif (parser.action == 'trim'): trimmer = Util.get_trimmer(parser) Medipack.aaudio_cutter(inp, trimmer, out) else: Colour.print('unknow action', Colour.YELLOW) sys.exit(0) # video if (parser.action == 'trim'): trimmer = Util.get_trimmer(parser) Medipack.video_trimmer(inp, trimmer, out) elif (parser.action == 'crop'): filters = Util.get_filters(parser) Medipack.video_cropper(inp, filters, out)