/examples/videx/vafroma3.py

http://echo-nest-remix.googlecode.com/ · Python · 105 lines · 70 code · 20 blank · 15 comment · 8 complexity · 5fc686a058ad536a75a112cf8a9a8a54 MD5 · raw file

  1. #!/usr/bin/env python
  2. # encoding: utf=8
  3. """
  4. vafroma3.py
  5. Re-synthesize video A using the segments of song A.
  6. Tries to use longer sequences of video by boosting the distance neighbors of similar 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 vafroma3.py <input_filename>
  18. Example:
  19. python vafroma3.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. last_index = 0
  51. collect = audio.AudioQuantumList()
  52. match_index = -1
  53. for a in self.segs:
  54. seg_index = a.absolute_context()[0]
  55. distances = self.get_distance_from(a)
  56. distances[seg_index] = sys.maxint
  57. if match_index < len(distances) -1:
  58. distances[match_index + 1] *= .3
  59. match_index = distances.index(min(distances))
  60. match = self.segs[match_index]
  61. print seg_index, match_index
  62. # make the length of the new seg match the length
  63. # of the old seg
  64. collect.append(match)
  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 = "aa3_" + 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)