banner
moeyy

moeyy

一条有远大理想的咸鱼。
github
mastodon
email

Automatically upload sliced videos to a free CDN in China, accelerating video playback without any cost.

Note: When we watch movies that we have downloaded ourselves, they often lag due to network or bandwidth issues. Therefore, MoeClub, a great expert, spent half a month in seclusion and wrote a script that automatically transcodes and slices videos and uploads them to a domestic cdn. The script defaults to uploading to the language cdn and uses multi-threaded uploading. The default is 10 threads, which can greatly improve the playback speed of videos.

Update#

【2020.4.1】
Since the language `cdn` has fixed the upload bug, the default has been changed to Alibaba Cloud image bed.

Usage#

Github address: https://github.com/MoeClub/Note/tree/master/ffmpeg

1. Install ffmpeg

wget https://www.moerats.com/usr/down/ffmpeg/ffmpeg-git-$(getconf LONG_BIT)bit-static.tar.xz
tar xvf ffmpeg-git-*-static.tar.xz
mv ffmpeg-git-*/ffmpeg  ffmpeg-git-*/ffprobe /usr/local/bin/
rm -rf ffmpeg-git-*

2. Install the script

# Create a new /opt/ffmpeg folder to store scripts and other files
mkdir /opt/ffmpeg && cd $_
# Download and run the script
wget https://raw.githubusercontent.com/MoeClub/Note/master/ffmpeg/Install.sh
bash Install.sh

3. Start the player
After the video is sliced and the m3u8 file is generated, a player is required to play it. The upload script will automatically push the m3u8 file to the player's root directory, so they can be used together. Of course, if you have a player that can play m3u8 files, you don't need to set it up. Choose according to your own needs.

Install pip3:

# For CentOS 6 system
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
yum install python34 -y
wget https://bootstrap.pypa.io/get-pip.py
python3 get-pip.py

# For CentOS 7 system
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install python36 -y
wget https://bootstrap.pypa.io/get-pip.py
python3 get-pip.py

# For Debian/Ubuntu system
apt update
apt install python3-pip -y

Install dependencies:

# Go to the player source code folder
cd /opt/ffmpeg/Player
pip3 install tornado

Start in the background:

nohup python3 Player.py&

Note that the MasterKey value in the Player.py file must correspond to the Token value in the publish.sh file. Do not change it randomly.

4. Script usage

# Command example
bash /path/to/media.sh <filename_without_special_characters.mp4> [special_options]

# Special options: number; optional parameter, default is 0
# 0: Automatically calculate the appropriate slice size based on the file's bitrate. (The target file size is less than 20M, which is generally fine)
# 1: Force re-encoding, with a bitrate of 2400k. (Generally used for non-H264 encoding, file slicing, slower speed)
# 2: Custom slice time. When set to 2, the slice time is 3 seconds. When it is greater than or equal to 3, the slice time is the number of special options.

Usage example:

# Go to the directory where the video is located
cd /root/movies

# Default mode slicing
bash /opt/ffmpeg/media.sh rats.mp4
# Force re-encoding
bash /opt/ffmpeg/media.sh rats.mp4 1
# Ultra-fast mode, set a slice time of 2 seconds (when the source file is H264 encoded)
bash /opt/ffmpeg/media.sh rats.mp4 2

5. Player examples

# View all m3u8 files pushed to the player's side
http://ip:5866/Player/list

# Play the rats.m3u8 file in the root directory displayed in the list
http://ip:5866/Player/rats.m3u8

Upload to Yuque CDN#

Note: This solution is optional. Currently, Yuque provides a default free 10G storage. Please refer to the official instructions for details.

First, go to the Yuque official website and register an account → Link. Then, obtain the values of ctoken and session. Here is a rough method to obtain them, using Google Chrome as an example.

image

After logging in, press F12 to enter the console, select Network, click on any link file starting with yuque, and then select Cookies to see the required 2 parameters.

Then, copy the values of ctoken and session.

Next, check the Yuque script → Link, copy all the contents of the script and replace them in the upload.sh script. Fill in the values of ctoken and session, and save it. Refer to the above for the upload method.

Finally, if you are using a VPS, it is not recommended to force transcoding. Slicing alone is sufficient. Otherwise, if the CPU is occupied for a long time, the server may be banned. Basically, most mp4 files can be sliced directly.

Original article Rat's Blog

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