Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- # edit 26/08/13
- # Fixed results 51 - 77 ( added &start-index=51 )
- # requires PIL
- import urllib
- import json
- import os
- import re
- import wave
- import struct
- import math
- import Image
- mp4_dir = 'mp4'
- wav_dir = 'wav'
- img_dir = 'img'
- if not os.path.exists(mp4_dir): os.makedirs(mp4_dir)
- if not os.path.exists(wav_dir): os.makedirs(wav_dir)
- if not os.path.exists(img_dir): os.makedirs(img_dir)
- # get list of last 77 vids
- url = 'http://gdata.youtube.com/feeds/api/users/pronunciationbook/uploads?max-results=50&alt=json'
- data = json.load(urllib.urlopen(url))['feed']['entry']
- url = 'http://gdata.youtube.com/feeds/api/users/pronunciationbook/uploads?max-results=27&start-index=51&alt=json'
- data.extend(json.load(urllib.urlopen(url))['feed']['entry'])
- for item in data:
- title = item['title']['$t']
- url = item['link'][0]['href'].split('&', 1)[0]
- id = item['id']['$t'].split('/')[-1]
- match = re.search(r'^How to Pronounce (\d{1,2})$', title)
- # not a countdown video
- if not match: continue
- # vid already downloaded
- if os.path.exists('{0}/{1}-{2}.mp4'.format(mp4_dir, title, id)): continue
- uri = item['link'][0]['href'].split('&', 1)[0]
- print 'downloading ' + url
- cmd = 'cd {0}; youtube-dl {1}'.format(mp4_dir, url)
- os.popen(cmd, 'r').read()
- # convert videos to wav
- for f in os.listdir(mp4_dir):
- matches = re.search(r'^How to Pronounce (\d{1,2}).*\.mp4$', f)
- if matches and not os.path.exists('{0}/{1}.wav'.format(wav_dir, matches.group(1))):
- cmd = 'ffmpeg -y -i "{0}/{1}" "{2}/{3}.wav"'.format(mp4_dir, matches.group(0), wav_dir, matches.group(1))
- os.popen(cmd, 'r').read()
- # generate image
- img = Image.new('L', (77 * 2, 77 * 2), 'white')
- def process_file(num):
- global img
- count = 308
- try:
- w = wave.open(wav_dir + '/' + str(num) + '.wav', 'r')
- except IOError: return
- begin = w.getnframes() - 1839 * 308 - 13000 # nooodl's guestimate
- w.readframes(begin)
- for i in xrange(0, count):
- lc = 0
- f = w.readframes(1839)
- l = struct.unpack('3678h', f)[::2]
- for j in l:
- lc += j * j
- lc /= 1839
- lc = int(math.sqrt(lc))
- color = lc * 255 / 512
- x = i % 154
- y = (num * 2) - 1 - (i / 154)
- img.putpixel((x, y), 255 - color)
- for i in xrange(1, 78):
- process_file(i)
- img.save(img_dir + '/output.bmp', 'BMP')
- img = img.resize((77 * 4, 77 * 4))
- img.save(img_dir + '/output-lrg.bmp', 'BMP')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement