Twitterの動画をダウンロード
できること: twitter の動画をダウンロードできる。
必要なこと: nodejs がなんとなくわかる。
問題がありそうなら記事消す。
nodejs で twitter の動画をダウンロードする。
なぜ nodejs なのかというと、普段から使っていて慣れているから。
twitter 側の仕様変更で使えなくなる可能性あり。
肝心の動画の url を取得する部分は GitHub - Kikobeats/get-social-video-url: Get video URL from a social link (Facebook/Twitter/Vimeo/etc). に依存する。
手順
nodejs 環境がある前提として
- 適当なフォルダを作って下の二つのファイル(package.json, index.js)を保存する。
index.js
のtwitterUrl
に動画が張られているツイートの url を入力する。- コマンドラインか何かで
npm i
と入力してエンターキー。 - なんか色々(browserless, download, get-social-video-url, puppeteer)ダウンロードされる。
- コマンドラインか何かで
npm start
と入力してエンターキー。 - 動画がダウンロードされて同じフォルダに保存される。
まとめて落としたり、ツイートの url の渡し方を改善する等までのやる気は起きなかった。
package.json
{ "name": "twivload", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "node ./index.js" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "browserless": "^5.18.19", "download": "^7.1.0", "get-social-video-url": "^2.1.0", "puppeteer": "^2.0.0" } }
index.js
const fs = require("fs"); const download = require("download"); const getSocialVideoUrl = require("get-social-video-url"); const browserless = require("browserless")(); // 動画がはっつけられているツイートの url を指定する。 const twitterUrl = "https://twitter.com/xxxx/status/xxxxxxxxxxxxxxxxxxxx"; // 例 // ダウンロードした動画ファイルに適当にファイル名を付ける。 const outputFilePath = Date.now().toString() + ".mp4"; (async (url, dist) => { try { // 動画 の url を探す /** * @type {string[]} 動画の url の配列。index が小さい順に大きいサイズの動画の url が格納される。 */ const videoQualities = await getSocialVideoUrl({ url: url, browserless }); // console.log(videoQualities); const videoUrl = videoQualities[0]; // ダウンロードする const video = await download(videoUrl); // 保存する fs.writeFile(dist, video, err => { if (err) process.exit(1); process.exit(); }); } catch (err) { console.log(err.message); process.exit(1); } })(twitterUrl, outputFilePath);