util.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import H265webjsModule from './h265/index'
  2. const durationFormmat = (val) => {
  3. val = val.toString()
  4. if (val.length < 2) return '0' + val
  5. return val
  6. }
  7. const setDurationText = (duration) => {
  8. if (duration < 0) return 'Player'
  9. const randDuration = Math.round(duration)
  10. return (
  11. durationFormmat(Math.floor(randDuration / 3600)) +
  12. ':' +
  13. durationFormmat(Math.floor((randDuration % 3600) / 60)) +
  14. ':' +
  15. durationFormmat(Math.floor(randDuration % 60))
  16. )
  17. }
  18. export const createPlayerServer = (videoUrl, config) => {
  19. return H265webjsModule.createPlayer(videoUrl, config)
  20. }
  21. export const executePlayerServer = (player, timelineEl) => {
  22. // todo....
  23. let mediaInfo = null
  24. player.onLoadFinish = () => {
  25. mediaInfo = player.mediaInfo()
  26. if (mediaInfo.videoType === 'vod') {
  27. timelineEl.textContent =
  28. setDurationText(0) +
  29. '/' +
  30. setDurationText(mediaInfo.meta.durationMs / 1000)
  31. }
  32. }
  33. player.onPlayTime = (pts) => {
  34. if (mediaInfo.videoType === 'vod') {
  35. timelineEl.textContent =
  36. setDurationText(pts) +
  37. '/' +
  38. setDurationText(mediaInfo.meta.durationMs / 1000)
  39. }
  40. }
  41. console.log(player)
  42. player.do()
  43. }
  44. /**
  45. *
  46. * @param {ReturnType<typeof H265webjsModule.createPlayer>} playerInstance
  47. */
  48. export const destoryPlayerServer = (playerInstance) => {
  49. // release playerInstance & forece Gc
  50. playerInstance.release()
  51. playerInstance = null
  52. }