Created
October 8, 2024 03:59
-
-
Save xianminx/19b7bed22925822d98d9a60111446d4b to your computer and use it in GitHub Desktop.
西方文明史 搬运到 Youtube 和 Spotify
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
/** | |
* # 西方文明史 搬运到 Youtube 和 Spotify | |
* | |
* [西方文明史](https://ocw.aca.ntu.edu.tw/ntu-ocw/ocw/cou/100S218/1) 是台湾大学 歷史系 劉慧教授所讲。 | |
* 多年前在通勤路上听过一个学期,非常受教。 搬运到 Youtube 和 Spotify(音频) 方便大家观看。 | |
* | |
* 本課程將介紹西方文明在公元 1500 年之前上古、古典、中古時期的發展。課程從文明的起源、近東地區文字與城市的出現談起; | |
* 進入西方文明之典範的希臘、羅馬時期後,將以哲學思想、政治組織為重心;接著介紹基督... | |
*/ | |
import fs from "fs"; | |
import path from "path"; | |
import https from "https"; | |
import { execSync } from 'child_process'; | |
const titles = [ | |
"單元 1.課程介紹;城市與文明", | |
"單元 2.兩河與埃及", | |
"單元 3.希臘", | |
"單元 4.希臘", | |
"單元 5.羅馬", | |
"單元 6.羅馬", | |
"單元 7.基督教、伊斯蘭教", | |
"單元 8.卡洛林帝國瓦解;教宗與神聖羅馬皇帝;法蘭西地區的「封建制度」", | |
"單元 9.農業、商業發展", | |
"單元 10.英格蘭的共同法、大憲章與國會", | |
"單元 11.中古歐洲社會", | |
"單元 12.中古歐洲藝術", | |
"單元 13.中古歐洲學術:大學、士林哲學", | |
"單元 14.中古宗教發展至十六世紀宗教改革", | |
"單元 15.英法百年戰爭", | |
"單元 16.航海、遊歷", | |
]; | |
function getVideoUrl(seq) { | |
let formattedSeq = seq.toString().padStart(2, "0"); | |
return `https://ocw.aca.ntu.edu.tw/videos/100S218/100S218_CT${formattedSeq}V01.mp4`; | |
} | |
async function download() { | |
const downloadPromises = titles.map((_, i) => { | |
const url = getVideoUrl(i + 1); | |
const name = path.basename(url); | |
const file = fs.createWriteStream(name); | |
console.log(`Downloading ${name}`); | |
return new Promise((resolve, reject) => { | |
https | |
.get(url, (response) => { | |
response.pipe(file); | |
file.on("finish", () => { | |
file.close(); | |
console.log(`Downloaded: ${name}`); | |
resolve(); | |
}); | |
}) | |
.on("error", (err) => { | |
fs.unlink(name, () => {}); | |
console.error(`Error downloading ${url}: ${err.message}`); | |
reject(err); | |
}); | |
}); | |
}); | |
await Promise.all(downloadPromises); | |
} | |
// 下载 MP4; | |
// download(); | |
// 转译到 MP3 上传 Spotify | |
function extractAudio(mp4) { | |
const audio = mp4.replace(".mp4", ".mp3"); | |
const cmd = `ffmpeg -loglevel error -i ${mp4} -q:a 0 -map a ${audio}`; | |
try { | |
execSync(cmd, { stdio: 'inherit' }); | |
console.log(`Extracted audio: ${audio}`); | |
} catch (error) { | |
console.error(`exec error: ${error}`); | |
} | |
} | |
async function convertMp4ToMp3() { | |
try { | |
const files = await fs.promises.readdir("."); | |
const mp4Files = files.filter(file => file.endsWith(".mp4")); | |
for (const file of mp4Files) { | |
console.log(`start convert ${file}`); | |
extractAudio(file); | |
console.log(`convert ${file} done.`); | |
} | |
} catch (err) { | |
console.error(`Error reading directory: ${err.message}`); | |
} | |
} | |
// Uncomment to download videos | |
// download(); | |
// Convert downloaded MP4 files to MP3 | |
convertMp4ToMp3(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment