code

HTTP LIve 스트리밍

codestyles 2021. 1. 10. 17:17
반응형

HTTP LIve 스트리밍


좋아, 나는이 http 라이브 스트리밍에 대해 머리를 감싸려고 노력하고 있습니다. 나는 그것을 이해하지 못하고 네 모든 사과 문서를 읽고 wwdc 비디오를 보았지만 여전히 매우 혼란 스럽기 때문에 프로그래머가되고 싶어하는 것을 도와주세요!

작성한 코드가 서버에 있습니까? xcode에 없습니까? 내가 맞다면 어떻게 설정합니까? 내 서버에 특별한 것을 설정해야합니까? PHP 같은 거요? Apple .. Segmenter 등에서 제공하는 도구를 어떻게 사용합니까?

도와주세요, 감사합니다


HTTP 라이브 스트리밍

HTTP Live Streaming은 Apple에서 제안한 스트리밍 표준입니다. 최신 초안 표준을 참조하십시오 .

관련된 파일은 다음과 같습니다.

  • .m4a 오디오 용 (오디오 스트림 만 원하는 경우).
  • .ts비디오. 일반적으로 h.264 / AAC 페이로드를 사용하는 MPEG-2 전송입니다. 10 초 분량의 비디오가 포함되어 있으며 원본 비디오 파일을 분할하거나 라이브 비디오를 변환하여 생성됩니다.
  • .m3u8재생 목록. 이것은 WinAmp 형식의 UTF-8 버전입니다.

라이브 스트리밍이라고하는 경우에도 일반적으로 비디오가 변환되고 ts 및 m3u8 파일이 작성되고 클라이언트가 m3u8 파일을 새로 고치는 동안 1 분 정도의 지연이 있습니다.

이 모든 파일은 서버의 정적 파일입니다. 그러나 라이브 이벤트에서는 더 많은 .ts 파일이 추가되고 m3u8 파일이 업데이트됩니다.

이 질문에 iOS에 태그를 지정 했으므로 관련 App Store 규칙을 언급하는 것과 관련이 있습니다.

  • 10 분 미만 또는 5 분마다 5MB 미만의 비디오에만 점진적 다운로드를 사용할 수 있습니다. 그렇지 않으면 HTTP 라이브 스트리밍을 사용해야합니다.
  • HTTP 라이브 스트리밍을 사용하는 경우 64Kbps 이하의 대역폭에서 하나 이상의 스트림을 제공해야합니다 (낮은 대역폭 스트림은 오디오 전용이거나 정지 이미지가있는 오디오 일 수 있음).

스트리밍 도구 받기

HTTP 라이브 스트리밍 도구를 다운로드하려면 다음을 수행하십시오.

설치된 명령 줄 도구 :

 /usr/bin/mediastreamsegmenter
 /usr/bin/mediafilesegmenter
 /usr/bin/variantplaylistcreator
 /usr/bin/mediastreamvalidator
 /usr/bin/id3taggenerator

매뉴얼 페이지의 설명 :

  • Media Stream Segmenter : HTTP 라이브 스트리밍을 위해 MPEG-2 전송 스트림에서 세그먼트를 생성합니다.
  • 미디어 파일 분할기 : 미디어 파일에서 HTTP 라이브 스트리밍을위한 세그먼트를 만듭니다.
  • Variant Playlist Creator : mediafilesegmenter에서 만든 HTTP 라이브 스트리밍 세그먼트에서 스트림 전환을위한 재생 목록을 만듭니다.
  • 미디어 스트림 검사기 : HTTP 라이브 스트리밍 스트림 및 서버의 유효성을 검사합니다.
  • ID3 태그 생성기 : ID3 태그를 생성합니다.

비디오 만들기

Macports를 설치하고 터미널로 이동하여 sudo port install ffmpeg. 그런 다음 다음 FFMpeg 스크립트를 사용하여 비디오를 전송 스트림 (.ts)으로 변환합니다.

# bitrate, width, and height, you may want to change this
BR=512k
WIDTH=432
HEIGHT=240
input=${1}

