transcoder.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. package transcoder
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "os/exec"
  7. "strconv"
  8. "strings"
  9. log "github.com/sirupsen/logrus"
  10. "github.com/teris-io/shortid"
  11. "github.com/owncast/owncast/config"
  12. "github.com/owncast/owncast/core/data"
  13. "github.com/owncast/owncast/logging"
  14. "github.com/owncast/owncast/models"
  15. "github.com/owncast/owncast/utils"
  16. )
  17. var _commandExec *exec.Cmd
  18. // Transcoder is a single instance of a video transcoder.
  19. type Transcoder struct {
  20. codec Codec
  21. stdin *io.PipeReader
  22. TranscoderCompleted func(error)
  23. playlistOutputPath string
  24. ffmpegPath string
  25. segmentIdentifier string
  26. internalListenerPort string
  27. input string
  28. segmentOutputPath string
  29. variants []HLSVariant
  30. currentStreamOutputSettings []models.StreamOutputVariant
  31. currentLatencyLevel models.LatencyLevel
  32. appendToStream bool
  33. isEvent bool
  34. }
  35. // HLSVariant is a combination of settings that results in a single HLS stream.
  36. type HLSVariant struct {
  37. audioBitrate string // The audio bitrate
  38. videoSize VideoSize // Resizes the video via scaling
  39. index int
  40. framerate int // The output framerate
  41. videoBitrate int // The output bitrate
  42. cpuUsageLevel int // The amount of hardware to use for encoding a stream
  43. isVideoPassthrough bool // Override all settings and just copy the video stream
  44. isAudioPassthrough bool // Override all settings and just copy the audio stream
  45. }
  46. // VideoSize is the scaled size of the video output.
  47. type VideoSize struct {
  48. Width int
  49. Height int
  50. }
  51. // For limiting the output bitrate
  52. // https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate
  53. // https://developer.apple.com/documentation/http_live_streaming/about_apple_s_http_live_streaming_tools
  54. // Adjust the max & buffer size until the output bitrate doesn't exceed the ~+10% that Apple's media validator
  55. // complains about.
  56. // getAllocatedVideoBitrate returns the video bitrate we allocate after making some room for audio.
  57. // 192 is pretty average.
  58. func (v *HLSVariant) getAllocatedVideoBitrate() int {
  59. return int(float64(v.videoBitrate) - 192)
  60. }
  61. // getMaxVideoBitrate returns the maximum video bitrate we allow the encoder to support.
  62. func (v *HLSVariant) getMaxVideoBitrate() int {
  63. return int(float64(v.getAllocatedVideoBitrate()) * 1.08)
  64. }
  65. // getBufferSize returns how often it checks the bitrate of encoded segments to see if it's too high/low.
  66. func (v *HLSVariant) getBufferSize() int {
  67. return int(float64(v.getMaxVideoBitrate()))
  68. }
  69. // getString returns a WxH formatted getString for scaling video output.
  70. func (v *VideoSize) getString() string {
  71. widthString := strconv.Itoa(v.Width)
  72. heightString := strconv.Itoa(v.Height)
  73. if widthString != "0" && heightString != "0" {
  74. return widthString + ":" + heightString
  75. } else if widthString != "0" {
  76. return widthString + ":-2"
  77. } else if heightString != "0" {
  78. return "-2:" + heightString
  79. }
  80. return ""
  81. }
  82. // Stop will stop the transcoder and kill all processing.
  83. func (t *Transcoder) Stop() {
  84. log.Traceln("Transcoder STOP requested.")
  85. err := _commandExec.Process.Kill()
  86. if err != nil {
  87. log.Errorln(err)
  88. }
  89. }
  90. // Start will execute the transcoding process with the settings previously set.
  91. func (t *Transcoder) Start(shouldLog bool) {
  92. _lastTranscoderLogMessage = ""
  93. command := t.getString()
  94. if shouldLog {
  95. log.Infof("Processing video using codec %s with %d output qualities configured.", t.codec.DisplayName(), len(t.variants))
  96. }
  97. createVariantDirectories()
  98. if config.EnableDebugFeatures {
  99. log.Println(command)
  100. }
  101. _commandExec = exec.Command("sh", "-c", command)
  102. if t.stdin != nil {
  103. _commandExec.Stdin = t.stdin
  104. }
  105. stdout, err := _commandExec.StderrPipe()
  106. if err != nil {
  107. log.Fatalln(err)
  108. }
  109. if err := _commandExec.Start(); err != nil {
  110. log.Errorln("Transcoder error. See", logging.GetTranscoderLogFilePath(), "for full output to debug.")
  111. log.Panicln(err, command)
  112. }
  113. go func() {
  114. scanner := bufio.NewScanner(stdout)
  115. for scanner.Scan() {
  116. line := scanner.Text()
  117. handleTranscoderMessage(line)
  118. }
  119. }()
  120. err = _commandExec.Wait()
  121. if t.TranscoderCompleted != nil {
  122. t.TranscoderCompleted(err)
  123. }
  124. if err != nil {
  125. log.Errorln("transcoding error. look at", logging.GetTranscoderLogFilePath(), "to help debug. your copy of ffmpeg may not support your selected codec of", t.codec.Name(), "https://owncast.online/docs/codecs/")
  126. }
  127. }
  128. // SetLatencyLevel will set the latency level for the instance of the transcoder.
  129. func (t *Transcoder) SetLatencyLevel(level models.LatencyLevel) {
  130. t.currentLatencyLevel = level
  131. }
  132. // SetIsEvent will allow you to set a stream as an "event".
  133. func (t *Transcoder) SetIsEvent(isEvent bool) {
  134. t.isEvent = isEvent
  135. }
  136. func (t *Transcoder) getString() string {
  137. port := t.internalListenerPort
  138. localListenerAddress := "http://127.0.0.1:" + port
  139. hlsOptionFlags := []string{
  140. "program_date_time",
  141. "independent_segments",
  142. }
  143. if t.appendToStream {
  144. hlsOptionFlags = append(hlsOptionFlags, "append_list")
  145. }
  146. if t.segmentIdentifier == "" {
  147. t.segmentIdentifier = shortid.MustGenerate()
  148. }
  149. hlsEventString := ""
  150. if t.isEvent {
  151. hlsEventString = "-hls_playlist_type event"
  152. } else {
  153. // Don't let the transcoder close the playlist. We do it manually.
  154. hlsOptionFlags = append(hlsOptionFlags, "omit_endlist")
  155. }
  156. hlsOptionsString := ""
  157. if len(hlsOptionFlags) > 0 {
  158. hlsOptionsString = "-hls_flags " + strings.Join(hlsOptionFlags, "+")
  159. }
  160. ffmpegFlags := []string{
  161. fmt.Sprintf(`FFREPORT=file="%s":level=32`, logging.GetTranscoderLogFilePath()),
  162. t.ffmpegPath,
  163. "-hide_banner",
  164. "-loglevel warning",
  165. t.codec.GlobalFlags(),
  166. "-fflags +genpts", // Generate presentation time stamp if missing
  167. "-flags +cgop", // Force closed GOPs
  168. "-i ", t.input,
  169. t.getVariantsString(),
  170. // HLS Output
  171. "-f", "hls",
  172. "-hls_time", strconv.Itoa(t.currentLatencyLevel.SecondsPerSegment), // Length of each segment
  173. "-hls_list_size", strconv.Itoa(t.currentLatencyLevel.SegmentCount), // Max # in variant playlist
  174. hlsOptionsString,
  175. hlsEventString,
  176. "-segment_format_options", "mpegts_flags=mpegts_copyts=1",
  177. // Video settings
  178. t.codec.ExtraArguments(),
  179. "-pix_fmt", t.codec.PixelFormat(),
  180. "-sc_threshold", "0", // Disable scene change detection for creating segments
  181. // Filenames
  182. "-master_pl_name", "stream.m3u8",
  183. "-hls_segment_filename", localListenerAddress + "/%v/stream-" + t.segmentIdentifier + "-%d.ts", // Send HLS segments back to us over HTTP
  184. "-max_muxing_queue_size", "400", // Workaround for Too many packets error: https://trac.ffmpeg.org/ticket/6375?cversion=0
  185. "-method PUT", // HLS results sent back to us will be over PUTs
  186. localListenerAddress + "/%v/stream.m3u8", // Send HLS playlists back to us over HTTP
  187. }
  188. return strings.Join(ffmpegFlags, " ")
  189. }
  190. func getVariantFromConfigQuality(quality models.StreamOutputVariant, index int) HLSVariant {
  191. variant := HLSVariant{}
  192. variant.index = index
  193. variant.isAudioPassthrough = quality.IsAudioPassthrough
  194. variant.isVideoPassthrough = quality.IsVideoPassthrough
  195. // If no audio bitrate is specified then we pass through original audio
  196. if quality.AudioBitrate == 0 {
  197. variant.isAudioPassthrough = true
  198. }
  199. if quality.VideoBitrate == 0 {
  200. quality.VideoBitrate = 1200
  201. }
  202. // If the video is being passed through then
  203. // don't continue to set options on the variant.
  204. if variant.isVideoPassthrough {
  205. return variant
  206. }
  207. // Set a default, reasonable preset if one is not provided.
  208. // "superfast" and "ultrafast" are generally not recommended since they look bad.
  209. // https://trac.ffmpeg.org/wiki/Encode/H.264
  210. variant.cpuUsageLevel = quality.CPUUsageLevel
  211. variant.SetVideoBitrate(quality.VideoBitrate)
  212. variant.SetAudioBitrate(strconv.Itoa(quality.AudioBitrate) + "k")
  213. variant.SetVideoScalingWidth(quality.ScaledWidth)
  214. variant.SetVideoScalingHeight(quality.ScaledHeight)
  215. variant.SetVideoFramerate(quality.GetFramerate())
  216. return variant
  217. }
  218. // NewTranscoder will return a new Transcoder, populated by the config.
  219. func NewTranscoder() *Transcoder {
  220. ffmpegPath := utils.ValidatedFfmpegPath(data.GetFfMpegPath())
  221. transcoder := new(Transcoder)
  222. transcoder.ffmpegPath = ffmpegPath
  223. transcoder.internalListenerPort = config.InternalHLSListenerPort
  224. transcoder.currentStreamOutputSettings = data.GetStreamOutputVariants()
  225. transcoder.currentLatencyLevel = data.GetStreamLatencyLevel()
  226. transcoder.codec = getCodec(data.GetVideoCodec())
  227. transcoder.segmentOutputPath = config.HLSStoragePath
  228. transcoder.playlistOutputPath = config.HLSStoragePath
  229. transcoder.input = "pipe:0" // stdin
  230. for index, quality := range transcoder.currentStreamOutputSettings {
  231. variant := getVariantFromConfigQuality(quality, index)
  232. transcoder.AddVariant(variant)
  233. }
  234. return transcoder
  235. }
  236. // Uses `map` https://www.ffmpeg.org/ffmpeg-all.html#Stream-specifiers-1 https://www.ffmpeg.org/ffmpeg-all.html#Advanced-options
  237. func (v *HLSVariant) getVariantString(t *Transcoder) string {
  238. variantEncoderCommands := []string{
  239. v.getVideoQualityString(t),
  240. v.getAudioQualityString(),
  241. }
  242. if (v.videoSize.Width != 0 || v.videoSize.Height != 0) && !v.isVideoPassthrough {
  243. // Order here matters, you must scale before changing hardware formats
  244. filters := []string{
  245. v.getScalingString(t.codec.Scaler()),
  246. }
  247. if t.codec.ExtraFilters() != "" {
  248. filters = append(filters, t.codec.ExtraFilters())
  249. }
  250. scalingAlgorithm := "bilinear"
  251. filterString := fmt.Sprintf("-sws_flags %s -filter:v:%d \"%s\"", scalingAlgorithm, v.index, strings.Join(filters, ","))
  252. variantEncoderCommands = append(variantEncoderCommands, filterString)
  253. } else if t.codec.ExtraFilters() != "" && !v.isVideoPassthrough {
  254. filterString := fmt.Sprintf("-filter:v:%d \"%s\"", v.index, t.codec.ExtraFilters())
  255. variantEncoderCommands = append(variantEncoderCommands, filterString)
  256. }
  257. preset := t.codec.GetPresetForLevel(v.cpuUsageLevel)
  258. if preset != "" {
  259. variantEncoderCommands = append(variantEncoderCommands, fmt.Sprintf("-preset %s", preset))
  260. }
  261. return strings.Join(variantEncoderCommands, " ")
  262. }
  263. // Get the command flags for the variants.
  264. func (t *Transcoder) getVariantsString() string {
  265. variantsCommandFlags := ""
  266. variantsStreamMaps := " -var_stream_map \""
  267. for _, variant := range t.variants {
  268. variantsCommandFlags = variantsCommandFlags + " " + variant.getVariantString(t)
  269. singleVariantMap := fmt.Sprintf("v:%d,a:%d ", variant.index, variant.index)
  270. variantsStreamMaps += singleVariantMap
  271. }
  272. variantsCommandFlags = variantsCommandFlags + " " + variantsStreamMaps + "\""
  273. return variantsCommandFlags
  274. }
  275. // Video Scaling
  276. // https://trac.ffmpeg.org/wiki/Scaling
  277. // If we'd like to keep the aspect ratio, we need to specify only one component, either width or height.
  278. // Some codecs require the size of width and height to be a multiple of n. You can achieve this by setting the width or height to -n.
  279. // SetVideoScalingWidth will set the scaled video width of this variant.
  280. func (v *HLSVariant) SetVideoScalingWidth(width int) {
  281. v.videoSize.Width = width
  282. }
  283. // SetVideoScalingHeight will set the scaled video height of this variant.
  284. func (v *HLSVariant) SetVideoScalingHeight(height int) {
  285. v.videoSize.Height = height
  286. }
  287. func (v *HLSVariant) getScalingString(scaler string) string {
  288. if scaler == "" {
  289. scaler = "scale"
  290. }
  291. return fmt.Sprintf("%s=%s", scaler, v.videoSize.getString())
  292. }
  293. // Video Quality
  294. // SetVideoBitrate will set the output bitrate of this variant's video.
  295. func (v *HLSVariant) SetVideoBitrate(bitrate int) {
  296. v.videoBitrate = bitrate
  297. }
  298. func (v *HLSVariant) getVideoQualityString(t *Transcoder) string {
  299. if v.isVideoPassthrough {
  300. return fmt.Sprintf("-map v:0 -c:v:%d copy", v.index)
  301. }
  302. gop := v.framerate * t.currentLatencyLevel.SecondsPerSegment // force an i-frame every segment
  303. cmd := []string{
  304. "-map v:0",
  305. fmt.Sprintf("-c:v:%d %s", v.index, t.codec.Name()), // Video codec used for this variant
  306. fmt.Sprintf("-b:v:%d %dk", v.index, v.getAllocatedVideoBitrate()), // The average bitrate for this variant allowing space for audio
  307. fmt.Sprintf("-maxrate:v:%d %dk", v.index, v.getMaxVideoBitrate()), // The max bitrate allowed for this variant
  308. fmt.Sprintf("-g:v:%d %d", v.index, gop), // Suggested interval where i-frames are encoded into the segments
  309. fmt.Sprintf("-keyint_min:v:%d %d", v.index, gop), // minimum i-keyframe interval
  310. fmt.Sprintf("-r:v:%d %d", v.index, v.framerate),
  311. t.codec.VariantFlags(v),
  312. }
  313. return strings.Join(cmd, " ")
  314. }
  315. // SetVideoFramerate will set the output framerate of this variant's video.
  316. func (v *HLSVariant) SetVideoFramerate(framerate int) {
  317. v.framerate = framerate
  318. }
  319. // SetCPUUsageLevel will set the hardware usage of this variant.
  320. func (v *HLSVariant) SetCPUUsageLevel(level int) {
  321. v.cpuUsageLevel = level
  322. }
  323. // Audio Quality
  324. // SetAudioBitrate will set the output framerate of this variant's audio.
  325. func (v *HLSVariant) SetAudioBitrate(bitrate string) {
  326. v.audioBitrate = bitrate
  327. }
  328. func (v *HLSVariant) getAudioQualityString() string {
  329. if v.isAudioPassthrough {
  330. return fmt.Sprintf("-map a:0? -c:a:%d copy", v.index)
  331. }
  332. // libfdk_aac is not a part of every ffmpeg install, so use "aac" instead
  333. encoderCodec := "aac"
  334. return fmt.Sprintf("-map a:0? -c:a:%d %s -b:a:%d %s", v.index, encoderCodec, v.index, v.audioBitrate)
  335. }
  336. // AddVariant adds a new HLS variant to include in the output.
  337. func (t *Transcoder) AddVariant(variant HLSVariant) {
  338. variant.index = len(t.variants)
  339. t.variants = append(t.variants, variant)
  340. }
  341. // SetInput sets the input stream on the filesystem.
  342. func (t *Transcoder) SetInput(input string) {
  343. t.input = input
  344. }
  345. // SetStdin sets the Stdin of the ffmpeg command.
  346. func (t *Transcoder) SetStdin(pipe *io.PipeReader) {
  347. t.stdin = pipe
  348. }
  349. // SetOutputPath sets the root directory that should include playlists and video segments.
  350. func (t *Transcoder) SetOutputPath(output string) {
  351. t.segmentOutputPath = output
  352. }
  353. // SetIdentifier enables appending a unique identifier to segment file name.
  354. func (t *Transcoder) SetIdentifier(output string) {
  355. t.segmentIdentifier = output
  356. }
  357. // SetInternalHTTPPort will set the port to be used for internal communication.
  358. func (t *Transcoder) SetInternalHTTPPort(port string) {
  359. t.internalListenerPort = port
  360. }
  361. // SetCodec will set the codec to be used for the transocder.
  362. func (t *Transcoder) SetCodec(codecName string) {
  363. t.codec = getCodec(codecName)
  364. }