FFMPEG with Start Time

This is a follow up to a previous post about FFMPEG. In addition to sending a RTMP stream, you can also set a start time and duration.

Here’s how to set the start time using -ss option and -to for the duration. Video will start at 1 min 23 secs and play until 2 mins.

ffmpeg -ss 00:01:23 -i video.mp4 -to 00:02:00 -c copy -copyts cut.mp4

Here’s how to start time to start the video at 1 min 23 sec for RTMP streaming. Please note, the start time option needs to be before the input file.

ffmpeg -ss 00:01:23 \
-re -i $1 -acodec libmp3lame -ar 44100 -b:a 128k \
-pix_fmt yuv420p -profile:v baseline -s 1280x720 -bufsize 6000k \
-vb 1200k -maxrate 1500k -deinterlace -vcodec libx264           \
-preset veryfast -g 30 -r 30 -f flv                            \
-flvflags no_duration_filesize                                 \
"rtmp://username:password@yourserver:1935/live/backup"

RTMP of MP4 via FFMPEG

FFMPEG is the jack of all trades utility for both audio and video files. You can record, convert, and even live stream an audio or video file in just about any file format. In this example, I will be sending a RTMP stream of a MP4 file to a Wowza streaming server. I can easily send the same stream to YouTube, Facebook or any streaming server that accepts a RTMP source. In this example, I created simple Bash script called play.sh, with all the options needed to live stream to Wowza Streaming server.

Here are the options in play.sh.

ffmpeg -re -i $1 -acodec libmp3lame -ar 44100 -b:a 128k \
-pix_fmt yuv420p -profile:v baseline -s 1280x720 -bufsize 6000k \
-vb 1200k -maxrate 1500k -deinterlace -vcodec libx264           \
-preset veryfast -g 30 -r 30 -f flv                            \
-flvflags no_duration_filesize                                 \
"rtmp://username:password@yourserver:1935/live/backup"

To launch, run the script.

bash play.sh filename.mp4

Convert MP3 to OGG

Convert MP3 to Vorbis OGG format using ffmpeg.

ffmpeg -i input.mp3 -c:a libvorbis -q:a 4 output.ogg

If you want multiple files, use a loop.

for f in ./*.mp3; do ffmpeg -i "$f" -c:a libvorbis -q:a 4 "${f/%mp3/ogg}"; done

How to Live Stream Using FFmpeg

FFmpeg is a complete cross-platform command-line tool for recording, converting and streaming audio and video files. FFmpeg is widely used for converting files from one format to another. It also used by some to record videos from the screen or camera. It’s also used by others to live stream a recorded file or camera to a RTMP streaming server such as Wowza, YouTube or Facebook Live.

How to record your camera to a video file.

$ ffmpeg -f avfoundation -i "1:0" -t 00:00:15 -s 1280x720 -r 30 -b:v 3500k -b:a 128k out.mpg
$ ffmpeg -f avfoundation -i "2:0" -t 00:00:15 -s 1280x720 -r 30 -b:v 3500k -b:a 128k out.mpg

-i = “video:audio”
-t = duration
-r = fps
-b:v = video bitrate
-b:a = audio bitrate

How to stream a video file to a RTMP server

$ ffmpeg -re -i input.mp4 -vcodec libx264 -preset veryfast -maxrate 1500k 
  -c:a aac -b:a 128k -ac 2 -ar 44100 -f flv rtmp://yourserver:1935/live/mystream

How to live stream your screen monitor to a RTMP server

$ ffmpeg -f avfoundation -i "2:0" -vcodec libx264 -preset ultrafast 
  -pix_fmt yuv420p -s 1280x720 -r 30 -b:v 1500k -bufsize 1500k -maxrate 7000k 
  -c:a aac -b:a 128k -ac 2 -ar 44100 -f flv rtmp://yourserver:1935/live/mystream

How to stream your camera to a RTMP server

$ ffmpeg -f avfoundation -i "1:0" -vcodec libx264 -preset ultrafast 
  -pix_fmt yuv420p -s 1280x720 -r 30 -b:v 1500k -bufsize 1500k -maxrate 7000k 
  -c:a aac -b:a 128k -ac 2 -ar 44100 -f flv rtmp://yourserver:1935/live/mystream

To learn details of FFmpeg options, read the documentation.

If you need assistance with the install, please see my previous post.