PageRenderTime 22ms CodeModel.GetById 16ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/videx/vafroma3.py

http://echo-nest-remix.googlecode.com/
Python | 105 lines | 92 code | 3 blank | 10 comment | 0 complexity | 5fc686a058ad536a75a112cf8a9a8a54 MD5 | raw file
  1
  2#!/usr/bin/env python
  3# encoding: utf=8
  4
  5"""
  6vafroma3.py
  7
  8Re-synthesize video A using the segments of song A.
  9Tries to use longer sequences of video by boosting the distance neighbors of similar segments.
 10
 11By Ben Lacker, P. Lamere
 12"""
 13import numpy
 14import sys
 15import time
 16import echonest.audio as audio
 17import echonest.video as video
 18import echonest.modify as modify
 19
 20usage="""
 21Usage:
 22    python vafroma3.py <input_filename>
 23
 24Example:
 25    python vafroma3.py BillieJeanMusicVideo.mp4
 26"""
 27
 28
 29dur_weight = 1000
 30#dur_weight = 100
 31timbre_weight = .001
 32pitch_weight = 10
 33loudness_weight = 1
 34
 35class AfromA(object):
 36    def __init__(self, input_filename, output_filename):
 37        self.av = video.loadav(input_filename)
 38        self.segs = self.av.audio.analysis.segments
 39        self.output_filename = output_filename
 40
 41    def get_distance_from(self, seg):
 42        distances = []
 43        for a in self.segs:
 44            ddur = numpy.square(seg.duration - a.duration)
 45            dloud = numpy.square(seg.loudness_max - a.loudness_max)
 46
 47            timbre_diff = numpy.subtract(seg.timbre, a.timbre)
 48            dtimbre = (numpy.sum(numpy.square(timbre_diff)))
 49
 50            pitch_diff = numpy.subtract(seg.pitches, a.pitches)
 51            dpitch = (numpy.sum(numpy.square(pitch_diff)))
 52
 53            #print dur_weight * ddur, timbre_weight * dtimbre, \
 54            #      pitch_weight * dpitch, loudness_weight * dloud
 55            distance =    dur_weight * ddur \
 56                        + loudness_weight * dloud \
 57                        + timbre_weight * dtimbre \
 58                        + pitch_weight * dpitch;
 59            distances.append(distance)
 60
 61        return distances
 62            
 63
 64    def run(self):
 65        st = modify.Modify()
 66        last_index = 0
 67        collect = audio.AudioQuantumList()
 68        match_index = -1
 69        for a in self.segs:
 70            seg_index = a.absolute_context()[0]
 71
 72            distances = self.get_distance_from(a)
 73
 74            distances[seg_index] = sys.maxint
 75
 76            if match_index < len(distances) -1:
 77                distances[match_index + 1] *= .3
 78
 79            match_index = distances.index(min(distances))
 80            match = self.segs[match_index]
 81            print seg_index, match_index
 82            # make the length of the new seg match the length
 83            # of the old seg
 84            collect.append(match)
 85        out = video.getpieces(self.av, collect)
 86        out.save(self.output_filename)
 87
 88def main():
 89    try:
 90        input_filename = sys.argv[1]
 91        if len(sys.argv) > 2:
 92            output_filename = sys.argv[2]
 93        else:
 94            output_filename = "aa3_" + input_filename
 95    except:
 96        print usage
 97        sys.exit(-1)
 98    AfromA(input_filename, output_filename).run()
 99
100
101if __name__=='__main__':
102    tic = time.time()
103    main()
104    toc = time.time()
105    print "Elapsed time: %.3f sec" % float(toc-tic)