AhFei

AhFei

简洁的写作需要勇气

BBDown Download B Station Videos - A Convenient Command Line Tool

If there are no separate 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 downloader for Bilibili. (github.com)

Below is a demonstration of its integration with a web notepad for convenient daily use.

image

Installation and Configuration#

Install prerequisite tools

sudo apt install -y wget unzip curl

Install BBDown#

Run the following command to install it (repeat the command to update).

# 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 to 2 seconds for multiple parts
--delay-per-page
2

# Enable danmaku download feature
--download-danmaku
# Skip subtitle download
--skip-subtitle

You can also specify the download directory in the configuration file by adding the following to the BBDown.config file.

# 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 the following command in the command line:

BBDown login   # Requires APP scan

Then you can download videos.

The video link can be the complete URL "https://www.bilibili.com/video/BV1Ee411u7hm" or just the part 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#

The previous content may not easily show the advantages of command-line tools over GUI tools, which are 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 you will save the video URLs.

#!/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 to 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

# Get one line at a time until the content 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's ENDLINE, exit the loop

done

/usr/local/bin/BBDown ${ENDLINE}   # Download the last line URL; if there's 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 links on your local computer first, and then use the script to download everything at once on the server or NAS, which is very convenient.

Integration with Web Notepad#

The drawback of command-line tools is that they have a certain threshold for use and are inconvenient for daily fragmented use. If you only download one or two videos each time, you still need to SSH to edit files and execute scripts, which can be cumbersome.

There was a previous blog post 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 change the BBLIST variable in the script to the corresponding file? This way, you can automatically execute the script to download videos every so often, saving you the hassle. (In fact, AhFei uses curl to fetch content locally instead of deploying a web notepad on the local machine.)

However, this approach is not very real-time. AhFei changes the download directory to Emby's media library to watch through Emby. This way, high-quality videos are saved directly, and after watching, unnecessary ones can be deleted without worrying about the videos being taken down, nor needing to set aside time to download and collect videos.


Similar to the above script process:

filename="bbdl.sh"
touch $filename && chmod +x $filename && vim $filename
vim bbdl.sh

In addition to BBLIST, a WEBLIST variable is 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 to 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

# Get one line at a time until the content 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's ENDLINE, exit the loop

done

/usr/local/bin/BBDown ${ENDLINE}   # Download the last line URL; if there's only one link, it will download twice
# Send completion status 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 needing to edit files and copy links.

bash -ex ./bbdl.sh

Finally, let me share how I watch Bilibili.

To avoid excessive scrolling, I don't have the Bilibili app on my phone; instead, I watch it on the computer through the web, focusing only on the UPs I follow. However, this also carries the risk of scrolling, especially with distractions from the homepage.

So, I generate an RSS feed of my personal updates through RSSHub, discover updates via an RSS reader, then directly jump to the video page and save the links to the web notepad. I download automatically every 4 hours, and then I can watch on Emby, especially using the Emby client on my phone, avoiding distractions from various ads and methods on commercial platforms while maintaining independence and proactivity in information intake. (Having such habits subtly cultivates awareness, thereby enhancing "immunity.")


In the past, I used a Bilibili downloader with a user interface: leiurayer/downkyi: Bilibili Download Tool, supports batch downloads, 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 are original by AhFei unless otherwise stated, 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 via RSS and leave comments for corrections.

You can join the Telegram group https://t.me/vfly2 to discuss any issues encountered during the article's steps.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.