If there are no specific instructions, execute the commands in the order given in the article's code block one by one to achieve the goal.
Applicable systems: Debian-based distributions, including Ubuntu and Armbian; other distributions can generally adapt the commands slightly according to the process.
Estimated time to complete: 10 minutes
I might have made some mistakes, please let me know if I’ve gotten anything wrong!
BBDown is a command-line downloader for Bilibili.
GitHub: nilaoda/BBDown: Bilibili Downloader. A command-line Bilibili downloader. (github.com)
Below is a demonstration of its integration with a web notepad for convenient daily use.
Installation and Configuration#
Install prerequisite tools
sudo apt install -y wget unzip curl
Install BBDown#
You can install it by copying and running the command (to update, just run it again)
# Get the latest download link
github_project="nilaoda/BBDown"
tag=$(wget -qO- -t1 -T2 "https://api.github.com/repos/${github_project}/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')
browser_download_url=$(wget -qO- -t1 -T2 "https://api.github.com/repos/${github_project}/releases/latest" | grep "browser_download_url" | head -n 1 | awk -F "_" '{print $5}' | sed 's/\"//g;s/,//g;s/ //g')
# echo $browser_download_url
# Download
wget https://github.com/nilaoda/BBDown/releases/download/$tag/BBDown_${tag}_${browser_download_url}_linux-x64.zip
# Unzip and organize
unzip BBDown_*_linux-x64.zip && rm BBDown_*_linux-x64.zip
chmod +x BBDown
sudo mv BBDown /usr/local/bin/
Create a data file (if not created in advance, the .data file defaults to the same directory as the program, and ordinary users do not have write permissions in the program's directory, causing the login session to not be saved)
sudo touch /usr/local/bin/BBDown.data && \
sudo chown $USER:$USER /usr/local/bin/BBDown.data
Install dependency ffmpeg#
sudo apt update && sudo apt install -y ffmpeg
Check ffmpeg version to ensure successful installation
ffmpeg -version
Configuration#
For more configurations, please check the project's GitHub page
sudo vim /usr/local/bin/BBDown.config
# The program reads non-blank content line by line, for an option, its parameter should appear on the next line
# Set output file name format
--file-pattern
<videoTitle>
--multi-file-pattern
<videoTitle>/[P<pageNumberWithZero>]<pageTitle>
# Set the download interval for multiple parts to 2 seconds
--delay-per-page
2
# Enable bullet screen download function
--download-danmaku
# Skip subtitle download
--skip-subtitle
You can also specify the download directory in the configuration file by adding the following to the above file BBDown.config.
# Download directory
--work-dir
/home/vfly2/bilibili
Usage#
You need to log in first (login is required to download 1080p), valid for about 1 month. Run in the command line:
BBDown login # Requires APP QR code scan
Then you can download videos.
The video link can be a complete URL "https://www.bilibili.com/video/BV1Ee411u7hm", or just the segment BV1Ee411u7hm. The commands are as follows:
# Download a single video
BBDown BV1Ee411u7hm
BBDown "https://www.bilibili.com/video/BV1Ee411u7hm"
# Download a collection
BBDown https://space.bilibili.com/88895225/channel/collectiondetail?sid=39377
For more usage, please check the project's GitHub page
Batch Download#
It may not be easy to see from the previous content, but the advantage of command-line tools compared to those with user interfaces is their speed, convenience, and automation. Below is a small script to leverage its advantages.
Batch Download Script#
First, create a script file:
filename="bbdl.sh"
touch $filename && chmod +x $filename && vim $filename
Edit the content:
vim bbdl.sh
You need to modify the value of the BBLIST variable, which is the absolute path of the file where the video URLs will be saved.
#!/bin/bash
# download bilibili videos according to selected file containing urls
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# Path of the file containing video URLs, just modify this
BBLIST="/home/vfly2/bblist"
# Get the content of the specified line from the file, variable 1 is the file path, variable 2 is the line number
function grepline(){
url=$(cat ${1} | head -n ${2} | tail -n 1)
echo ${url}
}
LINE=1
URL=$(grepline ${BBLIST} ${LINE}) # Get the first line
ENDLINE=$(cat ${BBLIST} | tail -n 1) # Get the last line
# Each time get one line, until the content of the line is ENDLINE
until [ "${URL}" == "${ENDLINE}" ]
do
/usr/local/bin/BBDown ${URL}
# /usr/local/bin/BBDown ${URL} --work-dir /home/vfly2/bilibili # You can specify the download directory here
LINE=$((${LINE}+1)) # Increment by 1
URL=$(grepline ${BBLIST} ${LINE}) # Get the next line, if it is ENDLINE, exit the loop
done
/usr/local/bin/BBDown ${ENDLINE} # Download the last line URL, if there is only one link, it will download twice
Save the video links in bblist:
vim /home/vfly2/bblist
One link per line
BV1Ee411u7hm
BV1p34y1G79Q
Execute the script (default downloads to the current directory)
bash -ex ./bbdl.sh
This way, you only need to save all the links on your local computer first, and then use the script to download everything on the server or NAS with one command, which is very convenient.
Combining with Web Notepad#
The downside of command-line tools is that they have a certain threshold for use and are not convenient for daily fragmented usage. That is, if you only want to download one or two videos each time, you still need to SSH to edit files and execute scripts, which can be cumbersome.
This blog previously had an article introducing a web notepad: Sharing my minimalist web notepad used for two years - AhFei's Mistake (vfly2.com), which saves data in a single file.
So why not save links through the web notepad at any time, and then change the BBLIST variable in the script to the corresponding file? This way, the script can automatically execute every so often to download videos, saving you the hassle. (In fact, AhFei uses curl to fetch content locally, rather than deploying a web notepad on the local machine)
However, this way the real-time aspect is not so high. AhFei has changed the download directory to Emby's media library, allowing viewing through Emby. This way, high-quality videos are saved directly, and after watching, unnecessary ones can be deleted without worrying about videos being censored, and there is no need to take time to download and collect videos separately.
Similar to the script process above:
filename="bbdl.sh"
touch $filename && chmod +x $filename && vim $filename
vim bbdl.sh
In addition to BBLIST, a variable WEBLIST is also added, which is the URL of the web notepad. Here, we use the demonstration set up by AhFei: https://forward.vfly.app/bblist, copy the links here, one per line.
Readers can directly use this public instance or set up their own: Sharing my minimalist web notepad used for two years - AhFei's Mistake (vfly2.com)
#!/bin/bash
# download bilibili videos according to the file containing urls
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# Path of the file containing video URLs
BBLIST="/home/vfly2/bblist"
WEBLIST="https://forward.vfly.app/bblist"
# Download video links from the web page
curl -k $WEBLIST > $BBLIST
# Get the content of the specified line from the file, variable 1 is the file path, variable 2 is the line number
function grepline(){
url=$(cat ${1} | head -n ${2} | tail -n 1)
echo ${url}
}
LINE=1
URL=$(grepline ${BBLIST} ${LINE}) # Get the first line
ENDLINE=$(cat ${BBLIST} | tail -n 1) # Get the last line
# Each time get one line, until the content of the line is ENDLINE
until [ "${URL}" == "${ENDLINE}" ]
do
# echo ${URL}
/usr/local/bin/BBDown ${URL}
# /usr/local/bin/BBDown ${URL} --work-dir /home/vfly2/bilibili # You can specify the download directory here
LINE=$((${LINE}+1)) # Increment by 1
URL=$(grepline ${BBLIST} ${LINE}) # Get the next line, if it is ENDLINE, exit the loop
done
/usr/local/bin/BBDown ${ENDLINE} # Download the last line URL, if there is only one link, it will download twice
# Send a completion flag to the web notepad
echo "\nabove have done, but may skip" >> $BBLIST
curl -k --data-urlencode "text@${BBLIST}" $WEBLIST
This way, every time you SSH, you only need to run the command below without editing files or copying links.
bash -ex ./bbdl.sh
Finally, let me share how I watch Bilibili.
To prevent excessive scrolling, I do not have the Bilibili app on my phone, but watch it on the computer through the web, only checking the dynamics of the UPs I follow. However, there is still a risk of scrolling, especially with distractions from the homepage.
So, I generate a personal dynamics RSS through RSSHub, discover updates through an RSS reader, then directly jump to the video page, and save the links to the web notepad. Every 4 hours, it automatically downloads, and then I can watch it on Emby, especially using the Emby client on the phone, avoiding distractions from various ads and methods on commercial platforms, while maintaining independence and proactivity in information intake. (Having such habits will subtly cultivate awareness, thereby enhancing "immunity")
I have previously used a Bilibili downloader with a user interface: leiurayer/downkyi: Bilibili download tool, supports batch download, supports 8K, HDR, Dolby Vision, provides toolbox (audio and video extraction, watermark removal, etc.). (github.com)
You can log in to your account to download favorites, but if there are many videos, it can easily crash.
Original link: https://blog.vfly2.com/2023/10/bbdown-download-bilibili-videos/
Copyright statement: All articles on this blog, unless otherwise stated, are original works by AhFei, licensed under CC BY-NC-SA 4.0. Please indicate the source when reprinting AhFei's Mistake (blog.vfly2.com).
Stay updated ٩(•̤̀ᵕ•̤́๑)ᵒᵏᵎᵎᵎᵎ with clear and repeatable practical skills, feel free to subscribe using RSS and leave comments for corrections.
You can communicate in the Telegram group https://t.me/vfly2 regarding any issues encountered following the steps in the article.