/examples/videx/vafroma2.py

http://echo-nest-remix.googlecode.com/ · Python · 103 lines · 70 code · 18 blank · 15 comment · 8 complexity · b5fb57dfc3e5ad723e2e825b58b159ce MD5 · raw file

  1. #!/usr/bin/env python
  2. # encoding: utf=8
  3. """
  4. vafroma2.py
  5. Re-synthesize video A using the segments of song A.
  6. Same as vafroma.py, but avoids re-using segments.
  7. By Ben Lacker, P. Lamere
  8. """
  9. import numpy
  10. import sys
  11. import time
  12. import echonest.audio as audio
  13. import echonest.video as video
  14. import echonest.modify as modify
  15. usage="""
  16. Usage:
  17. python vafroma2.py <input_filename>
  18. Example:
  19. python vafroma2.py BillieJeanMusicVideo.mp4
  20. """
  21. dur_weight = 1000
  22. #dur_weight = 100
  23. timbre_weight = .001
  24. pitch_weight = 10
  25. loudness_weight = 1
  26. class AfromA(object):
  27. def __init__(self, input_filename, output_filename):
  28. self.av = video.loadav(input_filename)
  29. self.segs = self.av.audio.analysis.segments
  30. self.output_filename = output_filename
  31. def get_distance_from(self, seg):
  32. distances = []
  33. for a in self.segs:
  34. ddur = numpy.square(seg.duration - a.duration)
  35. dloud = numpy.square(seg.loudness_max - a.loudness_max)
  36. timbre_diff = numpy.subtract(seg.timbre, a.timbre)
  37. dtimbre = (numpy.sum(numpy.square(timbre_diff)))
  38. pitch_diff = numpy.subtract(seg.pitches, a.pitches)
  39. dpitch = (numpy.sum(numpy.square(pitch_diff)))
  40. #print dur_weight * ddur, timbre_weight * dtimbre, \
  41. # pitch_weight * dpitch, loudness_weight * dloud
  42. distance = dur_weight * ddur \
  43. + loudness_weight * dloud \
  44. + timbre_weight * dtimbre \
  45. + pitch_weight * dpitch;
  46. distances.append(distance)
  47. return distances
  48. def run(self):
  49. st = modify.Modify()
  50. collect = audio.AudioQuantumList()
  51. used = []
  52. for a in self.segs:
  53. seg_index = a.absolute_context()[0]
  54. distances = self.get_distance_from(a)
  55. distances[seg_index] = sys.maxint
  56. for u in used:
  57. distances[u] = sys.maxint
  58. match_index = distances.index(min(distances))
  59. match = self.segs[match_index]
  60. print seg_index, match_index
  61. # make the length of the new seg match the length
  62. # of the old seg
  63. collect.append(match)
  64. used.append(match_index)
  65. out = video.getpieces(self.av, collect)
  66. out.save(self.output_filename)
  67. def main():
  68. try:
  69. input_filename = sys.argv[1]
  70. if len(sys.argv) > 2:
  71. output_filename = sys.argv[2]
  72. else:
  73. output_filename = "aa2_" + input_filename
  74. except:
  75. print usage
  76. sys.exit(-1)
  77. AfromA(input_filename, output_filename).run()
  78. if __name__=='__main__':
  79. tic = time.time()
  80. main()
  81. toc = time.time()
  82. print "Elapsed time: %.3f sec" % float(toc-tic)