def extract_audio(self, video_input): """Method to extract the audio stream from a video file, copies the audio stream without re-encoding it. :param video_input: the input to extract from :param_type: str, .e.g. video.avi """ # if video_input does not exist, inform the user # and return None, stop processing in ffmpeg something # which does not exist if not os.path.exists(video_input): print('Make sure video exists') return None # FIXME we need a way to generate the extension of the # audio stream, so we can copy without re-encoding it # so we need to determine the extension of the audio # stream with the help of the ? info = BasicFFProbe().probe(video_input) # probe the video for stream in info.streams: if stream._codec_info['codec_type'] == 'audio': extension = stream._codec_info['codec_name'] output_audio = '.'.join(['external_audio', extension]) if os.path.exists(output_audio): raise FFMpegAlreadyExistsError(output_audio) _cmds = ['-i', video_input, '-vn', '-acodec', 'copy', output_audio] p = self._spawn(_cmds) # spawns a child process _parser = self.parser(p) # child process is passed to the __init__ _parser.parse_output() # method to parse output in the parser return _parser.status_finished # return the status of the parser
def add_logo(self, video_input, video_output, logo, right_corner=False, position=[]): """Method which can be used to add a logo to a video""" # check if video_output already exists or not if os.path.exists(video_output): raise FFMpegAlreadyExistsError(video_output) if right_corner: overlay = 'overlay=main_w-overlay_w-' else: overlay = 'overlay=' if len(position): overlay = overlay + str(position[0]) + ':' + str(position[1]) _cmds = [ '-i', video_input, '-i', logo, '-filter_complex', '[0:v][1:v]%s' % overlay, '-codec:a', 'copy', video_output ] # pdb.set_trace() p = self._spawn(_cmds) _parser = self.parser(p) _parser.parse_output() return _parser.status_finished
def convert_video(self, video_input, video_output, overwrite=False, vcodec=None, acodec=None): """ Method which can be used to convert a video from one format to another, keeps the original video file and creates a new one in the new format. :param video_input: the input to be converted. :param_type: str, .e.g. video.mp4 :param video_output: the output to come out :param_type: str, .e.g. video.avi :param overwrite: tell to overwrite or not :param_type: Bool :param vcodec: the video codec to use :param_type: str, .e.g. -vcodec h264 :param acodec: the audio codec to use :param_type: str, .e.g. -acodec aac :return: ? """ # check if video_output already exists or not, if it exists # then raise FFMpegAlreadyExistsError to inform the user if os.path.exists(video_output): if not overwrite: raise FFMpegAlreadyExistsError(video_output) # raise error # in here send input commands to ffmpeg to tell it that we want # to overwrite the file since overwrite=True # what are we going to return in here? # define commands for the ffmpeg operation _cmds = ['-i', video_input, video_output] if vcodec is not None: _cmds.append(vcodec) if acodec is not None: _cmds.append(acodec) p = self._spawn(_cmds) # spawn child process # pdb.set_trace() _parser = self.parser(p) # p goes in __init__ _parser.parse_output() # parses each line return _parser.status_finished # status of process
def cut_video(self, video_input, video_output, start_cut, end_cut=None): """Method to cut a delta from a video. param video_input: the video to cut from param_type: str, .e.g. 'video.mp4' param video_output: the video to output param_type: str, .e.g. 'video_cut.mp4' param start_cut: the point to start the cut param_type: str, .e.g. '%H:%M:%S' param end_cut: the point to end the cut param_type: str, .e.g. '%H:%M:%S' """ # the end_cut is by default equal to None, # if not provided by the user, the program # will automatically cut the video from the # starting point to the end of it if not os.path.exists(video_input): # check for video_input path print('Make sure video exists') return None if os.path.exists(video_output): raise FFMpegAlreadyExistsError(video_output) # end_cut is None by default, if it is let so # then set end_cut to the entire length of the # video with the following if conditional if end_cut is None: # setup end_cut to the end length of the video info = BasicFFProbe().probe('test.mp4') end_cut = str(info.get_media_duration()) # build the commands for cutting the video _cmds = [ '-i', video_input, '-ss', start_cut, '-to', end_cut, '-c', 'copy', video_output ] p = self._spawn(_cmds) # spawn the child process _parser = self.parser(p) # activate the parser _parser.parse_output() # parse the output return _parser.status_finished # return the status