之前写过一篇文章:下载《极客时间》视频,只是自己使用就只使用工具完成。

后来有其他人也想使用,我觉得还是有必要写一个程序出来。

当然还是用 python 最简单了。

我们的任务很简单:

  1. 抓包 Charles 或者用 Fiddle
  2. 模拟浏览器请求 Postman
  3. 下载转换 FFmpeg
    $ ffmpeg -i input.m3u8 output.mp4
    

1. 抓包

请参考下载《极客时间》视频

Charles

复制request原始数据

2. 模拟浏览器请求

选择POST请求,Body类型选择 raw JSON(application/json)

postman_body

将上面从 Charles 复制的 Headers 粘贴到这里。点击 Send 得到 Response 数据。

postman_headers

点击 code,可以导出多种格式,这里我们选择 Python -> Requests

code

得到Python源码。

import requests

url = "https://time.geekbang.org/serv/v1/article"

payload = "{\"id\":5000}"
headers = {
    'Content-Type': "application/json",
    'Origin': "https://time.geekbang.org",
    'Accept-Encoding': "br, gzip, deflate",
    'Cookie': "_ga=****; _gat=****; _gid=****;GCID=****",
    'Connection': "keep-alive",
    'Accept': "application/json, text/plain, */*",
    'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1 Safari/605.1.15",
    'Referer': "https://time.geekbang.org/course/detail/77-5000",
    'Content-Length': "11",
    'Accept-Language': "zh-cn",
    'Cache-Control': "no-cache",
    'Postman-Token': "****"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)

运行后可以得到 json 格式数据,这是用 https://jsonlint.com 格式化后的结果。

{
	"error": [],
	"data": {
		"article_subtitle": "无",
		"video_time_arr": {
			"m": "03",
			"s": "57",
			"h": "00"
		},
		"audio_title": "",
		"video_cover": "",
		"audio_download_url": "",
		"audio_time": "0",
		"video_media": "{\"hd\":{\"size\":37284426,\"url\":\"https:\\\/\\\/res001.geekbang.org\\\***\\\/hd\\\/hd.m3u8\"},\"sd\":{\"size\":18428778,\"url\":\"https:\\\/\\\/res001.geekbang.org\\\/media\\\/video\\\***\\/sd\\\/sd.m3u8\"}}",
		"article_content": "<p><img src=\"https:\/\/static001.geekbang.org\/resource\/image\/5f\/dc\/5f12dff83e70008382527ae112facadc.png\" alt=\"\">\r\n<a href=\"http:\/\/promo.geekbang.org\/activity\/wegeektest?utm_source=App&utm_medium=course&utm_campaign=wegeektest\"><img src=\"https:\/\/static001.geekbang.org\/resource\/image\/98\/be\/98fc99774b9003a0edcca54cb1fa5cbe.png\" alt=\"\"><\/a>\n<!-- [[[read_end]]] -->\n",
		"like_count": 103,
		"video_height": 1080,
		"video_width": 1920,
		"audio_size": 0,
		"article_cover_hidden": true,
		"author_name": "无",
		"article_ctime": 1521718380,
		"id": 5000,
		"article_cover": "https:\/\/static001.geekbang.org\/resource\/image\/22\/be\/227093aa64405e2426c2d05a728cd1be.jpg",
		"audio_url": "",
		"had_liked": false,
		"column_is_experience": false,
		"video_total_seconds": 237,
		"audio_dubber": "",
		"video_play_seconds": 231,
		"had_viewed": true,
		"column_had_sub": true,
		"audio_md5": "",
		"article_sharetitle": "第1讲 | 小程序的背景—9小时搞定微信小程序开发",
		"video_play_utime": 1524313119,
		"cid": 77,
		"article_poster_wxlite": "https:\/\/static001.geekbang.org\/render\/screen\/f2\/3c\/f2c09f5ca359f3bb7a32a9f821dccb3c.jpeg",
		"article_summary": "无",
		"article_title": "第1讲 | 小程序的背景",
		"video_time": "00:03:57",
		"article_could_preview": true,
		"video_size": 30680399
	},
	"code": 0,
	"extra": []
}

3. 下载转换

最后就是编码了。源码请参考:https://github.com/iOSDevLog/geektime。欢迎 star

全部用 python 实现的有时间再写吧。