分类
编程

2019-12-01-twitter视频下载

今天在twitter上面看到一个好玩的视频,我就想下载下来,结果折腾了几个小时。

首先看网页代码,视频那里是一个blob:

<video preload="none" playsinline="" aria-label="Embedded video" poster="https://pbs.twimg.com/ext_tw_video_thumb/1200926756321951745/pu/img/l-V1sNMeAZL5tcGN.jpg" src="blob:https://twitter.com/dd866381-2ada-4c2b-80af-9cec2a580039" style="width: 100%; height: 100%; position: absolute; background-color: black; top: 0%; left: 0%; transform: rotate(0deg) scale(1.005);"></video>

我google了一下,这是流媒体的格式,要下载的话需要找到对应的m3u8文件,就是在chrom浏览器的Network里面过滤m3u8. 看到一个https://video.twimg.com/ext_tw_video/1200926756321951745/pu/pl/460×258/Kuir9CTQnfaw-EWA.m3u8

Google说可以用youtube-dl下载,然后我先去下载这个玩意,因为还有同学说可以直接用ffmpeg转换格式。

等youtube-dl下载完了之后,我运行下面的命令:

./youtube-dl -f mp4 -o baby.mp4 https://video.twimg.com/ext_tw_video/1200926756321951745/pu/pl/460x258/Kuir9CTQnfaw-EWA.m3u8

结果提示需要ffmpeg或者avconv,尼玛还是得安装ffmpeg。

然后我就老实的brew install ffmpeg。完事之后。

运行:

./youtube-dl -f mp4 -o baby.mp4 https://video.twimg.com/ext_tw_video/1200926756321951745/pu/pl/460x258/Kuir9CTQnfaw-EWA.m3u8

结果卡在那儿不动弹。

我就试一下直接用ffmpeg转换:

ffmpeg -i 'https://video.twimg.com/ext_tw_video/1200926756321951745/pu/pl/460x258/Kuir9CTQnfaw-EWA.m3u8' -vcodec copy -acodec copy -absf aac_adtstoasc o.mp4

也是一样的,下到一半卡在那儿不动弹。

我就换了个思路,想先看看m3u8里面的内容,

wget https://video.twimg.com/ext_tw_video/1200926756321951745/pu/pl/460x258/Kuir9CTQnfaw-EWA.m3u8
cat Kuir9CTQnfaw-EWA.m3u8

结果是这样:

#EXTM3U
#EXT-X-VERSION:6
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-TARGETDURATION:4
#EXT-X-PLAYLIST-TYPE:VOD
#EXT-X-ALLOW-CACHE:YES
#EXTINF:3.000,
/ext_tw_video/1200926756321951745/pu/vid/0/3000/460x258/G5KaOzbFFR97qdzl.ts
#EXTINF:3.000,
/ext_tw_video/1200926756321951745/pu/vid/3000/6000/460x258/4lEqbRtnJ2YtXHRw.ts
#EXTINF:3.000,
/ext_tw_video/1200926756321951745/pu/vid/6000/9000/460x258/uSvCKT4MckCDGKSf.ts
#EXTINF:3.000,
/ext_tw_video/1200926756321951745/pu/vid/9000/12000/460x258/QSnzs6dHUar2OXfY.ts
#EXTINF:3.000,
/ext_tw_video/1200926756321951745/pu/vid/12000/15000/460x258/zdytmI8intABhyFW.ts
#EXTINF:3.000,
/ext_tw_video/1200926756321951745/pu/vid/15000/18000/460x258/Gh8owrBVf_s8SwDs.ts
#EXTINF:3.000,
/ext_tw_video/1200926756321951745/pu/vid/18000/21000/460x258/GWnvSye6J8mkjr35.ts
#EXTINF:3.000,
/ext_tw_video/1200926756321951745/pu/vid/21000/24000/460x258/Bmd-gN7sTAtj_bme.ts
#EXTINF:3.000,
/ext_tw_video/1200926756321951745/pu/vid/24000/27000/460x258/lkTRSPSKrGpFB_h6.ts
#EXTINF:3.000,
/ext_tw_video/1200926756321951745/pu/vid/27000/30000/460x258/DcZQHhRTJOJ-HNyJ.ts
#EXTINF:3.000,
/ext_tw_video/1200926756321951745/pu/vid/30000/33000/460x258/-3GD9_NdfoDUVF9q.ts
#EXTINF:3.000,
/ext_tw_video/1200926756321951745/pu/vid/33000/36000/460x258/WuuI_REDOMnYjzSU.ts
#EXTINF:3.958,
/ext_tw_video/1200926756321951745/pu/vid/36000/39958/460x258/AZrLcuvgp5XsaOcU.ts
#EXT-X-ENDLIST

