用于视频下载/视频转音频/音频分割基于R语言的轻量流程化脚本

2020-10-27  本文已影响0人  生信摆渡

Author: Wang Jiahao

Date: 2020-10-27

GitHub: https://github.com/JiahaoWongg/Bilidown

Contact: jhaowong1998@sina.com

目录
目录

一、简介

话说这也算是第二个版本了,第一个版本 是写在新冠暴发之初,因无法返校无聊至极而写。第一版所实现的功能不多,因此一直有改进的想法。

脚本所实现的功能看标题大家也能猜出一二,构思由来已久,但我接触代码的世界在当时也才半年而已。随着知识的增加和实际需求的提出,便萌生了用R语言实现这些功能的想法了。

我比较喜欢听音乐,特别是周杰伦的歌,而其歌曲的扒带也是我的最爱,B站有许多优质的歌曲扒带视频,因此我就想要把这些视频转为音频,放到手机音乐播放器里。后来又实现了对音频文件的分割,并将这些过程流程化,你只需要提供视频链接和简单的参数的就可实现这些功能。总共也才 200来行代码,为了便于阅读,其中大概含有 3/4 的注释行和空行。

先说这么多,接下来具体介绍脚本所实现的功能。

二、功能介绍

根据提供视频链接 URL 来源的不同,将功能划分为两大功能模式:来源于命令行 CMD 和来源于文本文件。

① CMD模式

② 文件模式

如果对于代码和命令完全陌生的朋友,或许有些地方不明白,不过没关系,具体操和参数设置作将在下一节具体讲述。

三、环境配置

这一节是重头戏,也是最难的部分,使用不好的朋友要多读读。

另外,由于我电脑是 ==window== 系统,所以我只讲在 window 里的操作方法。

① 语言环境配置

需要的语言环境及工具:PythonRyou-getffmpeg,你已经有的就选择性安装

接下来一一介绍,不过首先讲一点,安装软件的时候要指定安装路径,不要总把软件放在C盘的Program Files里。安装的路径必须要知道,后面需要用到。比如我将D盘作为软件盘,找软件路径的时候非常方便:

② 安装 you-getffmpeg

  pip install you-get & pip install FFmpeg

运行:

  you-get --version & ffmpeg -version

前三行得到以下结果,则安装成功!

C:\Users\Jay Chou>you-get --version & ffmpeg -version
you-get: version 0.4.1456, a tiny downloader that scrapes the web.
ffmpeg version git-2020-01-15-0dc0837 Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 9.2.1 (GCC) 20200111

Python安装的软件不需要额外添加环境变量

至此,你已经完成了最难的部分,Congratulations !

三、脚本下载

访问:https://github.com/JiahaoWongg/Bilidown

在浏览器下载历史内,打开该文件所在路径:

解压,路径我设为:D:\,尽可能简单

将文件夹简化重命名为Bilidown

==以上设置即为初次使用该脚本所需的所环境配置和操作==

==以下为以后每次运次脚本时的流程==

四、 脚本运行测试

① 打开终端并切换工作路径

快捷键 Win + X + C 打开命令提示符面板

运行:

d: & cd \Bilidown

结果:

C:\Users\Jay Chou>d: & cd \Bilidown

D:\Bilidown>

② 脚本运行测试

运行:

Rscript bilidown.R -h

结果:

D:\Bilidown>Rscript bilidown.R -h
Usage: bilidown.R [options]


Options:
        --url=URL
                video URL.

        --list=LIST
                Whether download playlist.

        --mp3=MP3
                Whether tranfer to mp3.

        --file=FILE
                Whether get URL(s) from file.

        --mp3Folder=MP3FOLDER
                Mp3 output folder.

        --mp4Folder=MP4FOLDER
                Mp4 output folder.

        --name=NAME
                Rename output file.

        --start=START
                Start point of mp3 cut

        --end=END
                end point of mp3 cut

        -h, --help
                Show this help message and exit



D:\Bilidown>

