Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tan Quang Ngo committed Nov 6, 2018
0 parents commit ade2e80
Show file tree
Hide file tree
Showing 81 changed files with 9,952 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: go
go:
- "1.10"
- "1.11"

before_install:
- go get -t -v ./...

script:
- go test -v -race -coverprofile=coverage.txt -covermode=atomic ./test/...

after_success:
- bash <(curl -s https://codecov.io/bash)
44 changes: 44 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
name = "github.com/AlekSi/pointer"
version = "1.0.0"

[[constraint]]
name = "github.com/stretchr/testify"
version = "1.2.2"

[prune]
go-tests = true
unused-packages = true
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Tan Quang Ngo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
126 changes: 126 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
[![Build Status](https://travis-ci.org/quangngotan95/go-m3u8.svg?branch=master)](https://travis-ci.org/quangngotan95/go-m3u8)
[![codecov](https://codecov.io/gh/quangngotan95/go-m3u8/branch/master/graph/badge.svg)](https://codecov.io/gh/quangngotan95/go-m3u8)
# go-m3u8
Golang package for m3u8 (ported m3u8 gem https://github.com/sethdeckard/m3u8)

`go-m3u8` provides easy generation and parsing of m3u8 playlists defined in the HTTP Live Streaming (HLS) Internet Draft published by Apple.
* The library completely implements version 20 of the HLS Internet Draft.
* Provides parsing of an m3u8 playlist into an object model from any File, io.Reader or string.
* Provides ability to write playlist to a string via String()
* Distinction between a master and media playlist is handled automatically (single Playlist class).
* Optionally, the library can automatically generate the audio/video codecs string used in the CODEC attribute based on specified H.264, AAC, or MP3 options (such as Profile/Level).

## Installation
`go get github.com/quangngotan95/go-m3u8`

## Usage (creating playlists)
Create a master playlist and child playlists for adaptive bitrate streaming:
```go
import (
"github.com/quangngotan95/go-m3u8/m3u8"
"github.com/AlekSi/pointer"
)

playlist := m3u8.NewPlaylist()
```
Create a new playlist item:
```go
item := &m3u8.PlaylistItem{
Width: pointer.ToInt(1920),
Height: pointer.ToInt(1080),
Profile: pointer.ToString("high"),
Level: pointer.ToString("4.1"),
AudioCodec: pointer.ToString("aac-lc"),
Bandwidth: 540,
URI: "test.url",
}
playlist.AppendItem(item)
```
Add alternate audio, camera angles, closed captions and subtitles by creating MediaItem instances and adding them to the Playlist:
```go
item := &m3u8.MediaItem{
Type: "AUDIO",
GroupID: "audio-lo",
Name: "Francais",
Language: pointer.ToString("fre"),
AssocLanguage: pointer.ToString("spoken"),
AutoSelect: pointer.ToBool(true),
Default: pointer.ToBool(false),
Forced: pointer.ToBool(true),
URI: pointer.ToString("frelo/prog_index.m3u8"),
}
playlist.AppendItem(item)
```
Create a standard playlist and add MPEG-TS segments via SegmentItem. You can also specify options for this type of playlist, however these options are ignored if playlist becomes a master playlist (anything but segments added):
```go
playlist := &m3u8.Playlist{
Target: 12,
Sequence: 1,
Version: pointer.ToInt(1),
Cache: pointer.ToBool(false),
Items: []m3u8.Item{
&m3u8.SegmentItem{
Duration: 11,
Segment: "test.ts",
},
},
}
```
You can also access the playlist as a string:
```go
var str string
str = playlist.String()
...
fmt.Print(playlist)
```
Alternatively you can set codecs rather than having it generated automatically:
```go
item := &m3u8.PlaylistItem{
Width: pointer.ToInt(1920),
Height: pointer.ToInt(1080),
Codecs: pointer.ToString("avc1.66.30,mp4a.40.2"),
Bandwidth: 540,
URI: "test.url",
}
```

## Usage (parsing playlists)
Parse from file
```go
playlist, err := m3u8.ReadFile("path/to/file")
```
Read from string
```go
playlist, err := m3u8.ReadString(string)
```
Read from generic `io.Reader`
```go
playlist, err := m3u8.Read(reader)
```

Access items in playlist:
```go
gore> playlist.Items[0]
(*m3u8.SessionKeyItem)#EXT-X-SESSION-KEY:METHOD=AES-128,URI="https://priv.example.com/key.php?r=52"
gore> playlist.Items[1]
(*m3u8.PlaybackStart)#EXT-X-START:TIME-OFFSET=20.2
```

## Misc
Codecs:
* Values for audio_codec (codec name): aac-lc, he-aac, mp3
* Values for profile (H.264 Profile): baseline, main, high.
* Values for level (H.264 Level): 3.0, 3.1, 4.0, 4.1.

Not all Levels and Profiles can be combined and validation is not currently implemented, consult H.264 documentation for further details.

## Contributing
1. Fork it https://github.com/quangngotan95/go-m3u8/fork
2. Create your feature branch `git checkout -b my-new-feature`
3. Run tests `go test ./test/...`, make sure they all pass and new features are covered
4. Commit your changes `git commit -am "Add new features"`
5. Push to the branch `git push origin my-new-feature`
6. Create a new Pull Request

## License
MIT License - See [LICENSE.txt](https://github.com/quangngotan95/go-m3u8/blob/master/LICENSE) for details
46 changes: 46 additions & 0 deletions m3u8/byteRange.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package m3u8

import (
"fmt"
"strconv"
"strings"
)

// ByteRange represents sub range of a resource
type ByteRange struct {
Length *int
Start *int
}

func NewByteRange(text string) (*ByteRange, error) {
if text == "" {
return nil, nil
}

values := strings.Split(text, "@")

lengthValue, err := strconv.Atoi(values[0])
if err != nil {
return nil, err
}

br := ByteRange{Length: &lengthValue}

if len(values) >= 2 {
startValue, err := strconv.Atoi(values[1])
if err != nil {
return &br, err
}
br.Start = &startValue
}

return &br, nil
}

func (br *ByteRange) String() string {
if br.Start == nil {
return fmt.Sprintf("%d", *br.Length)
}

return fmt.Sprintf("%d@%d", *br.Length, *br.Start)
}
74 changes: 74 additions & 0 deletions m3u8/codecs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package m3u8

import "strings"

var (
AudioCodecMap = map[string]string{
"aac-lc": "mp4a.40.2",
"he-aac": "mp4a.40.5",
"mp3": "mp4a.40.34",
}

BaselineCodecMap = map[string]string{
"3.0": "avc1.66.30",
"3.1": "avc1.42001f",
}

MainCodecMap = map[string]string{
"3.0": "avc1.77.30",
"3.1": "avc1.4d001f",
"4.0": "avc1.4d0028",
"4.1": "avc1.4d0029",
}

HighCodecMap = map[string]string{
"3.0": "avc1.64001e",
"3.1": "avc1.64001f",
"3.2": "avc1.640020",
"4.0": "avc1.640028",
"4.1": "avc1.640029",
"4.2": "avc1.64002a",
"5.0": "avc1.640032",
"5.1": "avc1.640033",
"5.2": "avc1.640034",
}
)

func audioCodec(codec *string) *string {
if codec == nil {
return nil
}

key := strings.ToLower(*codec)
value, ok := AudioCodecMap[key]

if !ok {
return nil
}

return &value
}

func videoCodec(profile *string, level *string) *string {
if profile == nil || level == nil {
return nil
}

var value string
var ok bool

switch *profile {
case "baseline":
value, ok = BaselineCodecMap[*level]
case "main":
value, ok = MainCodecMap[*level]
case "high":
value, ok = HighCodecMap[*level]
}

if !ok {
return nil
}

return &value
}
Loading

0 comments on commit ade2e80

Please sign in to comment.