Last active
August 29, 2015 13:58
-
-
Save inaz2/10379124 to your computer and use it in GitHub Desktop.
wgetとavconvでYouTube URLをM4A (AAC) ファイルに変換
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python yt2m4a.py "http://www.youtube.com/watch?v=4YMD6xELI_k" | |
--2014-04-11 02:25:26-- http://r2---sn-ogueyn7z.googlevideo.com/videoplayback?sparams=id%2Cip%2Cipbits%2Citag%2Cpcm2fr%2Cratebypass%2Csource%2Cupn%2Cexpire&ipbits=0&mt=1397150542&expire=1397173854&upn=15FckWnBsJw&fexp=927905%2C936905%2C901065%2C916623%2C945513%2C937417%2C913434%2C936916%2C934022%2C936921%2C936923&signature=A2B6A3A92CCB9F6F09EDEC21266137C2A09975DF.A94CC65958FD475F79CE18DDF05188C29591C7A0&key=yt5&ip=123.224.99.3&pcm2fr=yes&itag=22&source=youtube&sver=3&mv=u&ratebypass=yes&ms=au&id=o-ADTX4MBPHtBDylBsjOsDle1msEAsW4PTJ7EyQH0qyPo1 | |
Resolving r2---sn-ogueyn7z.googlevideo.com (r2---sn-ogueyn7z.googlevideo.com)... 74.125.171.39 | |
Connecting to r2---sn-ogueyn7z.googlevideo.com (r2---sn-ogueyn7z.googlevideo.com)|74.125.171.39|:80... connected. | |
HTTP request sent, awaiting response... 302 Found | |
Location: http://r13---sn-ogueyn7z.googlevideo.com/videoplayback?sparams=id%2Cip%2Cipbits%2Citag%2Cpcm2fr%2Cratebypass%2Csource%2Cupn%2Cexpire&ipbits=0&mt=1397150542&expire=1397173854&upn=15FckWnBsJw&fexp=927905%2C936905%2C901065%2C916623%2C945513%2C937417%2C913434%2C936916%2C934022%2C936921%2C936923&signature=A2B6A3A92CCB9F6F09EDEC21266137C2A09975DF.A94CC65958FD475F79CE18DDF05188C29591C7A0& | |
key=yt5&ip=123.224.99.3&pcm2fr=yes&itag=22&source=youtube&sver=3&mv=u&ratebypass=yes&ms=au&id=o-ADTX4MBPHtBDylBsjOsDle1msEAsW4PTJ7EyQH0qyPo1&ir=1&rr=12 [following] | |
--2014-04-11 02:25:27-- http://r13---sn-ogueyn7z.googlevideo.com/videoplayback?sparams=id%2Cip%2Cipbits%2Citag%2Cpcm2fr%2Cratebypass%2Csource%2Cupn%2Cexpire&ipbits=0&mt=1397150542&expire=1397173854&upn=15FckWnBsJw&fexp=927905%2C936905%2C901065%2C916623%2C945513%2C937417%2C913434%2C936916%2C934022%2C936921%2C936923&signature=A2B6A3A92CCB9F6F09EDEC21266137C2A09975DF.A94CC65958FD475F79CE18DDF05188C29591C7A0&key=yt5&ip=123.224.99.3&pcm2fr=yes&itag=22&source=youtube&sver=3&mv=u&ratebypass=yes&ms=au&id=o-ADTX4MBPHtBDylBsjOsDle1msEAsW4PTJ7EyQH0qyPo1&ir=1&rr=12 | |
Resolving r13---sn-ogueyn7z.googlevideo.com (r13---sn-ogueyn7z.googlevideo.com)... 74.125.171.50 | |
Connecting to r13---sn-ogueyn7z.googlevideo.com (r13---sn-ogueyn7z.googlevideo.com)|74.125.171.50|:80... connected. | |
HTTP request sent, awaiting response... 200 OK | |
Length: 41464901 (40M) [video/mp4] | |
Saving to: `Google Maps: Pokémon Challenge.mp4' | |
100%[===========================================================================================================================================================>] 41,464,901 7.21M/s in 5.6s | |
2014-04-11 02:25:32 (7.06 MB/s) - `Google Maps: Pokémon Challenge.mp4' saved [41464901/41464901] | |
avconv version 0.8.10-4:0.8.10-0ubuntu0.12.04.1, Copyright (c) 2000-2013 the Libav developers | |
built on Feb 6 2014 20:56:59 with gcc 4.6.3 | |
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'Google Maps: Pokémon Challenge.mp4': | |
Metadata: | |
major_brand : mp42 | |
minor_version : 0 | |
compatible_brands: isommp42 | |
creation_time : 2014-03-28 21:36:51 | |
Duration: 00:02:33.41, start: 0.000000, bitrate: 2162 kb/s | |
Stream #0.0(und): Video: h264 (High), yuv420p, 1280x720 [PAR 1:1 DAR 16:9], 1967 kb/s, 23.98 fps, 23.98 tbr, 24k tbn, 47.95 tbc | |
Stream #0.1(und): Audio: aac, 44100 Hz, stereo, s16, 191 kb/s | |
Metadata: | |
creation_time : 2014-03-28 21:36:54 | |
Output #0, ipod, to 'Google Maps: Pokémon Challenge.m4a': | |
Metadata: | |
major_brand : mp42 | |
minor_version : 0 | |
compatible_brands: isommp42 | |
creation_time : 2014-03-28 21:36:51 | |
encoder : Lavf53.21.1 | |
Stream #0.0(und): Audio: aac, 44100 Hz, stereo, 191 kb/s | |
Metadata: | |
creation_time : 2014-03-28 21:36:54 | |
Stream mapping: | |
Stream #0:1 -> #0:0 (copy) | |
Press ctrl-c to stop encoding | |
size= 3648kB time=153.41 bitrate= 194.8kbits/s | |
video:0kB audio:3596kB global headers:0kB muxing overhead 1.455482% | |
$ ls | |
Google Maps: Pokémon Challenge.m4a Google Maps: Pokémon Challenge.mp4 yt2m4a.py | |
$ file Google* | |
Google Maps: Pokémon Challenge.m4a: ISO Media, MPEG v4 system, iTunes AAC-LC | |
Google Maps: Pokémon Challenge.mp4: ISO Media, MPEG v4 system, version 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import re | |
import urllib2 | |
import urllib | |
import subprocess | |
def yt2m4a(url): | |
f = urllib2.urlopen(url) | |
html = f.read() | |
f.close() | |
if not html: | |
raise Exception('error to get html') | |
m = re.search(r'"title": "([^"]+)"', html) | |
if m is None: | |
raise Exception('error to get title') | |
title = m.group(1).decode('unicode-escape') | |
m = re.search(r'"url_encoded_fmt_stream_map": "([^"]+)"', html) | |
if m is None: | |
raise Exception('error to get url_encoded_fmt_stream_map') | |
params = [x for x in m.group(1).split(',') if 'itag=22' in x] or [x for x in m.group(1).split(',') if 'itag=18' in x] | |
if not params: | |
raise Exception('error to get fmt=22 and fmt=18') | |
m = re.search(r'url=(.+)\\u0026', params[0]) | |
if m is None: | |
raise Exception('error to get video url') | |
video_url = urllib.unquote(m.group(1)) | |
filename = title.replace('/', '_').replace('\x00', '_') | |
subprocess.check_call(['wget', '-O', filename+'.mp4', video_url]) | |
subprocess.check_call(['avconv', '-i', filename+'.mp4', '-acodec', 'copy', '-vn', filename+'.m4a']) | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print >>sys.stderr, "Usage: python %s URL" % sys.argv[0] | |
sys.exit(1) | |
url = sys.argv[1] | |
yt2m4a(url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment