def get_audio_energy(path, filename, fps): full_path = path + filename time, data = audio_to_array(full_path) sample_rate = sndfile(full_path).samplerate() total_frames = 0 avg_e = 0.0 for i in xrange(0, len(time), int(sample_rate / fps)): data[i] = abs(data[i]) total_frames = total_frames + 1 avg_e = avg_e + data[i] avg_e = avg_e / total_frames t = [] for i in xrange(0, len(time), int(sample_rate / fps)): t.append(data[i]) r = kaiser.convolve_kaiser(70, 5, t) energy = [] max_r = max(r) for i in range(len(r)): v = r[i] / max_r * 100.0 * (1.0 - avg_e) energy.append(v) return energy
def get_audio_energy_ffmpeg(path, filename, fps): full_path = path + filename reader = pf.FFMpegReader() reader.open(full_path, pf.TS_AUDIO) audio_track = reader.get_tracks()[0] smp_rate = audio_track.get_samplerate() smp_in_frame = smp_rate / fps count = 0 fno = 1 s = 0 result = [] max_value = 1 try: while True: frame = audio_track.get_next_frame() for f in frame: d = abs((f[0] + f[1]) / 2) s = s + d count = count + 1 if count >= smp_in_frame: if max_value < s: max_value = s fno = fno + 1 result.append(s) count = 0 s = 0 except IOError: print "Energy complete" avg_e = 0.0 t = [] for i in result: v = float(i) / max_value avg_e = avg_e + v t.append(v) avg_e = avg_e / fno print avg_e, r = kaiser.convolve_kaiser(70, 5, t) energy = [] max_r = max(r) for i in range(len(r)): v = r[i] / max_r * 100.0 * (1.0 - avg_e) energy.append(v) return energy
def get_arousal(video_path, video_file, audio_path, audio_file): print "Calculating arousal for file", video_path, video_file fps = shot_motion.get_fps(video_path, video_file) print "FPS:", fps print "Shots and motions started" rhythm, motion = shot_motion.get_shot_motion(video_path, video_file) print "Shots and motions completed" print "Audio energy started" e = energy.get_audio_energy_ffmpeg(audio_path, audio_file, fps) print "Audio energy completed" a = [] for i in range(len(rhythm)): v = (rhythm[i] + motion[i] + e[i]) / 3 a.append(v) arousal = kaiser.convolve_kaiser(150, 5, a) return a, arousal
def get_shot_motion(path, filename, verbose=False): fullpath = path + filename # psyco.full() total_histo_size = 0 # create ffmpeg reader instance reader = pf.FFMpegReader() # open media file with PIL support reader.open(fullpath, pf.TS_VIDEO_PIL) # get video track video_track = reader.get_tracks()[0] fps = video_track.get_fps() idx = 0 # frame index histo = [] motion_vecs = [] print "Extracting shots and motions from", path, filename try: last_frame = None histo_size = 0 while True: frame = video_track.get_next_frame() idx = idx + 1 # Compare current frame and the last frame for histogram change h2 = frame.histogram() mv = video_track.get_current_frame_motion_vectors() m = calc_motion_val(mv, frame.size) # Compute motion vectors if math.isnan(m): m = calc_motion_val(mv, frame.size, True) motion_vecs.append(m) if last_frame == None: last_frame = frame for i in h2: histo_size += i continue h1 = last_frame.histogram() r = float(histo_change(h1, h2)) / histo_size / 2 last_frame = frame histo.append(r) except IOError: print "Frame read completed" reader.close() print "Detecting shots..." shots = detect_shots(histo, fps) print "Calculating shot change rate..." rhythm = calc_shot_change_rate(shots, idx) ret_rhythm = kaiser.convolve_kaiser(70, 5, rhythm) ret_motion = kaiser.convolve_kaiser(70, 5, motion_vecs) return ret_rhythm, ret_motion