Saturday, 18 February 2017

Transcoding streams with FFmpeg

RTSP/RTP camera
ffmpeg -i "rtsp://[camera-ip-address]/[camera-URI-syntax]" -vcodec libx264 -vb 150000 -g 60 -vprofile baseline -level 2.1 -acodec aac -ab 64000 -ar 48000 -ac 2 -vbsf h264_mp4toannexb -strict experimental -f mpegts udp://127.0.0.1:10000?pkt_size=1316
MPEG-TS stream

ffmpeg -i "udp://localhost:[port]" -vcodec libx264 -vb 150000 -g 60 -vprofile baseline -level 2.1 -acodec aac -ab 64000 -ar 48000 -ac 2 -vbsf h264_mp4toannexb -strict experimental -f mpegts udp://127.0.0.1:10000?pkt_size=1316
Native RTP stream
ffmpeg -i "unicast.sdp" -vcodec libx264 -vb 150000 -g 60 -vprofile baseline -level 2.1 -acodec aac -ab 64000 -ar 48000 -ac 2 -vbsf h264_mp4toannexb -strict experimental -f mpegts udp://127.0.0.1:10000?pkt_size=1316

Streaming alternative outgoing protocols

FFmpeg can publish streams using other outgoing protocols such as RTMP and RTSP/RTP (QuickTime ANNOUNCE). 

RTMP

To send a stream using RTMP, change the output portion of the FFmpeg URL from:

-f mpegts udp://127.0.0.1:10000?pkt_size=1316

To:

-f flv rtmp://127.0.0.1/live/myStream

The RTMP URL must follow this format: rtmp://[wowza-ip-address]:1935/[application]/[streamName].

RTSP/RTP

To send a stream using RTSP/RTP, change the output portion of the FFmpeg URL from:

-f mpegts udp://127.0.0.1:10000?pkt_size=1316

To:

-f rtsp rtsp://127.0.0.1:1935/live/myStream.sdp

The RTSP URL must follow this format: rtsp://[wowza-ip-address]:1935/[application]/[streamName].

Re-stream RTSP with password authentication

This example uses security credentials, H.264/AAC, baseline profile, level 3.0, 24 fps (frame rate), two-second keyframe frequency, 350kbps stream, 44.1khz, stereo.
ffmpeg -re -i sample.mp4 -c:v libx264 -profile:v baseline -level 3.0 -r 24 -g 48 -keyint_min 48 -sc_threshold 0 -vb 310k -c:a libvo_aacenc -ab 40k -ar 44100 -ac 2 -f rtsp -muxdelay 0.1 rtsp://username:password@[wowza-ip-address]:1935/live/myStream

Re-stream RTP with SDP

This example captures local hardware devices and generates SDP file in the current directory and uses a 15 fps frame rate and 12bit color:
ffmpeg -y -f dshow -s 640x480 -r:v 15 -i video="Integrated Webcam":audio="Microphone (Realtek High Defini" -c:v libx264 -pix_fmt yuv420p -vb 450 -an -map 0:0 -f rtp rtp://127.0.0.1:10000 -flags +global_header -c:a libvo_aacenc -vn -map 0:1 -f rtp rtp://127.0.0.1:10002 > 10000.sdp

Show stream encoding parameters

This example shows VOD or live stream encoding properties for troubleshooting purposes:

ffprobe -show_streams [stream-name]

The following is the output generated by this command when executed on sample.mp4:
[STREAM]
index=0
codec_name=h264
codec_long_name=H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10
profile=Constrained Baseline
codec_type=video
codec_time_base=1/60
codec_tag_string=avc1
codec_tag=0x31637661
width=512
height=288
has_b_frames=0
sample_aspect_ratio=1:1
display_aspect_ratio=16:9
pix_fmt=yuv420p
level=21
timecode=N/A
id=N/A
r_frame_rate=30/1
avg_frame_rate=30/1
time_base=1/15360
start_pts=0
start_time=0.000000
duration_ts=9747968
duration=634.633333
bit_rate=640158
nb_frames=19039
nb_read_frames=N/A
nb_read_packets=N/A
DISPOSITION:default=0
DISPOSITION:dub=0
DISPOSITION:original=0
DISPOSITION:comment=0
DISPOSITION:lyrics=0
DISPOSITION:karaoke=0
DISPOSITION:forced=0
DISPOSITION:hearing_impaired=0
DISPOSITION:visual_impaired=0
DISPOSITION:clean_effects=0
DISPOSITION:attached_pic=0
TAG:language=und
TAG:handler_name=VideoHandler
[/STREAM]
[STREAM]
index=1
codec_name=aac
codec_long_name=AAC (Advanced Audio Coding)
profile=unknown
codec_type=audio
codec_time_base=1/48000
codec_tag_string=mp4a
codec_tag=0x6134706d
sample_fmt=fltp
sample_rate=48000
channels=2
bits_per_sample=0
id=N/A
r_frame_rate=0/0
avg_frame_rate=0/0
time_base=1/48000
start_pts=-1024
start_time=-0.021333
duration_ts=30439936
duration=634.165333
bit_rate=96193
nb_frames=29727
nb_read_frames=N/A
nb_read_packets=N/A
DISPOSITION:default=0
DISPOSITION:dub=0
DISPOSITION:original=0
DISPOSITION:comment=0
DISPOSITION:lyrics=0
DISPOSITION:karaoke=0
DISPOSITION:forced=0
DISPOSITION:hearing_impaired=0
DISPOSITION:visual_impaired=0
DISPOSITION:clean_effects=0
DISPOSITION:attached_pic=0
TAG:language=und
TAG:handler_name=SoundHandler
[/STREAM]

List available devices

This example lists available hardware device names such as webcams and microphones for use as FFmpeg sources:

ffmpeg -list_devices true -f dshow -i dummy


Convert .ts source files to .mp4

This example converts .ts file (or any FFmpeg compatible sources) to the .mp4 container format for use with Wowza video-on-demand playback:

ffmpeg -i input.ts -c:v copy -c:a copy output.mp4

No comments:

Post a Comment