Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yonkidoodle/98ee6f2cdc53a248e4c1bdf81b73f3a3 to your computer and use it in GitHub Desktop.
Save yonkidoodle/98ee6f2cdc53a248e4c1bdf81b73f3a3 to your computer and use it in GitHub Desktop.
Video Stream Downloader
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import m3u8\n",
"import requests\n",
"from bs4 import BeautifulSoup\n",
"from tqdm import tqdm_notebook as tqdm\n",
"import subprocess"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"sess = requests.Session()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"r = sess.get(\"https://www.iplt20.com/video/144829/final-csk-vs-srh-fbb-stylish-player-of-the-match-lungi-ngidi\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"soup = BeautifulSoup(r.content, 'html5lib')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"video_id = soup.find('video', attrs={'id': 'playlistPlayer'})['data-video-id']\n",
"account_id = soup.find('video', attrs={'id': 'playlistPlayer'})['data-account']"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"url = \"https://secure.brightcove.com/services/mobile/streaming/index/master.m3u8\"\n",
"\n",
"params = {\n",
" 'videoId': video_id,\n",
" 'pubId': account_id,\n",
" 'secure': True\n",
"}\n",
"\n",
"r = sess.get(url, params=params)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"m3u8_master = m3u8.loads(r.text)\n",
"m3u8_playlist_uris = [playlist['uri'] for playlist in m3u8_master.data['playlists']]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'media_sequence': None,\n",
" 'is_variant': True,\n",
" 'is_endlist': False,\n",
" 'is_i_frames_only': False,\n",
" 'is_independent_segments': False,\n",
" 'playlist_type': None,\n",
" 'playlists': [{'uri': 'http://c.brightcove.com/services/mobile/streaming/index/rendition.m3u8?assetId=5790251759001&pubId=3588749423001&videoId=5790241186001',\n",
" 'stream_info': {'program_id': 1,\n",
" 'bandwidth': 514000,\n",
" 'resolution': '480x270'}},\n",
" {'uri': 'http://c.brightcove.com/services/mobile/streaming/index/rendition.m3u8?assetId=5790251660001&pubId=3588749423001&videoId=5790241186001',\n",
" 'stream_info': {'program_id': 1,\n",
" 'bandwidth': 795000,\n",
" 'resolution': '640x360'}},\n",
" {'uri': 'http://c.brightcove.com/services/mobile/streaming/index/rendition.m3u8?assetId=5790250742001&pubId=3588749423001&videoId=5790241186001',\n",
" 'stream_info': {'program_id': 1,\n",
" 'bandwidth': 997000,\n",
" 'resolution': '640x360'}},\n",
" {'uri': 'http://c.brightcove.com/services/mobile/streaming/index/rendition.m3u8?assetId=5790247422001&pubId=3588749423001&videoId=5790241186001',\n",
" 'stream_info': {'program_id': 1,\n",
" 'bandwidth': 1297000,\n",
" 'resolution': '960x540'}},\n",
" {'uri': 'http://c.brightcove.com/services/mobile/streaming/index/rendition.m3u8?assetId=5790247421001&pubId=3588749423001&videoId=5790241186001',\n",
" 'stream_info': {'program_id': 1,\n",
" 'bandwidth': 1828000,\n",
" 'resolution': '960x540'}},\n",
" {'uri': 'http://c.brightcove.com/services/mobile/streaming/index/rendition.m3u8?assetId=5790251575001&pubId=3588749423001&videoId=5790241186001',\n",
" 'stream_info': {'program_id': 1,\n",
" 'bandwidth': 2134000,\n",
" 'resolution': '1280x720'}}],\n",
" 'segments': [],\n",
" 'iframe_playlists': [],\n",
" 'media': [],\n",
" 'keys': []}"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"m3u8_master.data"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"playlist_uri = m3u8_playlist_uris[0]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"r = sess.get(playlist_uri)\n",
"playlist = m3u8.loads(r.text)\n",
"m3u8_segment_uris = [segment['uri'] for segment in playlist.data['segments']]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1005ad9c7b554e0d958db3311377f489",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(IntProgress(value=0, max=9), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"with open(\"video.ts\", 'wb') as f:\n",
" for segment_uri in tqdm(m3u8_segment_uris):\n",
" r = sess.get(segment_uri)\n",
" f.write(r.content)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"CompletedProcess(args=['ffmpeg', '-i', 'video.ts', 'video.mp4'], returncode=0)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"subprocess.run(['ffmpeg', '-i', 'video.ts', 'video.mp4'])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment