FFmpeg Video-Hosting Guide: HLS, GPU Encoding, and MP4

XMLans Posted on 2025-12-26 16 Views


FFmpeg processes audio and video for tasks such as transcoding, stream packaging, and compression. Download an appropriate build from the official download page, then add its executable directory to PATH if needed. Windows builds normally use ffmpeg.exe.

Command structure

ffmpeg [global options] [input options] -i input [output options] output

Order matters: input options generally belong before the input they affect, and output options before the destination. A command reference is not a list of switches to combine indiscriminately.

Option reference

# General
-h                         # Help
-h full                    # Full help
-version                   # Version
-loglevel info             # quiet/error/warning/info/verbose/debug
-y                         # Overwrite output
-n                         # Refuse to overwrite
-stats                     # Encoding progress
-hide_banner               # Hide build information
# Input and timing (placement before/after -i matters)
-i input.mp4               # Input file
-ss 00:01:00               # Seek to a time
-t 10                      # Duration in seconds
-to 00:02:00               # Stop time
-re                        # Read at native rate for real-time streaming
-stream_loop -1            # Loop input indefinitely
-itsoffset 1.5             # Input timestamp offset
# Video encoders (availability depends on build and hardware)
-c:v libx264               # CPU H.264
-c:v libx265               # CPU H.265
-c:v h264_nvenc            # NVIDIA H.264
-c:v hevc_nvenc            # NVIDIA H.265
-c:v av1_nvenc             # NVIDIA AV1 on supported GPUs
-c:v copy                  # Copy without video re-encoding
-profile:v high            # Encoder profile
-preset slow               # Encoder-specific preset
-level:v 4.2               # Codec level
# Rate and quality (not all encoders support every option)
-b:v 1M                    # Target video bitrate
-maxrate 1M                # Rate-control ceiling
-bufsize 2M                # Rate-control buffer
-crf 23                    # Quality control for supported encoders
-qp 20                     # Quantizer setting for supported encoders
-rc vbr                    # Encoder-specific rate-control mode
# Frame rate and keyframes
-r 30                      # Output frame rate
-g 300                     # Maximum GOP interval
-keyint_min 300            # Encoder-specific minimum keyframe interval
-sc_threshold 0            # Disable scene-change keyframes where supported
# Dimensions and filters
-s 1920x1080               # Output size
-aspect 16:9               # Display aspect ratio
-pix_fmt yuv420p           # Pixel format
-vf scale=1280:720         # Resize
-vf crop=1280:720:0:0      # Crop
-vf transpose=1            # Rotate
-vf fps=30                 # Frame-rate filter
# Audio
-c:a aac                   # AAC
-c:a libmp3lame            # MP3 with the LAME encoder
-c:a libopus               # Opus with libopus
-c:a copy                  # Copy audio without re-encoding
-b:a 128k                  # Audio bitrate
-ar 48000                  # Sample rate
-ac 2                      # Two channels
-af volume=1.5             # Volume multiplier
-af atempo=1.25            # Playback-speed multiplier
# Subtitles and stream mapping
-c:s mov_text              # MP4 text subtitles
-vf subtitles=sub.srt      # Burn subtitles into the image
-map 0:s:0                 # Select first subtitle stream
-disposition:s:0 default   # Mark default subtitle
-map 0                     # Select all streams
-map 0:v:0                 # Select first video stream
-map 0:a:0                 # Select first audio stream
-map -0:a                  # Exclude audio
# Containers
-f mp4                     # MP4
-f matroska                # MKV
-f flv                     # FLV, commonly used with RTMP
-f mpegts                  # MPEG transport stream
-f hls                     # HTTP Live Streaming
# HLS
-hls_time 10               # Target segment length
-hls_list_size 0           # Keep every playlist entry
-hls_segment_filename seg_%03d.ts # Segment names
-hls_flags independent_segments   # Use only when segment boundaries are independently decodable
-start_number 0            # First segment number
# Hardware (device support and filter formats matter)
-hwaccel cuda              # CUDA decoding where supported
-hwaccel_output_format cuda # Hardware frames
-init_hw_device cuda       # Initialize a device
-filter_hw_device cuda     # Select filter device
# Network outputs and diagnostics
-f flv rtmp://url          # RTMP output example
-f mpegts srt://url        # SRT output example
-report                    # Write a diagnostic report
-benchmark                 # Timing statistics
-threads 8                 # Thread option where supported

Example 1: AMD H.264 to HLS

This adapts the original AMF command with an encoder-specific quality setting. Confirm that your build and GPU support h264_amf:

ffmpeg -i m.mp4 -vf "scale=1920:1080,format=nv12" -c:v h264_amf -profile:v main -quality balanced -start_number 0 -hls_time 10 -hls_list_size 0 -hls_segment_filename "m%d.ts" -c:a copy m.m3u8

Example 2: NVIDIA H.264 to HLS

ffmpeg -hwaccel cuda -i m.mp4 -c:v h264_nvenc -profile:v main -preset p4 -s 1920x1080 -start_number 0 -hls_time 10 -hls_list_size 0 -hls_segment_filename "m%d.ts" -c:a copy m.m3u8

These examples transcode video and copy the existing audio. They do not guarantee the original bitrate, lossless quality, or compatibility of every copied audio codec.

Example 3: lower-bitrate NVIDIA HLS

ffmpeg -hwaccel cuda -i m.mp4 -c:v h264_nvenc -preset p4 -profile:v high -rc vbr -b:v 800k -maxrate 1M -bufsize 2M -r 30 -g 300 -keyint_min 300 -forced-idr 1 -no-scenecut 1 -s 1920x1080 -c:a aac -b:a 128k -f hls -hls_time 10 -hls_list_size 0 -start_number 0 -hls_segment_filename "m_%03d.ts" m.m3u8

The target video rate is 800 kbps with a 1 Mbps ceiling, at 1080p and 30 fps. That can look poor on detailed or fast-moving material. The GOP length is 300 frames, matching a ten-second target at 30 fps; actual segmentation follows keyframes.

Example 4: CPU H.264 to MP4

ffmpeg -i m.mp4 -c:v libx264 -preset medium -profile:v high -level 4.2 -b:v 1M -maxrate 1M -bufsize 2M -pix_fmt yuv420p -movflags +faststart -c:a aac -b:a 128k out.mp4

The faststart flag moves MP4 metadata toward the beginning for progressive delivery. Hardware encoding can be faster, but quality, compression efficiency, and supported options differ between GPUs and CPU encoders.

HLS playlist and segmented video output

What HLS changes

An m3u8 playlist references separate media segments. This can support streaming workflows and reduce unnecessary downloading when playback stops, but HLS does not itself shrink the same encoded video or guarantee a bandwidth saving.

Test on the actual player and device. Bitrate reduction trades detail for size; there is no universal middle setting with zero quality loss. Use FFmpeg’s documentation and ffmpeg -h encoder=NAME to check the options available in your build.

Adapted from the original Chinese article, published on December 26, 2025.

Hi! I frequently update with various articles about technology, practical tips, and cutting-edge news. I hope it will be helpful to you!
Last updated on 2026-09-30