# strip off the file extension
output=$(echo ${input} | sed 's/\..*//' )

# works for most videos
ffmpeg -y -i ${input} -f mpegts -acodec libmp3lame -ar 48000 -ab 64k -s ${WIDTH}x${HEIGHT} -vcodec libx264 -b ${BR} -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -subq 7 -trellis 0 -refs 0 -coder 0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 200k -maxrate ${BR} -bufsize ${BR} -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 30 -qmax 51 -qdiff 4 -level 30 -aspect ${WIDTH}:${HEIGHT} -g 30 -async 2 ${output}-iphone.ts

이것은 하나의 .ts 파일을 생성합니다. 이제 파일을 세그먼트로 분할하고 모든 파일을 포함하는 재생 목록을 만들어야합니다. 이를 위해 Apple을 사용할 수 있습니다 mediafilesegmenter.

mediafilesegmenter -t 10 myvideo-iphone.ts

그러면 비디오 10 초마다 하나의 .ts 파일과 이들 모두를 가리키는 .m3u8 파일이 생성됩니다.

웹 서버 설정

.m3u8iOS에서 재생하려면 모바일 사파리로 파일을 가리 킵니다. 물론 먼저 웹 서버에 배치해야합니다. Safari (또는 다른 플레이어)가 ts 파일을 인식하려면 MIME 유형을 추가해야합니다. Apache에서 :

 AddType application/x-mpegURL m3u8
 AddType video/MP2T ts

lighttpd에서 :

 mimetype.assign = ( ".m3u8" => "application/x-mpegURL", ".ts" => "video/MP2T" )

웹 페이지에서 링크하려면 :

<html><head>
    <meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
</head><body>
    <video width="320" height="240" src="stream.m3u8" />
</body></html>

To detect the device orientation see Detect and Set the iPhone & iPad's Viewport Orientation Using JavaScript, CSS and Meta Tags.

More stuff you can do is create different bitrate versions of the video, embed metadata to read it while playing as notifications, and of course have fun programming with the MoviePlayerController and AVPlayer.


This might help in swift:

    import UIKit
    import MediaPlayer

 class ViewController: UIViewController {

     var streamPlayer : MPMoviePlayerController =  MPMoviePlayerController(contentURL: NSURL(string:"http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8"))
     override func viewDidLoad() {
         super.viewDidLoad()
         streamPlayer.view.frame = self.view.bounds
         self.view.addSubview(streamPlayer.view)

         streamPlayer.fullscreen = true
         // Play the movie!
         streamPlayer.play()
}
}

MPMoviePlayerController is deprecated from iOS 9 onwards. We can use AVPlayerViewController() or AVPlayer for the purpose. Have a look:

import AVKit
import AVFoundation
import UIKit

AVPlayerViewController :

override func viewDidAppear(animated: Bool){
let videoURL = NSURL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(URL: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
    playerViewController.player!.play()
}
}

AVPlayer :

 override func viewDidAppear(animated: Bool){
    let videoURL = NSURL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
    let player = AVPlayer(URL: videoURL!)
    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = self.view.bounds
    self.view.layer.addSublayer(playerLayer)
    player.play()
    }

Another explanation from Cloudinary http://cloudinary.com/documentation/video_manipulation_and_delivery#http_live_streaming_hls

HTTP Live Streaming (also known as HLS) is an HTTP-based media streaming communications protocol that provides mechanisms that are scalable and adaptable to different networks. HLS works by breaking down a video file into a sequence of small HTTP-based file downloads, with each download loading one short chunk of a video file.

As the video stream is played, the client player can select from a number of different alternate video streams containing the same material encoded at a variety of data rates, allowing the streaming session to adapt to the available data rate with high quality playback on networks with high bandwidth and low quality playback on networks where the bandwidth is reduced.

At the start of the streaming session, the client software downloads a master M3U8 playlist file containing the metadata for the various sub-streams which are available. The client software then decides what to download from the media files available, based on predefined factors such as device type, resolution, data rate, size, etc.

ReferenceURL : https://stackoverflow.com/questions/6592485/http-live-streaming

반응형