FFmpeg

From Leo's Notes
Last edited on 15 June 2022, at 00:28.

Usage

Merging mp4 and m4a

Using ffmpeg you can combine the two together:

# ffmpeg -i video.mp4 -i audio.m4a -acodec copy -vcodec copy output.mp4

Extracting a video section

To extract a video section:

# ffmpeg -i input.mp4 -ss 00:00:15 -t 00:00:10 -async 1 output.mp4

Where:

-ss The start time in HH:MM:SS
-t The length of the video in HH:MM:SS

Slideshow to Video

See: https://trac.ffmpeg.org/wiki/Slideshow

Example usage:

## Remove any empty image files (explained below)
# find ./images -size  0 -print0 | xargs -0 rm --

## Process images to 25fps with 8 CPU cores
# ffmpeg -pattern_type glob \
   -i ./images/\*.jpg \
   -c:v libx264 \
   -vf "fps=25,format=yuv420p" \
   -threads 8 \
   output.mp4

The * needs to be escaped or contained in single quotes because ffmpeg will do the globbing, not the shell.

Empty files will cause ffmpeg to stop processing additional files. Run find ./slideshow -size 0 -print0 to delete zero-sized files to prevent this from happening.

RTMP stream to segmented video files

Record a RTMP stream into multiple segmented files could be useful in a surveillance application.

I'm currently using the following ffmpeg command in a Docker container to record a live video stream from my security cameras and saving them into 15 minute segments. The filename is then named by the date and time of the recording. This command will terminate when the segment is complete. As a result, you'll need to put this command within a loop of some sort if you wish to record indefinitely.

# ffmpeg -rtsp_transport tcp \
	-y \
	-stimeout 15000000 \
	-i "$RTMP_STREAM_URL" \
	-c copy \
	-f segment \
	-segment_time "$VIDEO_SEGMENT_TIME" \
	-reset_timestamps 1 \
	-segment_atclocktime 1 \
	-strftime 1 \
	"$dir/%Y-%m-%d_%H-%M-%S.mp4" \
	-loglevel error

The -y option overwrites the file in the event the filename already exists. The RTMP URL should be using the RTMP protocol, such as rtmp://rtsp-server:1935/garage.

The stimeout value is the socket timeout value. If the video stream for whatever reason hangs and exceeds this value, the recording will be terminated. The value is given in microseconds. In this example, 15'000'000 is equal to 15 seconds. Settings this value too low (eg. 1 second) could result in the stream periodically stopping due to Wi-Fi issues with the video stream resulting in short segments being generated.

The segment time can be given as seconds (eg. 900) or as a HH:MM:SS timestamp (eg. 00:15:00). segment_atclocktime ensures the segment's start/stop times line up with the hour.

Use strftime 1 to enable naming the file with strftime. The end result looks something similar to: 2022-06-14_16-30-01.mp4.

Issues

Copying DASH video (via -vcodec copy) using versions older than 0.10 will result in an empty file because it is unable to read the video contents.