codecs.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. ExtraArguments() string
  16. ExtraFilters() string
  17. VariantFlags(v *HLSVariant) string
  18. GetPresetForLevel(l int) string
  19. }
  20. var supportedCodecs = map[string]string{
  21. (&Libx264Codec{}).Name(): "libx264",
  22. (&OmxCodec{}).Name(): "omx",
  23. (&VaapiCodec{}).Name(): "vaapi",
  24. (&NvencCodec{}).Name(): "NVIDIA nvenc",
  25. }
  26. // Libx264Codec represents an instance of the Libx264 Codec.
  27. type Libx264Codec struct {
  28. }
  29. // Name returns the codec name.
  30. func (c *Libx264Codec) Name() string {
  31. return "libx264"
  32. }
  33. // DisplayName returns the human readable name of the codec.
  34. func (c *Libx264Codec) DisplayName() string {
  35. return "x264"
  36. }
  37. // GlobalFlags are the global flags used with this codec in the transcoder.
  38. func (c *Libx264Codec) GlobalFlags() string {
  39. return ""
  40. }
  41. // PixelFormat is the pixel format required for this codec.
  42. func (c *Libx264Codec) PixelFormat() string {
  43. return "yuv420p" //nolint:goconst
  44. }
  45. // ExtraArguments are the extra arguments used with this codec in the transcoder.
  46. func (c *Libx264Codec) ExtraArguments() string {
  47. return strings.Join([]string{
  48. "-tune", "zerolatency", // Option used for good for fast encoding and low-latency streaming (always includes iframes in each segment)
  49. }, " ")
  50. }
  51. // ExtraFilters are the extra filters required for this codec in the transcoder.
  52. func (c *Libx264Codec) ExtraFilters() string {
  53. return ""
  54. }
  55. // VariantFlags returns a string representing a single variant processed by this codec.
  56. func (c *Libx264Codec) VariantFlags(v *HLSVariant) string {
  57. return strings.Join([]string{
  58. 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
  59. fmt.Sprintf("-bufsize:v:%d %dk", v.index, v.getBufferSize()),
  60. fmt.Sprintf("-profile:v:%d %s", v.index, "high"), // Encoding profile
  61. }, " ")
  62. }
  63. // GetPresetForLevel returns the string preset for this codec given an integer level.
  64. func (c *Libx264Codec) GetPresetForLevel(l int) string {
  65. presetMapping := []string{
  66. "ultrafast",
  67. "superfast",
  68. "veryfast",
  69. "faster",
  70. "fast",
  71. }
  72. if l >= len(presetMapping) {
  73. return "superfast" //nolint:goconst
  74. }
  75. return presetMapping[l]
  76. }
  77. // OmxCodec represents an instance of the Omx codec.
  78. type OmxCodec struct {
  79. }
  80. // Name returns the codec name.
  81. func (c *OmxCodec) Name() string {
  82. return "h264_omx"
  83. }
  84. // DisplayName returns the human readable name of the codec.
  85. func (c *OmxCodec) DisplayName() string {
  86. return "OpenMAX (omx)"
  87. }
  88. // GlobalFlags are the global flags used with this codec in the transcoder.
  89. func (c *OmxCodec) GlobalFlags() string {
  90. return ""
  91. }
  92. // PixelFormat is the pixel format required for this codec.
  93. func (c *OmxCodec) PixelFormat() string {
  94. return "yuv420p"
  95. }
  96. // ExtraArguments are the extra arguments used with this codec in the transcoder.
  97. func (c *OmxCodec) ExtraArguments() string {
  98. return strings.Join([]string{
  99. "-tune", "zerolatency", // Option used for good for fast encoding and low-latency streaming (always includes iframes in each segment)
  100. }, " ")
  101. }
  102. // ExtraFilters are the extra filters required for this codec in the transcoder.
  103. func (c *OmxCodec) ExtraFilters() string {
  104. return ""
  105. }
  106. // VariantFlags returns a string representing a single variant processed by this codec.
  107. func (c *OmxCodec) VariantFlags(v *HLSVariant) string {
  108. return ""
  109. }
  110. // GetPresetForLevel returns the string preset for this codec given an integer level.
  111. func (c *OmxCodec) GetPresetForLevel(l int) string {
  112. presetMapping := []string{
  113. "ultrafast",
  114. "superfast",
  115. "veryfast",
  116. "faster",
  117. "fast",
  118. }
  119. if l >= len(presetMapping) {
  120. return "superfast"
  121. }
  122. return presetMapping[l]
  123. }
  124. // VaapiCodec represents an instance of the Vaapi codec.
  125. type VaapiCodec struct {
  126. }
  127. // Name returns the codec name.
  128. func (c *VaapiCodec) Name() string {
  129. return "h264_vaapi"
  130. }
  131. // DisplayName returns the human readable name of the codec.
  132. func (c *VaapiCodec) DisplayName() string {
  133. return "VA-API"
  134. }
  135. // GlobalFlags are the global flags used with this codec in the transcoder.
  136. func (c *VaapiCodec) GlobalFlags() string {
  137. flags := []string{
  138. "-vaapi_device", "/dev/dri/renderD128",
  139. }
  140. return strings.Join(flags, " ")
  141. }
  142. // PixelFormat is the pixel format required for this codec.
  143. func (c *VaapiCodec) PixelFormat() string {
  144. return "vaapi_vld"
  145. }
  146. // ExtraFilters are the extra filters required for this codec in the transcoder.
  147. func (c *VaapiCodec) ExtraFilters() string {
  148. return "format=nv12,hwupload"
  149. }
  150. // ExtraArguments are the extra arguments used with this codec in the transcoder.
  151. func (c *VaapiCodec) ExtraArguments() string {
  152. return ""
  153. }
  154. // VariantFlags returns a string representing a single variant processed by this codec.
  155. func (c *VaapiCodec) VariantFlags(v *HLSVariant) string {
  156. return ""
  157. }
  158. // GetPresetForLevel returns the string preset for this codec given an integer level.
  159. func (c *VaapiCodec) GetPresetForLevel(l int) string {
  160. presetMapping := []string{
  161. "ultrafast",
  162. "superfast",
  163. "veryfast",
  164. "faster",
  165. "fast",
  166. }
  167. if l >= len(presetMapping) {
  168. return "superfast"
  169. }
  170. return presetMapping[l]
  171. }
  172. // NvencCodec represents an instance of the Nvenc Codec.
  173. type NvencCodec struct {
  174. }
  175. // Name returns the codec name.
  176. func (c *NvencCodec) Name() string {
  177. return "h264_nvenc"
  178. }
  179. // DisplayName returns the human readable name of the codec.
  180. func (c *NvencCodec) DisplayName() string {
  181. return "nvidia nvenc"
  182. }
  183. // GlobalFlags are the global flags used with this codec in the transcoder.
  184. func (c *NvencCodec) GlobalFlags() string {
  185. flags := []string{
  186. "-hwaccel cuda",
  187. }
  188. return strings.Join(flags, " ")
  189. }
  190. // PixelFormat is the pixel format required for this codec.
  191. func (c *NvencCodec) PixelFormat() string {
  192. return "yuv420p"
  193. }
  194. // ExtraArguments are the extra arguments used with this codec in the transcoder.
  195. func (c *NvencCodec) ExtraArguments() string {
  196. return ""
  197. }
  198. // ExtraFilters are the extra filters required for this codec in the transcoder.
  199. func (c *NvencCodec) ExtraFilters() string {
  200. return ""
  201. }
  202. // VariantFlags returns a string representing a single variant processed by this codec.
  203. func (c *NvencCodec) VariantFlags(v *HLSVariant) string {
  204. tuning := "ll" // low latency
  205. return fmt.Sprintf("-tune:v:%d %s", v.index, tuning)
  206. }
  207. // GetPresetForLevel returns the string preset for this codec given an integer level.
  208. func (c *NvencCodec) GetPresetForLevel(l int) string {
  209. presetMapping := []string{
  210. "p1",
  211. "p2",
  212. "p3",
  213. "p4",
  214. "p5",
  215. }
  216. if l >= len(presetMapping) {
  217. return "p3"
  218. }
  219. return presetMapping[l]
  220. }
  221. // QuicksyncCodec represents an instance of the Intel Quicksync Codec.
  222. type QuicksyncCodec struct {
  223. }
  224. // Name returns the codec name.
  225. func (c *QuicksyncCodec) Name() string {
  226. return "h264_qsv"
  227. }
  228. // DisplayName returns the human readable name of the codec.
  229. func (c *QuicksyncCodec) DisplayName() string {
  230. return "Intel QuickSync"
  231. }
  232. // GlobalFlags are the global flags used with this codec in the transcoder.
  233. func (c *QuicksyncCodec) GlobalFlags() string {
  234. return ""
  235. }
  236. // PixelFormat is the pixel format required for this codec.
  237. func (c *QuicksyncCodec) PixelFormat() string {
  238. return "nv12"
  239. }
  240. // ExtraArguments are the extra arguments used with this codec in the transcoder.
  241. func (c *QuicksyncCodec) ExtraArguments() string {
  242. return ""
  243. }
  244. // ExtraFilters are the extra filters required for this codec in the transcoder.
  245. func (c *QuicksyncCodec) ExtraFilters() string {
  246. return ""
  247. }
  248. // VariantFlags returns a string representing a single variant processed by this codec.
  249. func (c *QuicksyncCodec) VariantFlags(v *HLSVariant) string {
  250. return ""
  251. }
  252. // GetPresetForLevel returns the string preset for this codec given an integer level.
  253. func (c *QuicksyncCodec) GetPresetForLevel(l int) string {
  254. presetMapping := []string{
  255. "ultrafast",
  256. "superfast",
  257. "veryfast",
  258. "faster",
  259. "fast",
  260. }
  261. if l >= len(presetMapping) {
  262. return "superfast"
  263. }
  264. return presetMapping[l]
  265. }
  266. // Video4Linux represents an instance of the V4L Codec.
  267. type Video4Linux struct{}
  268. // Name returns the codec name.
  269. func (c *Video4Linux) Name() string {
  270. return "h264_v4l2m2m"
  271. }
  272. // DisplayName returns the human readable name of the codec.
  273. func (c *Video4Linux) DisplayName() string {
  274. return "Video4Linux"
  275. }
  276. // GlobalFlags are the global flags used with this codec in the transcoder.
  277. func (c *Video4Linux) GlobalFlags() string {
  278. return ""
  279. }
  280. // PixelFormat is the pixel format required for this codec.
  281. func (c *Video4Linux) PixelFormat() string {
  282. return "nv21"
  283. }
  284. // ExtraArguments are the extra arguments used with this codec in the transcoder.
  285. func (c *Video4Linux) ExtraArguments() string {
  286. return ""
  287. }
  288. // ExtraFilters are the extra filters required for this codec in the transcoder.
  289. func (c *Video4Linux) ExtraFilters() string {
  290. return ""
  291. }
  292. // VariantFlags returns a string representing a single variant processed by this codec.
  293. func (c *Video4Linux) VariantFlags(v *HLSVariant) string {
  294. return ""
  295. }
  296. // GetPresetForLevel returns the string preset for this codec given an integer level.
  297. func (c *Video4Linux) GetPresetForLevel(l int) string {
  298. presetMapping := []string{
  299. "ultrafast",
  300. "superfast",
  301. "veryfast",
  302. "faster",
  303. "fast",
  304. }
  305. if l >= len(presetMapping) {
  306. return "superfast"
  307. }
  308. return presetMapping[l]
  309. }
  310. // GetCodecs will return the supported codecs available on the system.
  311. func GetCodecs(ffmpegPath string) []string {
  312. codecs := make([]string, 0)
  313. cmd := exec.Command(ffmpegPath, "-encoders")
  314. out, err := cmd.CombinedOutput()
  315. if err != nil {
  316. log.Errorln(err)
  317. return codecs
  318. }
  319. response := string(out)
  320. lines := strings.Split(response, "\n")
  321. for _, line := range lines {
  322. if strings.Contains(line, "H.264") {
  323. fields := strings.Fields(line)
  324. codec := fields[1]
  325. if _, supported := supportedCodecs[codec]; supported {
  326. codecs = append(codecs, codec)
  327. }
  328. }
  329. }
  330. return codecs
  331. }
  332. func getCodec(name string) Codec {
  333. switch name {
  334. case (&NvencCodec{}).Name():
  335. return &NvencCodec{}
  336. case (&VaapiCodec{}).Name():
  337. return &VaapiCodec{}
  338. case (&QuicksyncCodec{}).Name():
  339. return &QuicksyncCodec{}
  340. case (&OmxCodec{}).Name():
  341. return &OmxCodec{}
  342. case (&Video4Linux{}).Name():
  343. return &Video4Linux{}
  344. default:
  345. return &Libx264Codec{}
  346. }
  347. }