出现这样的结果说明一切正常,其中``Options`即为运行脚本所需设的参数,可以阅读一下,根据提示差不多能看出其作用。

测试成功后我们就要进行真正的运行了,该脚本和其他命令行软件一样,都必须赋予指定格式的参数或文件,一点错误一般都是不允许的,或者结果可能会不是你想要的。所以下一节的参数设置也至关重要,决定了能不能得到你想要的结果!

接下来将一一解释每一参数的作用和如何设置

五、参数设置

第二节内容提到,该脚本有两个使用模式,URL来源于CMD模式URL来源于文件模式

因此参数设置也要分为这两部分讲解

首先要知道的是:

① CMD模式

Rscript bilidown.R -- url "https://www.bilibili.com/video/BV1dt4s11a7FX?from=search&seid=3497926814758339505"

别猴急运行,说运行了再运行

Rscript bilidown.R --url "https://www.bilibili.com/video/BV1dt411a7FX?from=search&seid=3497926814758339505" --list TRUE

插播一条:文件保存路径必须使用 绝对路径 ,windowns绝对路径以盘符开头,如:D:/Bilidown/MP4

Rscript bilidown.R --url "https://www.bilibili.com/video/BV1dt411a7FX?from=search&seid=3497926814758339505" --list TRUE --mp3 TRUE --mp3Folder D:/Bilidown/MP3 --mp4Folder D:/Bilidown/MP4 --name 牛逼脚本下载的东东

则输出文件为:牛逼脚本下载的东东.mp3牛逼脚本下载的东东.mp4

Rscript bilidown.R --url "https://www.bilibili.com/video/BV1JE411D74q?from=search&seid=12356753915334889298"  --mp3 TRUE --mp3Folder D:/Bilidown/MP3 --mp4Folder D:/Bilidown/MP4 --name 牛逼脚本下载的东东 --start 00:00:10 --end 00:01:10

这就是该模式下最完整的代码,整理好看一点:

Rscript bilidown.R --url "https://www.bilibili.com/video/BV1dt411a7FX?from=search&seid=3497926814758339505" 
                   --mp3 TRUE 
                   --mp3Folder D:/Bilidown/MP3 
                   --mp4Folder D:/Bilidown/MP4 
                   --name 牛逼脚本下载的东东 
                   --start 00:00:10 
                   --end 00:01:10

一目了然。注意我把list参数给去掉了,因为,如果你下载全P视频,那你对多个视频在同样的时间点进行切割显然是不符合逻辑的

② 文件模式

file文件格式要求

​ 由于既要考虑用户的体验而格式不能太复杂,同时又要考虑作者的能力范围,因为要实现多个视频的流程化处理,该文件内容稍许复杂,其实也不是很复杂,就是将CMD模式的一些参数值写入该文件中而已,需要注意的是文件内容格式必须遵守规定,具体规定如下:

"https://www.bilibili.com/video/BV1dt411a7FX?from=search&seid=3497926814758339505" TRUE no no 00:00:10 00:01:10
该模式参数设置

脚本会循环file文件的每一行,每一循环执行一次CMD模式

六、开始实战

记得要先进入脚本所在文件夹:

d: & cd Bilidown

①CMD模式

单P下载

命令:

Rscript bilidown.R --url "https://www.bilibili.com/video/BV1JE411D74q?from=search&seid=12356753915334889298"  --mp3 TRUE --mp3Folder D:/Bilidown/MP3 --mp4Folder D:/Bilidown/MP4 --name 周杰伦-秘密花园(扒带) --start 00:00:10 --end 00:01:10

屏幕显示的下载过程:

D:\Bilidown>Rscript bilidown.R --url "https://www.bilibili.com/video/BV1JE411D74q?from=search&seid=12356753915334889298"  --mp3 TRUE --mp3Folder D:/Bilidown/MP3 --mp4Folder D:/Bilidown/MP4 --name 周杰伦-秘密花园(扒带) --start 00:00:10 --end 00:01:10

Downloading video file ...
site:                Bilibili
title:               周杰伦《秘密花园》编曲扒带
stream:
    - format:        dash-flv720
      container:     mp4
      quality:       高清 720P
      size:          21.9 MiB (22968001 bytes)
    # download-with: you-get --format=dash-flv720 [URL]

Downloading 周杰伦《秘密花园》编曲扒带.mp4 ...
 100% ( 21.9/ 21.9MB) ├████████████████████████████████████████┤[2/2]  743 kB/s
Merging video parts... Merged into 周杰伦《秘密花园》编曲扒带.mp4

Skipping captions or danmaku.
Transcoding flv file to mp4 file
Transcoding mp4 file to mp3 file
Cutting mp3 file
Copying & rename files and remove tmpFolder
Done!

D:\Bilidown>

结果:

MP4 文件:

MP3文件:

可以看到时长只有一分钟,因为我取的是 00:00:10 至 00:01:10

多P下载

多P下载就不必重命名了

命令:

Rscript bilidown.R --url "https://www.bilibili.com/video/BV17t411K7et?from=search&seid=3759713561223284474"  --mp3 TRUE --list TRUE --mp3Folder D:/Bilidown/MP3 --mp4Folder D:/Bilidown/MP4 

屏幕显示的下载过程:

D:\Bilidown>Rscript bilidown.R --url "https://www.bilibili.com/video/BV17t411K7et?from=search&seid=3759713561223284474"  --mp3 TRUE --list TRUE --mp3Folder D:/Bilidown/MP3 --mp4Folder D:/Bilidown/MP4

Downloading video file ...
site:                Bilibili
title:               你能猜到这是周杰伦哪一首歌吗?答案在2P (P1. 你能猜到这是周杰伦哪一首歌吗?答案在2P)
stream:
    - format:        dash-flv720
      container:     mp4
      quality:       高清 720P
      size:          4.3 MiB (4464751 bytes)
    # download-with: you-get --format=dash-flv720 [URL]

Downloading 你能猜到这是周杰伦哪一首歌吗?答案在2P (P1. 你能猜到这是周杰伦哪一首歌吗?答案在2P).mp4 ...
 100% (  4.3/  4.3MB) ├████████████████████████████████████████┤[2/2]  473 kB/s
Merging video parts... Merged into 你能猜到这是周杰伦哪一首歌吗?答案在2P (P1. 你能猜到这是周杰伦哪一首歌吗?答案在2P).mp4

Skipping captions or danmaku.
site:                Bilibili
title:               你能猜到这是周杰伦哪一首歌吗?答案在2P (P2. 致敬科比_考古MV《天地一斗》)
stream:
    - format:        dash-flv720
      container:     mp4
      quality:       高清 720P
      size:          45.2 MiB (47435509 bytes)
    # download-with: you-get --format=dash-flv720 [URL]

Downloading 你能猜到这是周杰伦哪一首歌吗?答案在2P (P2. 致敬科比_考古MV《天地一斗》).mp4 ...
 100% ( 45.2/ 45.2MB) ├████████████████████████████████████████┤[2/2]   92 kB/s
Merging video parts... Merged into 你能猜到这是周杰伦哪一首歌吗?答案在2P (P2. 致敬科比_考古MV《天地一斗》).mp4

Skipping captions or danmaku.
Transcoding flv file to mp4 file
Transcoding mp4 file to mp3 file
Copying & rename files and remove tmpFolder
Done!

D:\Bilidown>

结果:

MP4文件:

MP3文件:

②文件模式

切记URL文件内容格式!!!

我准备了几个测试数据,保存在同一文件夹下的``test.txt`文件中,有5个链接,给与了不同的处理模式。大家也可以在当前文件夹下找到。在使用的时候按照这样的格式创建就行了,记得运行的代码文件名字要改成你自己写的。

运行代码:

Rscript bilidown.R --file D:/Bilidown/test.txt --mp3Folder D:/Bilidown/MP3 --mp4Folder D:/Bilidown/MP4 

屏幕显示的下载过程:

D:\Bilidown>Rscript bilidown.R --file D:/Bilidown/test.txt --mp3Folder D:/Bilidown/MP3 --mp4Folder D:/Bilidown/MP4

Processing serial number 1, Remian 4

Downloading video file ...
site:                Bilibili
title:               我把周杰伦婚礼进行曲扒带编出来了
stream:
    - format:        dash-flv
      container:     mp4
      quality:       高清 1080P
      size:          23.7 MiB (24804205 bytes)
    # download-with: you-get --format=dash-flv [URL]

Downloading 我把周杰伦婚礼进行曲扒带编出来了.mp4 ...
 100% ( 23.7/ 23.7MB) ├████████████████████████████████████████┤[2/2]  106 kB/s
Merging video parts... Merged into 我把周杰伦婚礼进行曲扒带编出来了.mp4

Skipping captions or danmaku.
Transcoding flv file to mp4 file
Transcoding mp4 file to mp3 file
Copying & rename files and remove tmpFolder
Done!

Processing serial number 2, Remian 3

Downloading video file ...
site:                Bilibili
title:               周杰伦秘密花园扒带伴奏!既然原版的不够清晰,那就自己做一个吧!
stream:
    - format:        dash-flv
      container:     mp4
      quality:       高清 1080P
      size:          18.2 MiB (19045299 bytes)
    # download-with: you-get --format=dash-flv [URL]

Downloading 周杰伦秘密花园扒带伴奏!既然原版的不够清晰,那就自己做一个吧!.mp4 ...
 100% ( 18.2/ 18.2MB) ├████████████████████████████████████████┤[2/2]  696 kB/s
Merging video parts... Merged into 周杰伦秘密花园扒带伴奏!既然原版的不够清晰,那就自己做一个吧!.mp4

Skipping captions or danmaku.
Transcoding flv file to mp4 file
Transcoding mp4 file to mp3 file
Copying & rename files and remove tmpFolder
Done!

Processing serial number 3, Remian 2

Downloading video file ...
site:                Bilibili
title:               周杰伦在快本给吴昕的三键成曲扒带
stream:
    - format:        dash-flv
      container:     mp4
      quality:       高清 1080P
      size:          31.1 MiB (32599288 bytes)
    # download-with: you-get --format=dash-flv [URL]

Downloading 周杰伦在快本给吴昕的三键成曲扒带.mp4 ...
 100% ( 31.1/ 31.1MB) ├████████████████████████████████████████┤[2/2]   96 kB/s
