Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nikhilkumarsingh/197dcb3e4bd81ef50dc28d565010ab18 to your computer and use it in GitHub Desktop.
Save nikhilkumarsingh/197dcb3e4bd81ef50dc28d565010ab18 to your computer and use it in GitHub Desktop.

Revisions

  1. nikhilkumarsingh created this gist Mar 22, 2019.
    64 changes: 64 additions & 0 deletions Video Stream Downloader (Example-1).ipynb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    {
    "cells": [
    {
    "cell_type": "code",
    "execution_count": 1,
    "metadata": {},
    "outputs": [],
    "source": [
    "import requests"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 2,
    "metadata": {},
    "outputs": [],
    "source": [
    "url = \"https://files3.lynda.com/secure/courses/786416/VBR_MP4h264_main_SD/786416_00_01_WL30_Batteries.mp4?_JlGPFuqEmh2WYnzy8KqP63YccR5B9TyiaLDUyJqMD8vI0FKsEoIijfxnOyVetNrjDj4fDCUidEarb6NlvSpp8gqOtp4MNS2E_14bhH483Nj8ekt9I0KwkiE6T4k39UnzitRKsZgt55bwl0MhP3aWp1j2U6IsuiqTBssp-Gug0_ZtYFcWvzsH5BN5EsA_A&c3.ri=3775645051486129780\""
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 3,
    "metadata": {},
    "outputs": [],
    "source": [
    "chunk_size = 256\n",
    "r = requests.get(url, stream=True)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 4,
    "metadata": {},
    "outputs": [],
    "source": [
    "with open(\"lynda.mp4\", \"wb\") as f:\n",
    " for chunk in r.iter_content(chunk_size=chunk_size):\n",
    " f.write(chunk)"
    ]
    }
    ],
    "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
    }
    229 changes: 229 additions & 0 deletions Video Stream Downloader (Example-2).ipynb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,229 @@
    {
    "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
    }