Compile and install FFmpeg to the target version 4.4.4 with C3V Codec supported:
1 . Install building package
sudo apt update sudo apt install -y build-essential meson ninja-build python3-pip bison libglib2.0-dev flex git sudo apt install -y libmp3lame-dev libx264-dev libopus-dev libvpx-dev libssl-dev libfdk-aac-dev libv4l-dev sudo apt install -y nasm libsdl2-dev libxcb-xfixes0-dev libxcb-shm0-dev libsctp-dev libfdk-aac-dev liblz-dev liblzma-dev sudo pip3 install --upgrade meson
2 . Build and install openh264 :
Openh264 is a free software library for real-time encoding and decoding video streams in the H.264/MPEG-4 AVC format. If you want to use it with ffmpeg, you can do this step.
Get source code:
mkdir code cd code git clone https://github.com/cisco/openh264.git
Compile and install:
cd openh264 meson build --prefix=/usr sudo ninja -C build/ install
3 . Build ffmpeg and install it:
Gst source and extract it.
cd ~/code wget https://ffmpeg.org/releases/ffmpeg-4.4.4.tar.xz tar -xf ffmpeg-4.4.4.tar.xz
4 . Apply code patch for supporting C3V codec
Patch files are below. Please download files and copy files to root path of ffmpeg code folder extracted in step 3.
Apply patch
cd ~/code/ffmpeg-4.4.4 patch -p1 < 0001-swscale-x86-yuv2rgb-Fix-build-without-SSSE3.patch patch -p1 < 0002-avcodec-vaapi_h264-skip-decode-if-pic-has-no-slices.patch patch -p1 < 0003-libavutil-Fix-mips-build.patch patch -p1 < 0004-configure-add-extralibs-to-extralibs_xxx.patch patch -p1 < 0005-v4l2-hw-codec-support.patch patch -p1 < 0006-v4l2-enc-add-spspps-to-each-idr.patch
5 . Configure ,build and install in prefix path “/home/sunplus/code/install-ffmpeg
"
mkdir build cd build ../configure --prefix=/home/sunplus/code/install-ffmpeg \ --enable-static --enable-shared --enable-gpl --enable-nonfree \ --enable-sdl --enable-ffplay --disable-optimizations \ --enable-libv4l2 --enable-libopus --enable-openssl \ --enable-libfdk-aac --enable-libopenh264 --enable-libx264 \ --enable-libmp3lame \ --enable-debug \ --extra-cflags=-g make -j4 make install
6 . Set environment variable to run ffmpeg
Because ffmpeg install path is/home/sunplus/code/install-ffmpeg
, it needs to set LD_LIBRARY_PATH and PATH to execute ffmpeg.
export LD_LIBRARY_PATH=/home/sunplus/code/install-ffmpeg/lib:/usr/local/lib/aarch64-linux-gnu:$LD_LIBRARY_PATH export PATH=/home/sunplus/code/install-ffmpeg/bin:$PATH
7 . Running cmd example:
Encode video frame of webcam to be H264 video file.
I use logitech c270 usb webcam. When it is plugged in C3V usb port, you will see video device in /dev.
In my case, c270 usb webcam device is video0.
You can check the video format support list with “v4l-ctl” app. It can be installed by below command.
sudo apt install v4l-utils
Check command is below:
v4l2-ctl --list-formats-ext --device /dev/video0
I choose to use YUYV422 640x480 30fps video format to be the encode video source. Action command is below:
ffmpeg -f v4l2 -input_format yuyv422 -framerate 30 -video_size 640x480 -i /dev/video0 -vcodec h264_v4l2m2m -vframes 100 output.h264
If you want to play the encode file “output.h264”, you can use mplayer. It can be installed by below command.
sudp pat install mplayer
Then output.h264 can be played by
mplayer output.h264
You can also record video and separate the record file by time with fix video length. Action command is below:
ffmpeg -f v4l2 -input_format yuyv422 -framerate 30 -video_size 640x480 -i /dev/video0 -vcodec h264_v4l2m2m -spspps_to_idr 1 -f segment -segment_time 60 -map 0 -reset_timestamps 1 -strftime 1 -preset ultrafast -crf 23 output_%Y-%m-%d_%H:%M:%S.h264
Command argument -spspps_to_idr 1
will automatically add SPS and PPS to the second and subsequent recorded video files to prevent playback issues.
Decode H.264 to YUV
ffmpeg -benchmark -y -vcodec h264_v4l2m2m -i output.h264 ffout.yuv
JPEG Encode
ffmpeg -benchmark -y -pix_fmt nv12 -s 640x480 -i ffout.yuv -vcodec jpeg_v4l2m2m -vframes 5 ffout_%03d.jpg
JPEG Decode
ffmpeg -benchmark -y -vcodec jpeg_v4l2m2m -i ffout_001.jpg -vframes 1 ffout_001.yuv