Merging video parts... Merged into 周杰伦在快本给吴昕的三键成曲扒带.mp4

Skipping captions or danmaku.
Transcoding flv file to mp4 file
Transcoding mp4 file to mp3 file
Copying & rename files and remove tmpFolder
Done!

Processing serial number 4, Remian 1

Downloading video file ...
site:                Bilibili
title:               周杰伦编曲扒带第一张专辑《反方向的钟》编曲扒带
stream:
    - format:        dash-flv
      container:     mp4
      quality:       高清 1080P
      size:          52.9 MiB (55428095 bytes)
    # download-with: you-get --format=dash-flv [URL]

Downloading 周杰伦编曲扒带第一张专辑《反方向的钟》编曲扒带.mp4 ...
 100% ( 52.9/ 52.9MB) ├████████████████████████████████████████┤[2/2]  100 kB/s
Merging video parts... Merged into 周杰伦编曲扒带第一张专辑《反方向的钟》编曲扒带.mp4

Skipping captions or danmaku.
Transcoding flv file to mp4 file
Transcoding mp4 file to mp3 file
Cutting mp3 file
Copying & rename files and remove tmpFolder
Done!

Processing serial number 5, Remian 0

Downloading video file ...
site:                Bilibili
title:               周杰伦 《蒲公英的约定》完整扒带还原-林迈可编曲 (P1. 蒲公英的约定)
stream:
    - format:        dash-flv
      container:     mp4
      quality:       高清 1080P
      size:          96.8 MiB (101538738 bytes)
    # download-with: you-get --format=dash-flv [URL]

Downloading 周杰伦 《蒲公英的约定》完整扒带还原-林迈可编曲 (P1. 蒲公英的约定).mp4 ...
 100% ( 96.8/ 96.8MB) ├████████████████████████████████████████┤[2/2]   90 kB/s
Merging video parts... Merged into 周杰伦 《蒲公英的约定》完整扒带还原-林迈可编曲 (P1. 蒲公英的约定).mp4

Skipping captions or danmaku.
site:                Bilibili
title:               周杰伦 《蒲公英的约定》完整扒带还原-林迈可编曲 (P2. 蒲公英的约定伴奏)
stream:
    - format:        dash-flv
      container:     mp4
      quality:       高清 1080P
      size:          61.7 MiB (64708427 bytes)
    # download-with: you-get --format=dash-flv [URL]

Downloading 周杰伦 《蒲公英的约定》完整扒带还原-林迈可编曲 (P2. 蒲公英的约定伴奏).mp4 ...
 100% ( 61.7/ 61.7MB) ├████████████████████████████████████████┤[2/2]  659 kB/s
Merging video parts... Merged into 周杰伦 《蒲公英的约定》完整扒带还原-林迈可编曲 (P2. 蒲公英的约定伴奏).mp4

Skipping captions or danmaku.
Transcoding flv file to mp4 file
Transcoding mp4 file to mp3 file
Copying & rename files and remove tmpFolder
Done!

D:\Bilidown>

每处理一个URL都会提示:Processing serial number 1, Remian 4,这告诉你当前处理第几个URL,还剩多少个等待处理。

结果:

MP4文件:

MP3文件:

虽然写URL文件有些麻烦,但现在看来是不是挺值得的😏


视频弹幕文件下载,并以txt格式输出

结果展示:

排在上面的是最新的发布的弹幕,越往下越晚

使用方法

运行

Rscript getChat.R --url "https://www.bilibili.com/video/BV1aW411y7Ap?from=search&seid=1744832395255800797" --name 测试 --outFolder Chats

对比


FFmpeg的一些常用功能

文件格式的转换,视音频的切割都是使用 ffmpeg 软件完成的,我只是加以包装整合。

如果小伙伴们想单独完成上述某一功能的话,ffmpeg 还需略懂一点的

功能介绍

同样也是在终端里运行,直接使用ffmpeg命令即可,具体常用功能如下

① 格式转换

ffmpeg -i xxx.format1 `xxx.format2`
ffmpeg -i test.flv `test.mp4`

② 视音频切割

另外,搜资料时看到一位老兄写道音视频还可以有转场效果,ffmpeg的功能真强大:

从0s开始淡入3s

ffmpeg -i input.mp3 -af "afade=t=in:ss=0:d=3" out.mp3

从50秒开始淡出5s

ffmpeg -i input.mp3 -af "afade=t=out:st=50:d=5" out.mp3

参数-af为音频滤镜,afade为音频淡入淡出。相应的如果是对视频的操作,这里的参数afade处可以使用fade

合并音视频

ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -lavfi hstack=inputs=3 output.mp4

需要一一指定输入文件和输入文件个数


如有疑问或纠错,请联系:jhaowong1998@sina.com

上一篇 下一篇

猜你喜欢

热点阅读