Showing posts with label ffmpeg. Show all posts
Showing posts with label ffmpeg. Show all posts

Monday, March 27, 2023

Convert a sequence of JPG images into a MP4 video using FFMPEG

If you have a sequence of images like the screen shot below, it is quite straightforward to use ffmpeg to concatenate them into a single video file. 


Obviously, you need to specify the image file name and sequence pattern, the frame rate to show each image as shown in the example ffmpeg command below.

$ ffmpeg -hide_banner \
-f image2 \
-framerate 1 \
-start_number 1 \
-i %02d.jpeg \
-vcodec mpeg4  \
output.mp4

where -f specifies the input format as image,

-framerate specifies the rate in Hz at which to display each image frame,

-start_number specifies the starting number of the image frame sequence,

-i specifies the file name and sequence pattern, e.g. %02d means a pattern of 2 digits with a prefix character '0' for single digit numbers,

-vcodec tells ffmpeg to output mpeg4 format.

 Running the command will display the following output and generate the output video file output.mp4.


Monday, November 7, 2022

Shell script to batch bulk convert *.flac files to *.mp3

I have many music files in flac format and I wanted to convert them to a more compressed mp3 format with ffmpeg on Ubuntu so I can upload them to a storage limited portable music player. To ease the conversion task, I decided to write this simple shell script to do the job. In brief, the script will do the following:

  • find all the files with the extension .flac in the current directory
  • replace the file name extension .flac with the .mp3 extension
  • create a temporary script that calls the ffmpeg command to convert
  • run the temporary script

The listing of the shell script is shown below.  

# Define the internal field separator as a newline
IFS=$'\n'

# Find all the *.flac files in the current directory and perform the conversion
for f in `find . -name "*.flac" `;
do
	# Use the input flac file name prefix and replace the .flac extension with a .mp3 extension
	f=$(echo $f | cut -c 3-)
	outfile=$(basename $f .flac)
	outfile=$outfile.mp3
	
	echo "Convert $f->$outfile..."
	
	# Form the ffmpeg command to convert the input flac file to mp3
	cmd="ffmpeg -hide_banner -i \"$f\" -ab 320k -map_metadata 0 -id3v2_version 3 \"$outfile\" "
	
	# Create a temporary shell script for running the conversion
	echo $cmd > /tmp/tmp.sh
	
	# Run the conversion to mp3
	bash /tmp/tmp.sh
	
	# Clean up
	rm /tmp/tmp.sh
done

To use this shell script, you can do the following:

  1. Save the code listing above to a file e.g. run.sh in a directory, e.g. /path/to
    /directory/


  2. Open up a Linux Terminal.

  3. In the Terminal, type in the command to change directory to the location of the flac files, e.g. /path/to/music/

    $ cd /path/to/music



  4. At the prompt, type in the command to run the shell script.

    $ bash /path/to/run.sh

    The flac files are converted to mp3 files.
 

Monday, March 21, 2022

Setup a Raspberry Pi 4B to publish a webcam with hardware accelerated video encoding

The Raspberry Pi 4B has an integrated video processing unit, a so called GPU. I wanted to use the GPU to offload the video encoding processing from the CPU when I publish my webcam as a RTSP video stream from the Pi board running Ubuntu 20.04 64 bit and using ffmpeg

Download and install rtsp-simple-server

  1. Using a browser, download and extract the open source rtsp-simple-server from https://github.com/aler9/rtsp-simple-server into a folder, e.g. /path/to/rtsp/

    The files rtsp-simple-server and rtsp-simple-server.yml are extracted out.

  2. Open up a Terminal. At the prompt, change directory to the previously created folder /path/to/rtsp/.

    $ cd /path/to/rtsp/

  3. Move the binary to the folder /usr/local/bin/

    $ sudo mv rtsp-simple-server /usr/local/bin/

  4. Move the yml configuration file to the folder /usr/local/etc/

    $ sudo mv rtsp-simple-server.yml /usr/local/etc/

Configure rtsp-simple-server to publish the webcam from ffmpeg

  1.  Open up a Terminal. At the prompt, change directory to the directory /usr/local/etc/.

    $ cd /usr/local/etc

  2. Using a text editor, edit the configuration file rtsp-simple-server.yml. Create a new path name, e.g. cam under the paths key.

    paths:
      cam:
        runOnDemand: ffmpeg -hide_banner -s 1280x720 -r 25 -i /dev/video0 -b:v 2M -c:v h264_v4l2m2m -f rtsp rtsp://localhost:$RTSP_PORT/$RTSP_PATH
        runOnDemandRestart: yes
    

    Notes:
    -s 1280x720 is the source video resolution size from the webcam
    -r 25 is the source sample rate
    -i /dev/video0 is the source webcam video
    -b:v 2M is the output video bit rate
    -c:v h264_v4l2m2m is the video encoder to use


  3. Save and close the configuration file.

 Create and start the rtsp-simple-server service

  1.  Open up a Terminal. Type in the following to create the systemd service.

    sudo tee /etc/systemd/system/rtsp-simple-server.service >/dev/null << EOF
    [Unit]
    After=network.target
    [Service]
    ExecStart=/usr/local/bin/rtsp-simple-server /usr/local/etc/rtsp-simple-server.yml
    [Install]
    WantedBy=multi-user.target
    EOF
    

  2. Enable and start the service by executing the following commands. Or reboot the system.

    $ sudo systemctl enable rtsp-simple-server
    $ sudo systemctl start rtsp-simple-server

Viewing the webcam stream using VLC

  1. If the configuration parameters are correct, then start up VLC on a PC on the network.

    VLC starts up.


  2. Select Media | Open Network Stream.

    The Open Media dialog box appears.


  3. In the URL field, type in the address of the Raspberry Pi e.g. rtsp://192.168.18.5:8554/cam. Then click Play.

    If all the parameters are correct, then VLC should show a stream from the webcam.

    VLC displaying a webcam stream.


    Note: if the video stream looks wrong i.e. greenish and weird as shown above, then the ffmpeg version installed on the Raspberry Pi is an older buggy version 4.2.4 and you need to replace it by downloading the latest ffmpeg and building from source.

Replacing ffmpeg and build from source

  1. Open up a Terminal and remove the system installed ffmpeg with the following command:

    $ sudo apt remove ffmpeg

  2. Using a browser and follow the instructions on https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu to install the latest ffmpeg.

  3. Compilation on the Raspberry Pi 4B may take a while, maybe an hour or two so be aware. After compiling successfully, you can try to use VLC and open up the webcam steam again.

    VLC displaying the webcam stream correctly.

Monday, December 27, 2021

ffmpeg command to encode videos using Intel GPU on Ubuntu

Using ffmpeg to encode a video stream, I found the encoding process to use my CPU excessively. I wanted to reduce the CPU usage by transferring the encoding to my built-in Intel GPU. To determine details about the on board Intel GPU driver, you can use the following command on Ubuntu:

$ vainfo

 

In the example screenshots below, I used the libx264 software encoding option in the ffmpeg command to encode a video stream coming from the device /dev/video0 into an output mpeg file output.mp4:

$ sudo ffmpeg -hide_banner -i /dev/video0 -c:v libx264 output.mp4

While this command is running, the top command shows a high CPU usage.

$ top

After reading through the ffmpeg manual pages and a lot of trials, I found the options to use to enable Intel GPU encoding with the ffmpeg command:

$ sudo ffmpeg -hide_banner -vaapi_device /dev/dri/renderD128 -i /dev/video0 -vf 'format=nv12,hwupload' -c:v h264_vaapi output.mp4

 

Running the top command shows reduce CPU usage during the encoding process:



Another example with more options is illustrated below: 

$ ffmpeg \
        -vaapi_device /dev/dri/renderD128 \
        -s 1280x720 \
        -i /dev/video0 \
        -vf 'scale=320x240,fps=fps=25,format=nv12,hwupload' \
        -c:v h264_vaapi \
        -b:v 600k \
        output.mp4

ffmpeg will request a video stream of resolution 1280x720 from the source, then scale the frames to 320x240 resolution with a fps of 25. Then it uploads the video data to the Intel GPU for encoding before writing it out with a video bitrate of 600k to the output.mp4 file.


Monday, September 6, 2021

How to quickly publish an RTSP stream from a webcam on Ubuntu

I wanted to quickly publish an RTSP video stream from a webcam on Linux without having to work with the complexity of sources and layers in OBS Studio. I found this neat software RtspSimpleServer downloadable from https://github.com/aler9/rtsp-simple-server

 For my testing purposes using just the default parameters, I did the following:

Install and run RtspSimpleServer

  1. Using a browser, download and extract the RtspSimpleServer binary from the github repo https://github.com/aler9/rtsp-simple-server into a folder, e.g. /path/to/rtsp/

    The files rtsp-simple-server and the rtsp-simple-server.yml are extracted out into a directory /path/to/rtsp/.

  2. Open up a Terminal. At the prompt, type in the cd command to change to the directory of the rtsp-simple-server.

    $ cd /path/to/rtsp/

  3. At the prompt, run the rtsp-simple-server server:

    $ ./rtsp-simple-server

    Process messages appear to show the server is running.



Publish a video stream

  1. Optional. If the video for linux utils are not installed, then run the apt command to install it.

    $ sudo apt install v4l-utils

  2. Open up a new Terminal. Type in the ffmpeg command to publish the stream from the webcam device (assuming /dev/video0).

    $ ffmpeg \
    -f v4l2 \
    -framerate 90 \
    -re -stream_loop -1 \
    -video_size 640x320 \
    -input_format mjpeg \
    -i /dev/video0 \
    -c copy \
    -f rtsp \
    rtsp://localhost:8554/mystream




    Processing messages appear.


     

Open the stream with a VLC client

  1. Open up a new Terminal.

     
  2. Run the vlc command to open the published stream.

    $ vlc rtsp://localhost:8554/mystream



    The VLC client pops up to show the RTSP stream from the webcam.