How to download and decrypt HTTP Live Streaming (HLS) videos in iOS?

74,656

Solution 1

You can use ffmpeg to download and decode the HTTP-LS stream:

ffmpeg -i http://example.org/playlist.m3u8 -c copy -bsf:a aac_adtstoasc output.mp4

There is an iOS version of ffmpeg available.

Solution 2

This Perl script is a good fetcher: https://github.com/osklil/hls-fetch

Steps:

wget https://raw.githubusercontent.com/osklil/hls-fetch/master/hls-fetch
chmod +x hls_fetch
./hls_fetch --playlist "THE_URL"

Replace THE_URL with the full URL of your M3U8 playlist (or try other options with --help).

Bonus: If you have missing Perl's JSON module (as I had), simply run sudo cpan JSON.

Solution 3

There also exists a Chrome extension that makes a whole video from m3u8 chunks, here's the link HLS Video Saver

Solution 4

From iOS 10, you can use AVFoundation to download HTTP Live Streaming (HLS) assets to an iOS device.

https://developer.apple.com/documentation/avfoundation/media_assets_playback_and_editing/asset_manipulation/downloading_and_playing_offline_http_live_streaming_content?changes=_4

or use this git : HLSion

Solution 5

url: https://mnmedias.api.telequebec.tv/m3u8/29880.m3u8

Step-1: ffmpeg -i 'https://mnmedias.api.telequebec.tv/m3u8/29880.m3u8' -vf scale=w=1280:h=720:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -b:a 128k -c:v h264 -profile:v main -crf 20 -g 48 -keyint_min 48 -sc_threshold 0 -b:v 2500k -maxrate 2675k -bufsize 3750k -hls_time 10 -hls_playlist_type vod -hls_segment_filename my_hls_video/720p_%03d.ts my_hls_video/720p.m3u8

Step-2:

-i 'https://mnmedias.api.telequebec.tv/m3u8/29880.m3u8' :=> Set https://mnmedias.api.telequebec.tv/m3u8/29880.m3u8 as the video source source.

-vf "scale=w=1280:h=720:force_original_aspect_ratio=decrease" :=> Scale video to maximum possible within 1280x720 while preserving aspect ratio

-c:a aac -ar 48000 -b:a 128k :=> Set audio codec to AAC with sampling of 48kHz and bitrate of 128k

-c:v h264 :=> Set video codec to be H264 which is the standard codec of HLS segments

-profile:v main :=> Set H264 profile to main - this means support in modern devices read more

-crf 20 :=> Constant Rate Factor, high level factor for overall quality

-g 48 -keyint_min 48 :=> IMPORTANT create key frame (I-frame) every 48 frames (~2 seconds) - will later affect correct slicing of segments and alignment of renditions

-sc_threshold 0 :=> Don't create key frames on scene change - only according to -g

-b:v 2500k -maxrate 2675k -bufsize 3750k :=> Limit video bitrate, these are rendition specific and depends on your content type - read more

-hls_time 4 : :=> Segment target duration in seconds - the actual length is constrained by key frames

-hls_playlist_type vod :=> Sdds the #EXT-X-PLAYLIST-TYPE:VOD tag and keeps all segments in the playlist

-hls_segment_filename beach/720p_%03d.ts :=> - explicitly define segments files names my_hls_video/720p.m3u8 - path of the playlist file - also tells ffmpeg to output HLS (.m3u8)

Share:
74,656

Related videos on Youtube

NSPratik
Author by

NSPratik

SOreadytohelp I am a direct sub-class of NSObject ! I was not born, I was compiled ! I only understand binary language ! I am so innovative person. Like to play with new technologies. Be aware of the new things available in technology world. I always do the things what I am interested in...

Updated on July 09, 2022

Comments

  • NSPratik
    NSPratik almost 2 years

    I want to download M3U8 file chunks (HLS) and store that video (after decrypting it) for later viewing. I have made a demo to play M3U8 file but I want to download video data for later view.

    • Brad
      Brad almost 8 years
      How are you playing the HLS stream? In-browser with a normal <video> tag?
    • NSPratik
      NSPratik almost 8 years
      I am playing HLS in AVPlayer
    • vipw
      vipw over 7 years
      Which step are you having trouble with? You need to download the video data, decrypt it with the decryption key and save the resulting data to a file. Or are you having trouble with playing back the resulting file?
    • NSPratik
      NSPratik over 7 years
      I have trouble on how to download M3U8 file data. But, my requirement is changed now. MP4 will be downloaded for later viewing. While in case of streaming, encrypted HLS needs to be played on iOS side. How would I play encrypted HLS on iOS ?
    • Dmitry L.
      Dmitry L. over 7 years
      So did you resolve the issue?
    • NSPratik
      NSPratik over 7 years
      No @DmitryDonskoy. Downloading the M3U8 and play it....HLS is not made up for this kind of intention. It is simply for streaming. Otherwise, Apple would have provided an utility to download the M3U8 content. If you want to store the video for later viewing, then you will have to download MP4 (or other video format supported in iOS).
    • Dmitry L.
      Dmitry L. over 7 years
      @NSPratik, thx for answer. I've found this github.com/kencool/KSHLSPlayer Looks like someone can store a video from stream. I'm trying to compile and check it. Maybe it can be useful.
    • NSPratik
      NSPratik over 7 years
      Thanks... Let me check it
  • Lebnik
    Lebnik about 4 years
    $ ./hls-fetch --help Can't locate JSON.pm in @INC (you may need to install the JSON module) (@INC contains: /home/valya/perl5/lib/perl5 /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.22.1 /usr/local/share/perl/5.22.1 /usr/lib/x86_64-linux-gnu/perl5/5.22 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.22 /usr/share/perl/5.22 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base .) at ./hls-fetch line 24. BEGIN failed--compilation aborted at ./hls-fetch line 24.
  • sequielo
    sequielo almost 4 years
    @Lebnik Have you read my bonus tip for installing missing JSON module?
  • TastyWheat
    TastyWheat over 3 years
    I had to add on -protocol_whitelist "file,http,https,tcp,tls" because I was seeing Protocol 'https' not on whitelist. Overall this was the command I needed though. Thanks!