codecs.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. //nolint:goconst
  2. package transcoder
  3. import (
  4. "fmt"
  5. "os/exec"
  6. "strings"
  7. log "github.com/sirupsen/logrus"
  8. )
  9. // Codec represents a supported codec on the system.
  10. type Codec interface {
  11. Name() string
  12. DisplayName() string
  13. GlobalFlags() string
  14. PixelFormat() string
  15. Scaler() string
  16. ExtraArguments() string
  17. ExtraFilters() string
  18. VariantFlags(v *HLSVariant) string
  19. GetPresetForLevel(l int) string
  20. }
  21. var supportedCodecs = map[string]string{
  22. (&Libx264Codec{}).Name(): "libx264",
  23. (&OmxCodec{}).Name(): "omx",
  24. (&VaapiCodec{}).Name(): "vaapi",
  25. (&NvencCodec{}).Name(): "NVIDIA nvenc",
  26. (&VideoToolboxCodec{}).Name(): "videotoolbox",
  27. }
  28. // Libx264Codec represents an instance of the Libx264 Codec.
  29. type Libx264Codec struct{}
  30. // Name returns the codec name.
  31. func (c *Libx264Codec) Name() string {
  32. return "libx264"
  33. }
  34. // DisplayName returns the human readable name of the codec.
  35. func (c *Libx264Codec) DisplayName() string {
  36. return "x264"
  37. }
  38. // GlobalFlags are the global flags used with this codec in the transcoder.
  39. func (c *Libx264Codec) GlobalFlags() string {
  40. return ""
  41. }
  42. // PixelFormat is the pixel format required for this codec.
  43. func (c *Libx264Codec) PixelFormat() string {
  44. return "yuv420p" //nolint:goconst
  45. }
  46. // Scaler is the scaler used for resizing the video in the transcoder.
  47. func (c *Libx264Codec) Scaler() string {
  48. return ""
  49. }
  50. // ExtraArguments are the extra arguments used with this codec in the transcoder.
  51. func (c *Libx264Codec) ExtraArguments() string {
  52. return strings.Join([]string{
  53. "-tune", "zerolatency", // Option used for good for fast encoding and low-latency streaming (always includes iframes in each segment)
  54. }, " ")
  55. }
  56. // ExtraFilters are the extra filters required for this codec in the transcoder.
  57. func (c *Libx264Codec) ExtraFilters() string {
  58. return ""
  59. }
  60. // VariantFlags returns a string representing a single variant processed by this codec.
  61. func (c *Libx264Codec) VariantFlags(v *HLSVariant) string {
  62. return strings.Join([]string{
  63. fmt.Sprintf("-x264-params:v:%d \"scenecut=0:open_gop=0\"", v.index), // How often the encoder checks the bitrate in order to meet average/max values
  64. fmt.Sprintf("-bufsize:v:%d %dk", v.index, v.getBufferSize()),
  65. fmt.Sprintf("-profile:v:%d %s", v.index, "high"), // Encoding profile
  66. }, " ")
  67. }
  68. // GetPresetForLevel returns the string preset for this codec given an integer level.
  69. func (c *Libx264Codec) GetPresetForLevel(l int) string {
  70. presetMapping := map[int]string{
  71. 0: "ultrafast",
  72. 1: "superfast",
  73. 2: "veryfast",
  74. 3: "faster",
  75. 4: "fast",
  76. }
  77. preset, ok := presetMapping[l]
  78. if !ok {
  79. defaultPreset := presetMapping[1]
  80. log.Errorf("Invalid level for x264 preset %d, defaulting to %s", l, defaultPreset)
  81. return defaultPreset
  82. }
  83. return preset
  84. }
  85. // OmxCodec represents an instance of the Omx codec.
  86. type OmxCodec struct{}
  87. // Name returns the codec name.
  88. func (c *OmxCodec) Name() string {
  89. return "h264_omx"
  90. }
  91. // DisplayName returns the human readable name of the codec.
  92. func (c *OmxCodec) DisplayName() string {
  93. return "OpenMAX (omx)"
  94. }
  95. // GlobalFlags are the global flags used with this codec in the transcoder.
  96. func (c *OmxCodec) GlobalFlags() string {
  97. return ""
  98. }
  99. // PixelFormat is the pixel format required for this codec.
  100. func (c *OmxCodec) PixelFormat() string {
  101. return "yuv420p"
  102. }
  103. // Scaler is the scaler used for resizing the video in the transcoder.
  104. func (c *OmxCodec) Scaler() string {
  105. return ""
  106. }
  107. // ExtraArguments are the extra arguments used with this codec in the transcoder.
  108. func (c *OmxCodec) ExtraArguments() string {
  109. return strings.Join([]string{
  110. "-tune", "zerolatency", // Option used for good for fast encoding and low-latency streaming (always includes iframes in each segment)
  111. }, " ")
  112. }
  113. // ExtraFilters are the extra filters required for this codec in the transcoder.
  114. func (c *OmxCodec) ExtraFilters() string {
  115. return ""
  116. }
  117. // VariantFlags returns a string representing a single variant processed by this codec.
  118. func (c *OmxCodec) VariantFlags(v *HLSVariant) string {
  119. return ""
  120. }
  121. // GetPresetForLevel returns the string preset for this codec given an integer level.
  122. func (c *OmxCodec) GetPresetForLevel(l int) string {
  123. presetMapping := map[int]string{
  124. 0: "ultrafast",
  125. 1: "superfast",
  126. 2: "veryfast",
  127. 3: "faster",
  128. 4: "fast",
  129. }
  130. preset, ok := presetMapping[l]
  131. if !ok {
  132. defaultPreset := presetMapping[1]
  133. log.Errorf("Invalid level for omx preset %d, defaulting to %s", l, defaultPreset)
  134. return defaultPreset
  135. }
  136. return preset
  137. }
  138. // VaapiCodec represents an instance of the Vaapi codec.
  139. type VaapiCodec struct{}
  140. // Name returns the codec name.
  141. func (c *VaapiCodec) Name() string {
  142. return "h264_vaapi"
  143. }
  144. // DisplayName returns the human readable name of the codec.
  145. func (c *VaapiCodec) DisplayName() string {
  146. return "VA-API"
  147. }
  148. // GlobalFlags are the global flags used with this codec in the transcoder.
  149. func (c *VaapiCodec) GlobalFlags() string {
  150. flags := []string{
  151. "-hwaccel", "vaapi",
  152. "-hwaccel_output_format", "vaapi",
  153. "-vaapi_device", "/dev/dri/renderD128",
  154. }
  155. return strings.Join(flags, " ")
  156. }
  157. // PixelFormat is the pixel format required for this codec.
  158. func (c *VaapiCodec) PixelFormat() string {
  159. return "vaapi_vld"
  160. }
  161. // Scaler is the scaler used for resizing the video in the transcoder.
  162. func (c *VaapiCodec) Scaler() string {
  163. return "scale_vaapi"
  164. }
  165. // ExtraFilters are the extra filters required for this codec in the transcoder.
  166. func (c *VaapiCodec) ExtraFilters() string {
  167. return ""
  168. }
  169. // ExtraArguments are the extra arguments used with this codec in the transcoder.
  170. func (c *VaapiCodec) ExtraArguments() string {
  171. return ""
  172. }
  173. // VariantFlags returns a string representing a single variant processed by this codec.
  174. func (c *VaapiCodec) VariantFlags(v *HLSVariant) string {
  175. return ""
  176. }
  177. // GetPresetForLevel returns the string preset for this codec given an integer level.
  178. func (c *VaapiCodec) GetPresetForLevel(l int) string {
  179. presetMapping := map[int]string{
  180. 0: "ultrafast",
  181. 1: "superfast",
  182. 2: "veryfast",
  183. 3: "faster",
  184. 4: "fast",
  185. }
  186. preset, ok := presetMapping[l]
  187. if !ok {
  188. defaultPreset := presetMapping[1]
  189. log.Errorf("Invalid level for vaapi preset %d, defaulting to %s", l, defaultPreset)
  190. return defaultPreset
  191. }
  192. return preset
  193. }
  194. // NvencCodec represents an instance of the Nvenc Codec.
  195. type NvencCodec struct{}
  196. // Name returns the codec name.
  197. func (c *NvencCodec) Name() string {
  198. return "h264_nvenc"
  199. }
  200. // DisplayName returns the human readable name of the codec.
  201. func (c *NvencCodec) DisplayName() string {
  202. return "nvidia nvenc"
  203. }
  204. // GlobalFlags are the global flags used with this codec in the transcoder.
  205. func (c *NvencCodec) GlobalFlags() string {
  206. flags := []string{
  207. "-hwaccel", "cuda",
  208. }
  209. return strings.Join(flags, " ")
  210. }
  211. // PixelFormat is the pixel format required for this codec.
  212. func (c *NvencCodec) PixelFormat() string {
  213. return "yuv420p"
  214. }
  215. // Scaler is the scaler used for resizing the video in the transcoder.
  216. func (c *NvencCodec) Scaler() string {
  217. return ""
  218. }
  219. // ExtraArguments are the extra arguments used with this codec in the transcoder.
  220. func (c *NvencCodec) ExtraArguments() string {
  221. return ""
  222. }
  223. // ExtraFilters are the extra filters required for this codec in the transcoder.
  224. func (c *NvencCodec) ExtraFilters() string {
  225. return ""
  226. }
  227. // VariantFlags returns a string representing a single variant processed by this codec.
  228. func (c *NvencCodec) VariantFlags(v *HLSVariant) string {
  229. tuning := "ll" // low latency
  230. return fmt.Sprintf("-tune:v:%d %s", v.index, tuning)
  231. }
  232. // GetPresetForLevel returns the string preset for this codec given an integer level.
  233. func (c *NvencCodec) GetPresetForLevel(l int) string {
  234. presetMapping := map[int]string{
  235. 0: "p1",
  236. 1: "p2",
  237. 2: "p3",
  238. 3: "p4",
  239. 4: "p5",
  240. }
  241. preset, ok := presetMapping[l]
  242. if !ok {
  243. defaultPreset := presetMapping[2]
  244. log.Errorf("Invalid level for nvenc preset %d, defaulting to %s", l, defaultPreset)
  245. return defaultPreset
  246. }
  247. return preset
  248. }
  249. // QuicksyncCodec represents an instance of the Intel Quicksync Codec.
  250. type QuicksyncCodec struct{}
  251. // Name returns the codec name.
  252. func (c *QuicksyncCodec) Name() string {
  253. return "h264_qsv"
  254. }
  255. // DisplayName returns the human readable name of the codec.
  256. func (c *QuicksyncCodec) DisplayName() string {
  257. return "Intel QuickSync"
  258. }
  259. // GlobalFlags are the global flags used with this codec in the transcoder.
  260. func (c *QuicksyncCodec) GlobalFlags() string {
  261. return ""
  262. }
  263. // PixelFormat is the pixel format required for this codec.
  264. func (c *QuicksyncCodec) PixelFormat() string {
  265. return "nv12"
  266. }
  267. // Scaler is the scaler used for resizing the video in the transcoder.
  268. func (c *QuicksyncCodec) Scaler() string {
  269. return ""
  270. }
  271. // ExtraArguments are the extra arguments used with this codec in the transcoder.
  272. func (c *QuicksyncCodec) ExtraArguments() string {
  273. return ""
  274. }
  275. // ExtraFilters are the extra filters required for this codec in the transcoder.
  276. func (c *QuicksyncCodec) ExtraFilters() string {
  277. return ""
  278. }
  279. // VariantFlags returns a string representing a single variant processed by this codec.
  280. func (c *QuicksyncCodec) VariantFlags(v *HLSVariant) string {
  281. return ""
  282. }
  283. // GetPresetForLevel returns the string preset for this codec given an integer level.
  284. func (c *QuicksyncCodec) GetPresetForLevel(l int) string {
  285. presetMapping := map[int]string{
  286. 0: "ultrafast",
  287. 1: "superfast",
  288. 2: "veryfast",
  289. 3: "faster",
  290. 4: "fast",
  291. }
  292. preset, ok := presetMapping[l]
  293. if !ok {
  294. defaultPreset := presetMapping[1]
  295. log.Errorf("Invalid level for quicksync preset %d, defaulting to %s", l, defaultPreset)
  296. return defaultPreset
  297. }
  298. return preset
  299. }
  300. // Video4Linux represents an instance of the V4L Codec.
  301. type Video4Linux struct{}
  302. // Name returns the codec name.
  303. func (c *Video4Linux) Name() string {
  304. return "h264_v4l2m2m"
  305. }
  306. // DisplayName returns the human readable name of the codec.
  307. func (c *Video4Linux) DisplayName() string {
  308. return "Video4Linux"
  309. }
  310. // GlobalFlags are the global flags used with this codec in the transcoder.
  311. func (c *Video4Linux) GlobalFlags() string {
  312. return ""
  313. }
  314. // PixelFormat is the pixel format required for this codec.
  315. func (c *Video4Linux) PixelFormat() string {
  316. return "nv21"
  317. }
  318. // Scaler is the scaler used for resizing the video in the transcoder.
  319. func (c *Video4Linux) Scaler() string {
  320. return ""
  321. }
  322. // ExtraArguments are the extra arguments used with this codec in the transcoder.
  323. func (c *Video4Linux) ExtraArguments() string {
  324. return ""
  325. }
  326. // ExtraFilters are the extra filters required for this codec in the transcoder.
  327. func (c *Video4Linux) ExtraFilters() string {
  328. return ""
  329. }
  330. // VariantFlags returns a string representing a single variant processed by this codec.
  331. func (c *Video4Linux) VariantFlags(v *HLSVariant) string {
  332. return ""
  333. }
  334. // GetPresetForLevel returns the string preset for this codec given an integer level.
  335. func (c *Video4Linux) GetPresetForLevel(l int) string {
  336. presetMapping := map[int]string{
  337. 0: "ultrafast",
  338. 1: "superfast",
  339. 2: "veryfast",
  340. 3: "faster",
  341. 4: "fast",
  342. }
  343. preset, ok := presetMapping[l]
  344. if !ok {
  345. defaultPreset := presetMapping[1]
  346. log.Errorf("Invalid level for v4l preset %d, defaulting to %s", l, defaultPreset)
  347. return defaultPreset
  348. }
  349. return preset
  350. }
  351. // VideoToolboxCodec represents an instance of the VideoToolbox codec.
  352. type VideoToolboxCodec struct{}
  353. // Name returns the codec name.
  354. func (c *VideoToolboxCodec) Name() string {
  355. return "h264_videotoolbox"
  356. }
  357. // DisplayName returns the human readable name of the codec.
  358. func (c *VideoToolboxCodec) DisplayName() string {
  359. return "VideoToolbox"
  360. }
  361. // GlobalFlags are the global flags used with this codec in the transcoder.
  362. func (c *VideoToolboxCodec) GlobalFlags() string {
  363. var flags []string
  364. return strings.Join(flags, " ")
  365. }
  366. // PixelFormat is the pixel format required for this codec.
  367. func (c *VideoToolboxCodec) PixelFormat() string {
  368. return "nv12"
  369. }
  370. // Scaler is the scaler used for resizing the video in the transcoder.
  371. func (c *VideoToolboxCodec) Scaler() string {
  372. return ""
  373. }
  374. // ExtraFilters are the extra filters required for this codec in the transcoder.
  375. func (c *VideoToolboxCodec) ExtraFilters() string {
  376. return ""
  377. }
  378. // ExtraArguments are the extra arguments used with this codec in the transcoder.
  379. func (c *VideoToolboxCodec) ExtraArguments() string {
  380. return ""
  381. }
  382. // VariantFlags returns a string representing a single variant processed by this codec.
  383. func (c *VideoToolboxCodec) VariantFlags(v *HLSVariant) string {
  384. arguments := []string{
  385. "-realtime true",
  386. "-realtime true",
  387. "-realtime true",
  388. }
  389. if v.cpuUsageLevel >= len(arguments) {
  390. return ""
  391. }
  392. return arguments[v.cpuUsageLevel]
  393. }
  394. // GetPresetForLevel returns the string preset for this codec given an integer level.
  395. func (c *VideoToolboxCodec) GetPresetForLevel(l int) string {
  396. presetMapping := map[int]string{
  397. 0: "ultrafast",
  398. 1: "superfast",
  399. 2: "veryfast",
  400. 3: "faster",
  401. 4: "fast",
  402. }
  403. preset, ok := presetMapping[l]
  404. if !ok {
  405. defaultPreset := presetMapping[1]
  406. log.Errorf("Invalid level for videotoolbox preset %d, defaulting to %s", l, defaultPreset)
  407. return defaultPreset
  408. }
  409. return preset
  410. }
  411. // GetCodecs will return the supported codecs available on the system.
  412. func GetCodecs(ffmpegPath string) []string {
  413. codecs := make([]string, 0)
  414. cmd := exec.Command(ffmpegPath, "-encoders")
  415. out, err := cmd.CombinedOutput()
  416. if err != nil {
  417. log.Errorln(err)
  418. return codecs
  419. }
  420. response := string(out)
  421. lines := strings.Split(response, "\n")
  422. for _, line := range lines {
  423. if strings.Contains(line, "H.264") {
  424. fields := strings.Fields(line)
  425. codec := fields[1]
  426. if _, supported := supportedCodecs[codec]; supported {
  427. codecs = append(codecs, codec)
  428. }
  429. }
  430. }
  431. return codecs
  432. }
  433. func getCodec(name string) Codec {
  434. switch name {
  435. case (&NvencCodec{}).Name():
  436. return &NvencCodec{}
  437. case (&VaapiCodec{}).Name():
  438. return &VaapiCodec{}
  439. case (&QuicksyncCodec{}).Name():
  440. return &QuicksyncCodec{}
  441. case (&OmxCodec{}).Name():
  442. return &OmxCodec{}
  443. case (&Video4Linux{}).Name():
  444. return &Video4Linux{}
  445. case (&VideoToolboxCodec{}).Name():
  446. return &VideoToolboxCodec{}
  447. default:
  448. return &Libx264Codec{}
  449. }
  450. }