Advertisement
hitsujitmo

Automated 77days Image Generator

Aug 23rd, 2013
1,318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # edit 26/08/13
  5. # Fixed results 51 - 77 ( added &start-index=51 )
  6.  
  7. # requires PIL
  8.  
  9. import urllib
  10. import json
  11. import os
  12. import re
  13. import wave
  14. import struct
  15. import math
  16. import Image
  17.  
  18. mp4_dir = 'mp4'
  19. wav_dir = 'wav'
  20. img_dir = 'img'
  21.  
  22. if not os.path.exists(mp4_dir): os.makedirs(mp4_dir)
  23. if not os.path.exists(wav_dir): os.makedirs(wav_dir)
  24. if not os.path.exists(img_dir): os.makedirs(img_dir)
  25.  
  26. # get list of last 77 vids
  27.  
  28. url = 'http://gdata.youtube.com/feeds/api/users/pronunciationbook/uploads?max-results=50&alt=json'
  29. data = json.load(urllib.urlopen(url))['feed']['entry']
  30. url = 'http://gdata.youtube.com/feeds/api/users/pronunciationbook/uploads?max-results=27&start-index=51&alt=json'
  31. data.extend(json.load(urllib.urlopen(url))['feed']['entry'])
  32.  
  33.  
  34. for item in data:
  35.     title = item['title']['$t']
  36.     url   = item['link'][0]['href'].split('&', 1)[0]
  37.     id    = item['id']['$t'].split('/')[-1]
  38.  
  39.     match = re.search(r'^How to Pronounce (\d{1,2})$', title)
  40.     # not a countdown video
  41.     if not match: continue
  42.     # vid already downloaded
  43.     if os.path.exists('{0}/{1}-{2}.mp4'.format(mp4_dir, title, id)): continue
  44.  
  45.     uri = item['link'][0]['href'].split('&', 1)[0]
  46.  
  47.     print 'downloading ' + url
  48.     cmd = 'cd {0}; youtube-dl {1}'.format(mp4_dir, url)
  49.     os.popen(cmd, 'r').read()
  50.  
  51. # convert videos to wav
  52.  
  53. for f in os.listdir(mp4_dir):
  54.     matches = re.search(r'^How to Pronounce (\d{1,2}).*\.mp4$', f)
  55.     if matches and not os.path.exists('{0}/{1}.wav'.format(wav_dir, matches.group(1))):
  56.         cmd = 'ffmpeg -y -i "{0}/{1}" "{2}/{3}.wav"'.format(mp4_dir, matches.group(0), wav_dir, matches.group(1))
  57.         os.popen(cmd, 'r').read()
  58.  
  59. # generate image
  60.  
  61. img = Image.new('L', (77 * 2, 77 * 2), 'white')
  62.  
  63. def process_file(num):
  64.     global img
  65.     count = 308
  66.  
  67.     try:
  68.         w = wave.open(wav_dir + '/' + str(num) + '.wav', 'r')
  69.  
  70.     except IOError: return
  71.  
  72.     begin = w.getnframes() - 1839 * 308 - 13000 # nooodl's guestimate
  73.     w.readframes(begin)
  74.  
  75.     for i in xrange(0, count):
  76.         lc = 0
  77.  
  78.         f = w.readframes(1839)
  79.         l = struct.unpack('3678h', f)[::2]
  80.         for j in l:
  81.             lc += j * j
  82.  
  83.         lc /= 1839
  84.         lc = int(math.sqrt(lc))
  85.  
  86.         color = lc * 255 / 512
  87.  
  88.         x = i % 154
  89.         y = (num * 2) - 1 - (i / 154)
  90.  
  91.         img.putpixel((x, y), 255 - color)
  92.  
  93.  
  94. for i in xrange(1, 78):
  95.     process_file(i)
  96.  
  97. img.save(img_dir + '/output.bmp', 'BMP')
  98. img = img.resize((77 * 4, 77 * 4))
  99. img.save(img_dir + '/output-lrg.bmp', 'BMP')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement