Skip to content

Instantly share code, notes, and snippets.

@Andrey2G
Last active April 7, 2024 13:00
Show Gist options
  • Star 49 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save Andrey2G/78d42b5c87850f8fbadd0b670b0e6924 to your computer and use it in GitHub Desktop.
Save Andrey2G/78d42b5c87850f8fbadd0b670b0e6924 to your computer and use it in GitHub Desktop.
Video Encoding with multiple resolutions
ffmpeg -i "c:/videos/sample.mp4
-map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0
-c:v libx264 -crf 22 -c:a aac -ar 48000
-filter:v:0 scale=w=480:h=360 -maxrate:v:0 600k -b:a:0 64k
-filter:v:1 scale=w=640:h=480 -maxrate:v:1 900k -b:a:1 128k
-filter:v:2 scale=w=1280:h=720 -maxrate:v:2 900k -b:a:2 128k
-var_stream_map "v:0,a:0,name:360p v:1,a:1,name:480p v:2,a:2,name:720p"
-preset slow -hls_list_size 0 -threads 0 -f hls -hls_playlist_type event -hls_time 3
-hls_flags independent_segments -master_pl_name "name-pl.m3u8"
"c:/videos/encoded/name-%v.m3u8"
@Andrey2G
Copy link
Author

links which helped to resolve the issues with ffmpeg command line
https://ffmpeg.org/ffmpeg-filters.html#split_002c-asplit (split!)
https://trac.ffmpeg.org/wiki/Creating%20multiple%20outputs documentation on multiple outputs
https://digitalfortress.tech/tricks/encode-videos-with-ffmpeg/ - good article with references
https://www.martin-riedl.de/2018/08/25/using-ffmpeg-as-a-hls-streaming-server-part-3/ multiple bitrates
https://www.martin-riedl.de/2018/08/26/using-ffmpeg-as-a-hls-streaming-server-part-4-multiple-video-resolutions/ (multiple resolutions)
https://www.martin-riedl.de/2018/08/30/using-ffmpeg-as-a-hls-streaming-server-part-5-folder-structure/ very good explanation of folder structure for HLS
https://www.martin-riedl.de/2018/09/13/using-ffmpeg-as-a-hls-streaming-server-part-6-independent-segments/ (independent segments for HLS)
https://stackoverflow.com/questions/37066553/how-to-use-ffmpeg-to-encode-video-in-h-264
https://streaminglearningcenter.com/blogs/an-ffmpeg-script-to-render-and-package-a-complete-hls-presentation.html
https://superuser.com/questions/714804/converting-video-from-1080p-to-720p-with-smallest-quality-loss-using-ffmpeg
https://superuser.com/questions/753703/ffmpeg-map-optional-audio-stream
https://video.stackexchange.com/questions/25662/ffmpeg-exe-ignores-yadif-filter-when-using-1-filter-on-the-command-line
https://superuser.com/questions/1044988/merging-several-videos-with-audio-channel-and-without-audio
https://superuser.com/questions/841062/concat-two-audio-files-via-ffmpeg-filter-complex
https://superuser.com/questions/1072486/ffmpeg-filter-complex-chaining-video-streams-watermark-then-trim-and-join
https://superuser.com/questions/729991/join-multiple-video-files-two-witout-audio-and-three-with-audio
https://superuser.com/questions/1296836/how-to-encode-one-input-file-to-multiple-hls-streams-with-ffmpeg-including-the-m
https://superuser.com/questions/1453004/ffmpeg-tee-muxer-fails-on-multiple-outputs-hls-and-mp4
https://superuser.com/questions/1138178/cant-make-a-gif-using-ffmpeg-produces-a-gif-but-it-isnt-playable
https://stackoverflow.com/questions/32936421/how-to-correctly-combine-ffmpeg-file-pattern-and-complex-filter
https://stackoverflow.com/questions/33225026/creating-hls-variants-with-ffmpeg
https://stackoverflow.com/questions/45068155/stream-specifier-v0-in-filtergraph-description-1v0-1a0-2v0-2a
https://stackoverflow.com/questions/48577309/ffmpeg-version-2-6-8-stream-specifier-a-in-filtergraph-description-matches
https://stackoverflow.com/questions/19417412/ffmpeg-concatenation-filters-stream-specifier-0-in-filtergraph-matches-no-st
https://stackoverflow.com/questions/55725431/i-have-a-problem-withinvalid-file-index-1-in-filtergraph-description
https://stackoverflow.com/questions/51088697/add-png-overlay-to-multi-output-hls-m3u8-with-ffmpeg?rq=1
http://mplayerhq.hu/pipermail/ffmpeg-user/2019-July/044917.html - mail list! very helpful thread!
https://hlsbook.net/creating-a-master-playlist-with-ffmpeg/
https://superuser.com/questions/1429555/master-playlist-generate-points-to-only-one-resolution-among-multiple
https://stackoverflow.com/questions/33343795/manipulating-one-video-into-multi-outputs-with-ffmpeg-results-in-no-audio-in-the

Problem with CORS
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS - CORS documentation
https://stackoverflow.com/questions/55502725/aws-elastic-encoder-hls-streaming-cloudfront-crossdomain-issue
https://stackoverflow.com/questions/53800705/cors-configuration-for-s3-hosted-hls-video-in-jw-player

HLS player
https://github.com/dailymotion/hls.js
https://players.akamai.com/stream-validator
https://players.akamai.com/players

@90minutes
Copy link

That is so helpful, been looking for an efficient way to convert to hls in ffmpeg for a while. Thanks a lot!

@mburakeker
Copy link

It was helpful, thanks a lot!

@RocioZam
Copy link

RocioZam commented Jan 9, 2022

Thanks a lot!!!

@winlinvip
Copy link

It's useful, and bellow is for live streaming, to covert RTMP to HLS:

ffmpeg -f flv -i "rtmp://server/live/livestream" \
  -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 \
  -c:v libx264 -crf 22 -c:a aac -ar 48000 \
  -filter:v:0 scale=w=480:h=360  -maxrate:v:0 600k -b:a:0 500k \
  -filter:v:1 scale=w=640:h=480  -maxrate:v:1 1500k -b:a:1 1000k \
  -filter:v:2 scale=w=1280:h=720 -maxrate:v:2 3000k -b:a:2 2000k \
  -var_stream_map "v:0,a:0,name:360p v:1,a:1,name:480p v:2,a:2,name:720p" \
  -preset fast -hls_list_size 10 -threads 0 -f hls \
  -hls_time 3 -hls_flags independent_segments \
  -master_pl_name "livestream.m3u8" \
  -y "livestream-%v.m3u8"

For detail please read here

@gulshan-dev123789
Copy link

gulshan-dev123789 commented Mar 21, 2024

is there any way i can stream the output directly to s3 bucket not in my local ??
it will really helpful.

@gulshan-dev123789
Copy link

stderr: More than 1 variant streams are present, %v is expected either in the filename or in the sub-directory name of file pipe:
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument

encounter this error

@Andrey2G
Copy link
Author

is there any way i can stream the output directly to s3 bucket not in my local ?? it will really helpful.

I would suggest to use Amazon Elastic Transcoder in this case.

  • setup/configure preset
  • create a pipeline
  • upload source video to your S3 bucket
  • start the job for selected pipeline and that source video file

https://docs.aws.amazon.com/elastictranscoder/latest/developerguide/getting-started.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment