Skip to content

Instantly share code, notes, and snippets.

@IvanaGyro
Last active June 28, 2023 20:56
Show Gist options
  • Save IvanaGyro/e4dcbcc6781fbec911d57afc727dc447 to your computer and use it in GitHub Desktop.
Save IvanaGyro/e4dcbcc6781fbec911d57afc727dc447 to your computer and use it in GitHub Desktop.
Download video on NTU COOL
import os
import urllib.request
import threading
import sys
def filenames(type):
yield f'{type}-1080-init.m4s.mp4'
id = 1
while True:
yield f'{type}-1080-{id}.m4s'
id += 1
def download_m4s(base_url, type, video_name):
with open(f'{video_name}-{type}.mp4', 'wb') as f:
for filename in filenames(type):
url = base_url + filename
try:
response = urllib.request.urlopen(url)
except Exception as e:
print(e)
print(f'Fail {url} code: {response.getcode()}')
break
if response.getcode() == 200:
print(f'concat {filename}')
f.write(response.read())
else:
raise Exception(f'Fail {url} code: {response.getcode()}')
return
def download_video(base_url, video_name):
threads = [
threading.Thread(target=download_m4s,
args=(base_url, 'video', video_name)),
threading.Thread(target=download_m4s,
args=(base_url, 'audio', video_name)),
]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
exit_code = os.system(
f'ffmpeg -i "{video_name}-video.mp4" -i "{video_name}-audio.mp4" -c:v copy -c:a copy "{video_name}.mp4"'
)
if exit_code == 0:
os.remove(f'{video_name}-video.mp4')
os.remove(f'{video_name}-audio.mp4')
if __name__ == "__main__":
# base_url = "https://files-1.dlc.ntu.edu.tw/cool-video/202303/40a2b785-43a4-42fa-ab07-81908adf93a3/"
base_url = sys.argv[1]
# video_name = '20230301 Quantum Optics'
video_name = sys.argv[2]
download_video(base_url, video_name)
// Right click on the preview image and inspect the element. After opening the devtool, paste this script to the console.
(function () {
const div = document.querySelector(".vjs-poster");
let url = div.style.backgroundImage.match(/url\((.*?)\)/)[1];
url = url.replace("thumbnail.png", "");
const title = document.getElementById('VideoPlayerPageLayout_video-title__2ychH').innerText;
console.log(`python .\\download.py ${url} '${title}'`)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment