My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python
# encoding: utf=8

"""
vafroma.py

Re-synthesize video A using the segments of song A.

By Ben Lacker, P. Lamere
"""
import numpy
import sys
import time
import echonest.audio as audio
import echonest.video as video
import echonest.modify as modify

usage="""
Usage:
python vafroma.py <input_filename>

Example:
python vafroma.py BillieJeanMusicVideo.mp4
"""


dur_weight = 1000
#dur_weight = 100
timbre_weight = .001
pitch_weight = 10
loudness_weight = 1

class AfromA(object):
def __init__(self, input_filename, output_filename):
self.av = video.loadav(input_filename)
self.segs = self.av.audio.analysis.segments
self.output_filename = output_filename

def get_distance_from(self, seg):
distances = []
for a in self.segs:
ddur = numpy.square(seg.duration - a.duration)
dloud = numpy.square(seg.loudness_max - a.loudness_max)

timbre_diff = numpy.subtract(seg.timbre, a.timbre)
dtimbre = (numpy.sum(numpy.square(timbre_diff)))

pitch_diff = numpy.subtract(seg.pitches, a.pitches)
dpitch = (numpy.sum(numpy.square(pitch_diff)))

#print dur_weight * ddur, timbre_weight * dtimbre, \
# pitch_weight * dpitch, loudness_weight * dloud
distance = dur_weight * ddur \
+ loudness_weight * dloud \
+ timbre_weight * dtimbre \
+ pitch_weight * dpitch;
distances.append(distance)

return distances


def run(self):
st = modify.Modify()
collect = audio.AudioQuantumList()
for a in self.segs:
seg_index = a.absolute_context()[0]

distances = self.get_distance_from(a)

distances[seg_index] = sys.maxint

match_index = distances.index(min(distances))
match = self.segs[match_index]
print seg_index, match_index
# make the length of the new seg match the length
# of the old seg
collect.append(match)
out = video.getpieces(self.av, collect)
out.save(self.output_filename)

def main():
try:
input_filename = sys.argv[1]
if len(sys.argv) > 2:
output_filename = sys.argv[2]
else:
output_filename = "aa_" + input_filename
except:
print usage
sys.exit(-1)
AfromA(input_filename, output_filename).run()


if __name__=='__main__':
tic = time.time()
main()
toc = time.time()
print "Elapsed time: %.3f sec" % float(toc-tic)

Change log

r515 by blac...@echonest.com on Mar 31, 2011   Diff
more accurate usage
Go to: 
Project members, sign in to write a code review

Older revisions

r478 by jsundram on Jul 13, 2010   Diff
moving video examples to where they
belong
r381 by paul.lamere on Jan 5, 2010   Diff
added versions to adapt remix
r380 by paul.lamere on Dec 3, 2009   Diff
Added video a from a
All revisions of this file

File info

Size: 2642 bytes, 98 lines
Powered by Google Project Hosting