我就写了个脚本wget.sh先把这个视频下载下来再转换:

wget https://video.twimg.com/ext_tw_video/1200926756321951745/pu/vid/0/3000/460x258/G5KaOzbFFR97qdzl.ts
wget https://video.twimg.com/ext_tw_video/1200926756321951745/pu/vid/3000/6000/460x258/4lEqbRtnJ2YtXHRw.ts
wget https://video.twimg.com/ext_tw_video/1200926756321951745/pu/vid/6000/9000/460x258/uSvCKT4MckCDGKSf.ts
wget https://video.twimg.com/ext_tw_video/1200926756321951745/pu/vid/9000/12000/460x258/QSnzs6dHUar2OXfY.ts
wget https://video.twimg.com/ext_tw_video/1200926756321951745/pu/vid/12000/15000/460x258/zdytmI8intABhyFW.ts
wget https://video.twimg.com/ext_tw_video/1200926756321951745/pu/vid/15000/18000/460x258/Gh8owrBVf_s8SwDs.ts
wget https://video.twimg.com/ext_tw_video/1200926756321951745/pu/vid/18000/21000/460x258/GWnvSye6J8mkjr35.ts
wget https://video.twimg.com/ext_tw_video/1200926756321951745/pu/vid/21000/24000/460x258/Bmd-gN7sTAtj_bme.ts
wget https://video.twimg.com/ext_tw_video/1200926756321951745/pu/vid/24000/27000/460x258/lkTRSPSKrGpFB_h6.ts
wget https://video.twimg.com/ext_tw_video/1200926756321951745/pu/vid/27000/30000/460x258/DcZQHhRTJOJ-HNyJ.ts
wget https://video.twimg.com/ext_tw_video/1200926756321951745/pu/vid/30000/33000/460x258/-3GD9_NdfoDUVF9q.ts
wget https://video.twimg.com/ext_tw_video/1200926756321951745/pu/vid/33000/36000/460x258/WuuI_REDOMnYjzSU.ts
wget https://video.twimg.com/ext_tw_video/1200926756321951745/pu/vid/36000/39958/460x258/AZrLcuvgp5XsaOcU.ts

下载下来之后,因为m3u8里面的路径也要改一下:
把前面的“/ext_tw_video/1200926756321951745/pu/vid/33000/36000/460×258/”这一串也去掉。变成k.m3u8。

#EXTM3U
#EXT-X-VERSION:6
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-TARGETDURATION:4
#EXT-X-PLAYLIST-TYPE:VOD
#EXT-X-ALLOW-CACHE:YES
#EXTINF:3.000,
G5KaOzbFFR97qdzl.ts
#EXTINF:3.000,
4lEqbRtnJ2YtXHRw.ts
#EXTINF:3.000,
uSvCKT4MckCDGKSf.ts
#EXTINF:3.000,
QSnzs6dHUar2OXfY.ts
#EXTINF:3.000,
zdytmI8intABhyFW.ts
#EXTINF:3.000,
Gh8owrBVf_s8SwDs.ts
#EXTINF:3.000,
GWnvSye6J8mkjr35.ts
#EXTINF:3.000,
Bmd-gN7sTAtj_bme.ts
#EXTINF:3.000,
lkTRSPSKrGpFB_h6.ts
#EXTINF:3.000,
DcZQHhRTJOJ-HNyJ.ts
#EXTINF:3.000,
-3GD9_NdfoDUVF9q.ts
#EXTINF:3.000,
WuuI_REDOMnYjzSU.ts
#EXTINF:3.958,
AZrLcuvgp5XsaOcU.ts
#EXT-X-ENDLIST

然后再来用ffmpeg -i k.m3u8 k.mp4,就搞定了。

总结一下, ffmpeg这个肯定是需要安装的,youtube-dl是调用这个的。如果网络给力的话,youtube-dl应该可以直接完成转换,但是我试了两次就没成功,所以用wget先把ts文件下载下来再转换。

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据