DANGER
📌 Note: This article uses Buildroot as the system template. Debian and Ubuntu have not been tested, so there may be changes when transferring to the development board.
1. Flash the Image
Flash a Buildroot image (downloadable from the Taishan Pi’s Baidu Cloud resources). Here, I’ll choose the HDMI Buildroot image:
Flash tutorial (if you're unsure, you can check here):
Successful flashing outcome:
2. Setting Up the Cross-Compilation Environment
I’ve already explained how to configure the cross-compilation environment in a Ubuntu 18.04.6 virtual machine in my Qt5 article【the steps are the same】.
You can check the section Qt5 Porting -> Install Cross Compiler (GNU)
【Click here🚀】 for details.
In the terminal, input:
aarch64-linux-gnu-gcc -v
The path within the box shows your cross-compilation toolchain directory. Make sure to remember it! You'll need it later.
3. Cross-Compiling
1. Download x264 Source Code
Option 1 [Download from the website]: https://www.videolan.org/developers/x264.html
Option 2 [Git pull](Recommended)
We'll use the Git method here:
git clone https://code.videolan.org/videolan/x264.git
2. Cross-compile x264
Go to the x264 directory:
cd x264/
mkdir x264build
Create a autoConfig.sh script for compiling:
touch autoConfig.sh
Edit the autoConfig.sh:
gedit autoConfig.sh
Inside the autoConfig.sh, write the following content:
Note: The TOOLCHAIN_DIR path is the one emphasized earlier when setting up the cross-compilation environment.
#!/bin/bash
# Define variables
# Set the installation location for x264
PREFIX="./x264build"
# Cross compiler bin path (use `aarch64-linux-gnu-gcc -v` to find it)
TOOLCHAIN_DIR="/opt/gcc-aarch64-linux-gnu/bin"
# Toolchain prefix for specifying cross-compilation tool paths
TOOLCHAIN="$TOOLCHAIN_DIR/aarch64-linux-gnu-"
# Run the configure script
echo "Configuring x264..."
./configure \
--prefix=$PREFIX \
--disable-asm \
--enable-shared \
--enable-static \
--host=aarch64-linux-gnu \
--cross-prefix=$TOOLCHAIN \
--enable-pic
# Check configure's return status
if [ $? -ne 0 ]; then
echo "Error: x264 configuration failed."
exit 1
else
echo "Configuration successful."
fi
# Compile x264
echo "Compiling x264..."
# Use nproc to determine the number of processor cores for parallel compilation
make -j$(nproc)
# Check compile's return status
if [ $? -ne 0 ]; then
echo "Error: x264 compilation failed."
exit 1
else
echo "Compilation successful."
fi
# Install x264
echo "Installing x264..."
make install
# Check installation's return status
if [ $? -ne 0 ]; then
echo "Error: x264 installation failed."
exit 1
else
echo "Installation successful."
fi
# Clean up build directory
echo "Cleaning up..."
make clean
# Check clean-up's return status
if [ $? -ne 0 ]; then
echo "Warning: Failed to clean the build directory."
else
echo "Build process completed."
fi
# Exit script normally
exit 0
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Save the file:
Then, close the file.
Grant execute permissions to autoConfig.sh:
chmod +x autoConfig.sh
Run the autoConfig.sh script:
./autoConfig.sh
A successful result will look like this:
You can now find the related folders in the x264/x264build folder:
Get the absolute path of the installation directory:pwd
WARNING
📌 Remember this path, as you’ll need it when compiling FFmpeg later!!!
Note: This is my path, but everyone’s path will be different. Be sure to use pwd to check yours!!!
3、Download Rockchip FFmpeg Source Code
Return to the parent directory of x264:
Then use the command to clone the Rockchip FFmpeg source:
git clone https://gitclone.com/github.com/nyanmisaka/ffmpeg-rockchip.git
4. Cross-compile FFmpeg
Navigate to the ffmpeg-rockchip directory:
cd ffmpeg-rockchip
Create a folder for FFmpeg build files:
mkdir ffmpeg_rk_build
Create a new autoConfig.sh script:
touch autoConfig.sh
Edit the autoConfig.sh script:
gedit autoConfig.sh
In the autoConfig.sh script, write the following content:
he X264_BUILD_DIR path was obtained earlier when cross-compiling x264, just copy and paste it. Be sure to change it! Make sure to change it! Make sure to change it! The path may be different for everyone!
# Define prefix directory
PREFIX="./ffmpeg_rk_build"
# Absolute path to x264 installation directory
X264_BUILD_DIR="/home/lipeng/share/TaiShanPai/x264/x264build"
# Absolute path to the include directory in x264 installation directory
X264_INCLUDE_DIR="$X264_BUILD_DIR/include"
# Absolute path to the lib directory in x264 installation directory
X264_LIB_DIR="$X264_BUILD_DIR/lib"
# Set the PKG_CONFIG_PATH environment variable
export PKG_CONFIG_PATH=$X264_LIB_DIR/pkgconfig:$PKG_CONFIG_PATH
echo "Configuring environment variables:"
./configure \
--prefix=$PREFIX \
--enable-shared \
--disable-static \
--enable-cross-compile \
--arch=arm64 \
--disable-stripping \
--target-os=linux \
--cc=aarch64-linux-gnu-gcc \
--enable-libx264 \
--extra-cflags=-I$X264_INCLUDE_DIR \
--extra-ldflags=-L$X264_LIB_DIR \
--pkg-config-flags="--static" \
--extra-libs="-lpthread -lm" \
--enable-gpl \
--enable-pic \
--enable-nonfree \
--enable-pthreads
#Check if ./configure was successful
if []; then
echo "./configure configuration successful!"
else
echo "./configure configuration failed! Please check your environment!!!"
fi
#Run make
echo "Compiling ffmpeg-rockchip..."
# Use nproc to get the number of processor cores to determine the number of parallel make processes
make -j$(nproc)
if []; then
echo "Make successful!!!"
else
echo "Make failed!! Please check the error message, resolve the issues, and try again!!"
exit 1
fi
#Run make install
echo "Installing ffmpeg-rockchip..."
make install
if []; then
echo "Make install successful!!!!"
echo "============================================="
echo "ffmpeg_rk has been installed in $PREFIX!"
echo "============================================="
else
echo "Make install failed!! Please check the error message, resolve the issues, and try again!!"
exit 1
fi
#Clean up the build directory
echo "Cleaning up..."
make clean
# Check the return status of the cleanup
if []; then
echo "Warning: Failed to clean the build directory."
else
echo "Build process completed."
fi
#Exit the script normally
exit 0
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
Then click save:
Grant autoConfig.sh script execute permissions:
chmod +x autoConfig.sh
Now run the autoConfig.sh script:
./autoConfig.sh
We enter the ffmpeg_rk_build directory and can see the relevant folders:
cd ./ffmpeg_rk_build/
4. Transfer x264 and ffmpeg to Taishan-RK3566
Next, we can use any transfer tool, as long as it can move the relevant files to the Taishan-RK3566 development board!! I am using a USB drive for the transfer.
1. Transfer x264
Copy all the contents from the /x264/x264build/bin folder to the /usr/bin/ directory of the development board.
Copy all the contents from the /x264/x264build/lib folder to the /usr/lib/ directory of the development board.
2. Transfer ffmpeg
Copy all the contents from the /ffmpeg-rockchip/ffmpeg_rk_build/binfolder to the /usr/bin/ directory of the development board.
Copy all the contents from the /ffmpeg-rockchip/ffmpeg_rk_build/lib folder to the /usr/lib/ directory of the development board.
5. Testing
x264 Testing
x264 --help
ffmpeg Testing
Check version information:
ffmpeg -version
Video Recording Test
The software decoding performance is not sufficient, so performance limitations were imposed on the format. Here, we will record a 5-second video on a timer!
ffmpeg -f v4l2 -i /dev/video0 -vf scale=960:540 -c:v libx264 -preset ultrafast -tune zerolatency -crf 30 -b:v 50k -r 15 -t 5 output.mp4
-f v4l2
: This option tellsffmpeg
the format of the input stream.v4l2
refers toVideo for Linux 2
, which is the driver interface for video devices on Linux. This meansffmpeg
will read the video stream in thev4l2
format.-i /dev/video0
: This specifies the input file or device. In this case,/dev/video0
is a file name pointing to a video device, typically aUSB
camera or an onboard camera connected to the system.-vf scale=960:540
: This specifies the video filter (vf
stands forvideo filter
). Thescale
filter is used to change the size of the video.960:540
means the output video will be resized to 960x540 pixels.-c:v libx264
: This specifies the video codec.c:v
stands forcodec:video
, andlibx264
is a high-qualityH.264
video encoding library. This means the video stream will be encoded usinglibx264
.-preset ultrafast
: This sets the encoding preset. Theultrafast
preset is used for the fastest possible encoding, sacrificing some compression efficiency. This is very useful for real-time applications or devices with limited resources.-tune zerolatency
: This option is used to tune the encoder for low-latency applications. It typically sacrifices further compression efficiency to improve responsiveness.-crf 30
:crf
stands for Constant Rate Factor, a unique option forlibx264
that balances video quality and file size. The lower the value, the better the video quality, but the larger the file.30
is a relatively high value, meaning the video quality will be compromised to reduce file size and encoding time.-b:v 50k
: This sets the video bitrate.50k
means the maximum average bitrate for video encoding is 50 kilobits per second. A lower bitrate can reduce file size but typically results in lower video quality.-r 15
: This sets the video frame rate.15
means the video will be encoded at 15 frames per second. Lowering the frame rate can reduce CPU load and file size.-t 5
: This sets the video duration.5
means the video will be encoded to a length of5
seconds. This is useful for testing and debugging as it prevents indefinite encoding.output.mp4
: This is the output file name.ffmpeg
will save the encoded video to anoutput.mp4
file namedMP4
.
You can play the output.mp4 video file on a Windows computer.
If you have connected a screen to the development board (I have connected via HDMI), you can directly run the following command to play this MP4 video file on the screen:
gst-launch-1.0 filesrc location=output.mp4 ! decodebin ! autovideosink