Skip to content

Commit

Permalink
Add HLSDirectory config parameter
Browse files Browse the repository at this point in the history
to set directory for playlist/segment files
  • Loading branch information
Thor77 committed Nov 26, 2019
1 parent 9a22824 commit 10ce222
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ ffmpeg -i <input file> -f flv "rtmp://localhost/stream01"
| Key | `string` | | Expected value of `key` query parameter for connecting clients (disabled if unset) |
| MsPerSegment | `int64` | `15000` | Milliseconds of video/audio written to one segment file |
| LogLevel | `log.Level` | `"info"` | Level for the internal logger |
| HLSDirectory | `string` | `"."` | Directory to write playlist/segment files into
15 changes: 15 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import (
"fmt"
"os"

"github.com/BurntSushi/toml"
log "github.com/sirupsen/logrus"
)
Expand All @@ -11,9 +14,12 @@ type Config struct {
Key string
MsPerSegment int64
LogLevel log.Level
HLSDirectory string
}

func loadConfig(path string) (Config, error) {
log.Debugf("Reading config from %s\n", path)

var tmpConfig Config
if _, err := toml.DecodeFile(path, &tmpConfig); err != nil {
return tmpConfig, err
Expand All @@ -29,6 +35,15 @@ func loadConfig(path string) (Config, error) {
if tmpConfig.MsPerSegment == 0 {
tmpConfig.MsPerSegment = 15000
}
if tmpConfig.HLSDirectory == "" {
tmpConfig.HLSDirectory = "."
} else if _, err := os.Stat(tmpConfig.HLSDirectory); err != nil {
// try to create dir
log.Debugln("HLS directory doesn't exist, trying to create it")
if err := os.Mkdir(tmpConfig.HLSDirectory, 0755); err != nil {
return Config{}, fmt.Errorf("error while checking hls directory: %v", err)
}
}

return tmpConfig, nil
}
6 changes: 3 additions & 3 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func removeOutdatedSegments(streamLogger *log.Entry, streamName string, playlist
}
}
// find (probably) segment files in current directory
segmentFiles, err := filepath.Glob(fmt.Sprintf("%s*.ts", streamName))
segmentFiles, err := filepath.Glob(filepath.Join(config.HLSDirectory, fmt.Sprintf("%s*.ts", streamName)))
if err != nil {
return err
}
Expand Down Expand Up @@ -64,7 +64,7 @@ func publishHandler(conn *rtmp.Conn) {
streamLogger.Infoln("Client connected")

// create hls playlist
playlistFileName := fmt.Sprintf("%s.m3u8", streamName)
playlistFileName := filepath.Join(config.HLSDirectory, fmt.Sprintf("%s.m3u8", streamName))
playlist, err := m3u8.NewMediaPlaylist(5, 10)
if err != nil {
streamLogger.Errorln(err)
Expand All @@ -82,7 +82,7 @@ func publishHandler(conn *rtmp.Conn) {
var lastPacketTime time.Duration = 0
for clientConnected {
// create new segment file
segmentName := fmt.Sprintf("%s%04d.ts", streamName, i)
segmentName := filepath.Join(config.HLSDirectory, fmt.Sprintf("%s%04d.ts", streamName, i))
outFile, err := os.Create(segmentName)
if err != nil {
streamLogger.Errorln(err)
Expand Down

0 comments on commit 10ce222

Please sign in to comment.