def getAudioDuration(path): matedata = FFProbe(path) adur = 0 for stream in matedata.streams: if stream.is_audio(): adur = stream.duration_seconds() return adur
def checkFile(vid): try: fileInfo = FFProbe(vid) except: return False, [] size = os.stat(vid).st_size print(size) if size < 10000000: return False, [] filename, ext = os.path.splitext(vid) index = [] for item in fileInfo.streams: index.append(item.__dict__) for stream in fileInfo.streams: if stream.is_video(): my_stream = dict(stream.__dict__) print(my_stream) if (('TAG:DURATION' in my_stream and my_stream['TAG:DURATION'] > '00:10:00.000000000') or ('codec_type' in my_stream and my_stream['codec_type'] == 'video')) and my_stream['codec_name'] != 'ansi': return True, index return False, []
def Transcoding(self): destination = self.file_path print(destination) for file in glob.glob(destination): if file.split('.')[-1] not in self.extension_allowed: continue print(file) output_file = os.getcwd() + "/" + ((file.split('/')[-1]).split('.')[0]) + ".mxf" print(output_file) duration = self.get_duration(file) metadata = FFProbe(file) streams_metadata = metadata.streams field_order_scan = "" audio_channels = "" video_index = "" audio_indexes = [] audio_stream_channel_map = {} audio_codec_name = "" file_profile = "" file_processing_status = "" command = self.get_command_stub(file) for stream_name in streams_metadata: if stream_name.codec_type == "video": profile,command, field_order_scan, video_index= self.video_metatdata(stream_name, command, field_order_scan, video_index) file_profile = file_profile + profile.split('_')[2] + "_" + profile.split('_')[3] + "_" elif stream_name.codec_type == "audio": profile, audio_stream_channel_map, audio_channels, audio_indexes, audio_codec_name = self.audio_metadata(stream_name, audio_stream_channel_map, audio_channels, audio_indexes, audio_codec_name ) file_profile = file_profile + "_" + str(audio_channels) elif stream_name.codec_type == "data": profile = self.data_metadata(stream_name) print(profile) if audio_channels == "": command, video_index = self.no_audio(command, video_index, output_file) elif ((type(audio_channels) is int) and (audio_channels >= 1)) : command = self.audio_present(command, video_index, audio_indexes, audio_stream_channel_map,audio_codec_name, output_file) command = self.get_audiocodec_output(command, audio_channels, audio_codec_name, output_file) print(field_order_scan) command = self.command_type_check(field_order_scan,command) print(1) print(command) try: self.file_processing_status = "DONE" # command = self.command_type_check(field_order_scan) # print(command) retcode = subprocess.call(command) self.Move_To_Dest(output_file, retcode) except: # print(command) self.file_processing_status = "ERROR" print("Cannot process file, Error logged.") self.add_fileprofile(file,file_profile,duration)
def getVideoFrames(self): # Split video into it corresponding parts based on self.desiredFrames print(self.inputVideo) metadata = FFProbe(self.inputVideo) print(metadata) # if len(metadata.streams) != 2: # # continue #duration = metadata.audio[0].duration_seconds() duration = self.getLength() self.duration = duration print(duration) # try: # duration = metadata.audio[0].duration_seconds() # except: # pass timestep = float(duration) / self.desiredFrames print(duration, timestep) print(self.inputVideo) cmd = 'ffmpeg -i ' + self.inputVideo + ' ' + self.outputPath + '$filename%04d.jpg' os.system(cmd) if os.path.exists(self.outputAudio + 'audio.wav'): os.remove(self.outputAudio + 'audio.wav') cmd2 = 'ffmpeg -i ' + self.inputVideo + ' ' + self.outputAudio + 'audio.wav' os.system(cmd2) #import pdb; pdb.set_trace() # os.system("ffmpeg -i %s %s" % (self.inputVideo, self.outputPath)) return True
def getLength(self, mediaPath): metadata=FFProbe(mediaPath) length = 0.0 for stream in metadata.streams: if (stream.is_video()) or (stream.is_audio()): length = stream.duration_seconds() #print(" -> Length: " + str(length)) return length
def getVideoDuration(path): matedata = FFProbe(path) # print("streams",len(matedata))matedata videoDuration = 0 for stream in matedata.streams: if stream.is_video(): videoDuration = stream.duration_seconds() return videoDuration
def extractThumbnail(originalPath, thumbnailPath): time = 0.0 metadata = FFProbe(originalPath) for stream in metadata.streams: if stream.is_video(): time = stream.duration_seconds() / 2 thumbnailTask = ffmpy3.FFmpeg( inputs={originalPath: None}, outputs={thumbnailPath: '-y -ss %d -vframes 1 ' % (time)}) thumbnailTask.run()
def aws_stuff(index): table = dynamodb.Table('Videos') metadata = FFProbe('./resources/{}'.format(index)) is_video = False for stream in metadata.streams: if stream.is_video(): is_video = True video_length = int(stream.duration_seconds()) table.update_item( Key= {'id': index}, UpdateExpression = "SET video_length = :video_length", ExpressionAttributeValues={':video_length': video_length} ) break if not is_video: table.update_item( Key= {'id': index}, UpdateExpression = "SET job_status = :job_status", ExpressionAttributeValues={':job_status': 'Invalid Video File'} ) #TODO delete file return table.update_item( Key= {'id': index}, UpdateExpression = "SET job_status = :job_status", ExpressionAttributeValues={':job_status': 'Extracting Audio'} ) audio_file = AudioExtract.extractFLAC(index) bucket = 'orbitalphase1' s3 = boto3.client('s3') s3.upload_file(audio_file, bucket, audio_file) transcribe = boto3.client('transcribe') result = transcribe.start_transcription_job( TranscriptionJobName=index, LanguageCode='en-US', MediaSampleRateHertz=44100, MediaFormat='flac', Media={ 'MediaFileUri': 'https://s3-us-east-2.amazonaws.com/{}/{}'.format(bucket,audio_file) } ) table.update_item( Key= {'id': index}, UpdateExpression = "SET job_status = :job_status", ExpressionAttributeValues={':job_status': 'Sent Audio For Transcription'} )
def stream_aspect_ratio(file): # Returns the aspect ratio expressed as a string, e.g. "4:3" file_metadata = FFProbe(file) ratio = None for stream in file_metadata.streams: if stream.is_video(): width, height = stream.frame_size() for resolution in RESOLUTIONS: if RESOLUTIONS[resolution]["width"] == width: ratio = RESOLUTIONS[resolution]["aspect_ratio"] return ratio if not ratio: print("Incompatible input dimensions.") sys.exit()
def getARandRes(path): inputfile = FFProbe(path) for stream in inputfile.streams: if stream.is_video(): width, height = stream.frame_size() ratio = "" for aspect in aspects: if aspect[0] == width: ratio = aspect[1] break if ratio == "": print("Incompatible input dimensions.") sys.exit() return ratio, [width, height]
def computeResolution(inputPath, outputPath, videoName, videoExtension): metadata = FFProbe(inputPath) videoRes = 0 videoMaxResolution = 0 for stream in metadata.streams: if stream.is_video(): (width, height) = stream.frame_size() videoRes = width * height for res in resolutions: if videoRes >= res: videoMaxResolution = res fmpg = ffmpy3.FFmpeg( inputs={inputPath: None}, outputs={ outputPath + '/' + videoName + '_' + resolutionNames[res] + '.' + videoExtension: resolutionComputing[res] }) fmpg.run() if videoMaxResolution == 0: return videoMaxResolution return "_" + resolutionNames[videoMaxResolution]
def calculate_length_and_upload_to_s3(sender, instance, **kwargs): post_save.disconnect(calculate_length_and_upload_to_s3, sender=sender) ffprobe = FFProbe(str(instance.audiofile.file.name)) print(str(instance.audiofile.file.name)) print(ffprobe.duration) if ffprobe.duration is not None: instance.length = ffprobe.duration else: instance.length = 0 # try: # ffprobe_out = subprocess.Popen( # "ffprobe " + str(instance.audiofile.file.name), # shell=True, # stdout=subprocess.PIPE, # stderr=subprocess.STDOUT, # close_fds=True # ) # for line in ffprobe_out.stdout.readlines(): # line = line.decode() # if "Duration" in line: # values = line.split(",")[0].split("Duration:")[1].split(":") # duration = 3600 * float(values[0]) + 60 * float(values[1]) + float(values[2]) # ffprobe_out.stdout.close() # ffprobe_out() # ffprobe_out.wait() # print(duration) # instance.length = duration # # ffprobe_out.stdout.close() # ffprobe_out.pipe_cloexec() # except IOError: # print("0") # instance.length = 0 instance.save() upload_to_s3(instance) post_save.connect(calculate_length_and_upload_to_s3, sender=sender)
def checkDir(dirname): global total_duration global cnt global verbose global only_errors for f in os.scandir(dirname): if f.is_dir(): checkDir(f.path) else: if f.name[-3:] in ['mp4', 'mov', 'avi']: cnt += 1 try: metadata = FFProbe(f.path) except: if verbose or only_errors: print('Error in file', f.path) continue duration_vs = 0.0 for stream in metadata.streams: if stream.is_video(): duration_vs += stream.duration_seconds() if verbose: print(f.name, "duration by video streams:", duration_vs) total_duration += duration_vs
action='store_true', help='filter results that are low bit rate') args = parser.parse_args() file_mask = "*." + args.extension if args.recursive: file_mask = "**/*." + args.extension print( f"Starting directory scan path={args.path} recursive={args.recursive} file_mask={file_mask}" ) for filename in glob.iglob(args.path + file_mask, recursive=args.recursive): metadata = FFProbe(filename) pass_filter = True if args.find_stereo_only: pass_filter = is_stereo_only(metadata) if pass_filter and args.find_single_audio_only: pass_filter = is_single_audio_only(metadata) if pass_filter and args.find_fullscreen: pass_filter = is_fullscreen(metadata) if pass_filter and args.find_low_bit_rate: pass_filter = is_low_bit_rate(metadata, args.min_bit_rate)
help="never use previous encoding from ffmpeg if available") group.add_argument( "-n", action="store_false", dest="overwrite", help="always use previous encoding from ffmpeg if available") args = parser.parse_args() print(version) print(args) input_filepath = args.source # probe metadata input_metadata = FFProbe(input_filepath) for stream in input_metadata.streams: if stream.is_video(): input_framerate = stream.frames() / stream.duration_seconds() output_keyinput_framerate = round( (args.segment_duration * input_framerate) / (1000 * args.i_factor)) dir_dashed = "video_dashed" dir_encoded = "video_encoded" ### SETUP FOLDERS ### # Set up folders if first run def makeFolder(folderpath): if not os.path.exists(folderpath):
def getLength(filename): """Returns length of file to the microsecond using ffprobe""" return FFProbe(filename).streams[0].duration_seconds()
def main(): '''Populate video info file.''' from argparse import ArgumentParser # for some reason pylint complains about members being undefined :( # pylint: disable=E1101 parser = ArgumentParser( description='Run ffprobe on file to find video FPS. Load and restore to videoname.mp4.info') show_default = ' (default %(default)s)' parser.add_argument('-V', dest='video_in', default=None, help='Input video') parser.add_argument('-F', '--family-preset', dest='familyPreset', help='Family preset: detect "inv" at the end of the name to set both "family" and "inverse"') parser.add_argument('-fps', dest='fps', default=20.0, type=float, help='video codec FPS, may be different to actual fps for broken encoders '+ show_default) options = parser.parse_args() if (options.video_in == None): print('No video provided') return -1 if (not os.path.isfile(options.video_in)): print('Videofile {} not found'.format(options.video_in)) return -1 jsonfile = options.video_in+'.info' jsonfileout=jsonfile #+'_out.txt' print("jsonfile="+jsonfile) print("jsonfileout="+jsonfileout) if (os.path.isfile(jsonfile)): with open(jsonfile) as infile: try: data_orig = json.load(infile) data = copy.deepcopy(data_orig) except ValueError as err: print('Error parsing JSON file {}'.format(jsonfile)) print(err) return 1 print("Loaded old JSON file: {}".format(jsonfile)) print(data) else: data = {'place': 'Gurabo'} print("No preexisting JSON file: {}".format(jsonfile)) print(data) metadata=FFProbe(options.video_in) #FFStream.frame_rate = get_frame_rate def get_field(self, name): """ Returns named parameter using ffprobe. Returns none is it is not a video stream. """ f = None if self.is_video(): if self.__dict__[name]: f = self.__dict__[name] return f for stream in metadata.streams: if stream.is_video(): #print(stream.__dict__.keys()) data["nframes"]=stream.frames() data["duration"]=stream.duration_seconds() r_frame_rate=get_field(stream,'r_frame_rate') ratio = r_frame_rate.split('/') if (len(ratio)==1): fps = float(ratio[0]) elif (len(ratio)==2): fps = float(ratio[0])/float(ratio[1]) else: print('Error, could not parse r_frame_rate={}'.format(r_frame_rate)) data["r_frame_rate"]=r_frame_rate data["videofps"]=fps #data["ffprobe"] = stream.__dict__ print("### Original JSON:") pp(data_orig) print("### Updated JSON:") pp(data) if (not user_yes_no_query('Update file {} ?'.format(jsonfileout))): return 0 with open(jsonfileout, 'w') as outfile: json.dump(data, outfile, indent=2, sort_keys=False) outfile.write('\n') return 0
def extract_files(): """After we have all of our videos split between train and test, and all nested within folders representing their classes, we need to make a data file that we can reference when training our RNN(s). This will let us keep track of image sequences and other parts of the training process. We'll first need to extract images from each of the videos. We'll need to record the following data in the file: [train|test], class, filename, nb frames Extracting can be done with ffmpeg: `ffmpeg -i video.mpg image-%04d.jpg` """ data_file = [] folders = ['./train/', './test/'] seq_len = 40 def rescale_list(input_list, size): """Given a list and a size, return a rescaled/samples list. For example, if we want a list of size 5 and we have a list of size 25, return a new list of size five which is every 5th element of the origina list.""" #import pdb;pdb.set_trace() try: assert len(input_list) >= size except: return [] # Get the number to skip between iterations. skip = len(input_list) // size # Build our new output. output = [input_list[i] for i in range(0, len(input_list), skip)] # Cut off the last one if needed. return output[:size] f = open('failedfiles.csv','w') for folder in folders: class_folders = glob.glob(folder + 'vids/%s'%sys.argv[1]) #class_folders = glob.glob(folder + 'vids/ApplyEyeMakeup') #class_folders = glob.glob(folder + 'vids/test') for vid_class in class_folders: class_files = glob.glob(vid_class + '/*.mp4') #class_files = glob.glob('./train/vids/ApplyEyeMakeup/v_ApplyEyeMakeup_g09_c01.avi') #class_files = glob.glob('./train/vids/test/2013DisneysTwilightZoneTowerofTerror-.mp4') for video_path in class_files: # Get the parts of the file. #import pdb; pdb.set_trace() video_parts = get_video_parts(video_path) train_or_test, srctype, classname, filename_no_ext, filename = video_parts #import pdb;pdb.set_trace() # Check if this class exists. for t in ['imgs','wavs','wavsfull','specs','imgsmerge','imgsrescale']: if not os.path.exists(train_or_test + '/'+t+'/' + classname): print("Creating folder for %s/%s" % (train_or_test, classname)) os.makedirs(train_or_test + '/'+t+'/' + classname) # Only extract if we haven't done it yet. Otherwise, just get # the info. if not check_already_extracted(video_parts): # Now extract it. src = train_or_test + '/vids/' + classname + '/' + \ filename dest = train_or_test + '/imgs/' + classname + '/' + \ filename_no_ext + '-%04d.jpg' dest1 = train_or_test + '/imgsrescale/' + classname + '/' + \ filename_no_ext dest2 = train_or_test + '/wavsfull/' + classname + '/' + \ filename_no_ext + '.wav' dest3 = train_or_test + '/wavs/' + classname + '/' + \ filename_no_ext #+ '-%04d.wav' dest4 = train_or_test + '/specs/' + classname + '/' dest5 = train_or_test + '/imgsrescale/' +classname + '/' dest6 = train_or_test + '/imgsmerge/' +classname + '/' from ffprobe3 import FFProbe metadata = FFProbe(src) if len(metadata.streams) != 2: continue try: duration = metadata.audio[0].duration_seconds() except: continue timestep = float(duration)/seq_len print (duration,timestep) os.system("ffmpeg -i %s %s" %(src, dest)) #import pdb; pdb.set_trace() os.system("ffmpeg -i %s %s" % (src, dest2)) imgs = glob.glob(train_or_test + '/imgs/' + classname + '/' + \ filename_no_ext+'*.jpg') rescale_imgs = rescale_list(sorted(imgs),seq_len) if rescale_imgs == []: f.write("glob,%s\n"%filename_no_ext) continue for i,im in enumerate(rescale_imgs): os.system("cp %s %s-%04d.jpg"%(im,dest1,i)) # from ffprobe3 import FFProbe # metadata = FFProbe(src) # duration = metadata.streams[0].duration_seconds() # timestep = float(duration)/seq_len # print (duration,timestep) import split_audio as spa spa.split(dest2,dest3,timestep=timestep*1000) import audio2spec_scipy2 as a2s import img_concat as imcat #from glob import glob names = glob.glob(dest3+'*.wav') if len(names) == 0: continue try: assert len(rescale_imgs)==len(names) except: f.write("audio,%s\n"%filename_no_ext) continue #for t in [('imgs','.jpg'),('wavs','.wav'),('wavsfull','.wav'),('specs','.png'),('imgsmerge','.jpg'),('imgsrescale','.jpg')]: # if os.path.exists(train_or_test + '/'+t[0]+'/' + classname+'/'+filename_no_ext+'*'): # os.system("rm -f %s"%(train_or_test + '/'+t[0]+'/' + classname+'/'+filename_no_ext+'*')) #os.remove(train_or_test + '/'+t[0]+'/' + classname+'/'+filename_no_ext+t[1]) for wf in names: #a2s.graph_spectrogram(wf,dest4+os.path.basename(wf).split('.')[0]) a2s.plotstft(wf, plotpath=dest4+os.path.basename(wf).split('.')[0]) imcat.concat(dest5+os.path.basename(wf).split('.')[0]+'.jpg',\ dest4+os.path.basename(wf).split('.')[0]+'.png',\ dest6+os.path.basename(wf).split('.')[0]) #os.system("ffmpeg -t 60 -acodec copy -i %s %s" % (src, dest3)) #call(["ffmpeg", "-r", '1', "-i", src, dest]) #call(["ffmpeg", "-vn", "-acodec", "pcm_s16le", "-ar", '44100', "-ac", '2', "-i", src, dest2]) #call(["ffmpeg", "-t", '60', "-acodec", "copy", "-i", dest2, dest3]) # Now get how many frames it is. nb_frames = get_nb_frames_for_video(video_parts) data_file.append([train_or_test, classname, filename_no_ext, nb_frames]) print("Generated %d frames for %s" % (nb_frames, filename_no_ext)) f.close() with open('data_file.csv', 'w') as fout: writer = csv.writer(fout) writer.writerows(data_file) print("Extracted and wrote %d video files." % (len(data_file)))
import os from ffprobe3 import FFProbe from ffprobe3.exceptions import FFProbeError test_dir = os.path.dirname(os.path.abspath(__file__)) test_videos = [ os.path.join(test_dir, './data/SampleVideo_720x480_5mb.mp4'), os.path.join(test_dir, './data/SampleVideo_1280x720_1mb.mp4'), os.path.join(test_dir, './data/SampleVideo_360x240_50mb.mp4'), os.path.join(test_dir, './data/SampleVideo_1280x720_50mb.mp4'), ] for test_video in test_videos: media = FFProbe(test_video) print('File:', test_video) print('\tStreams:', len(media.streams)) for index, stream in enumerate(media.streams, 1): print('\tStream: ', index) try: if stream.is_video(): frame_rate = stream.frames() / stream.duration_seconds() print('\t\tFrame Rate:', frame_rate) print('\t\tFrame Size:', stream.frame_size()) print('\t\tDuration:', stream.duration_seconds()) print('\t\tFrames:', stream.frames()) print('\t\tIs video:', stream.is_video()) except FFProbeError as e: print(e) except Exception as e: