utils.ts 761 Bytes
export enum VideoPlayerType {
  m3u8 = 'application/x-mpegURL',
  mp4 = 'video/mp4',
  webm = 'video/webm',
  flv = 'video/x-flv',
}

export const isRtspProtocol = (url: string) => {
  const reg = /^rtsp:\/\//g;
  return reg.test(url);
};

export const getVideoTypeByUrl = (url: string) => {
  try {
    const { protocol, pathname } = new URL(url);
    if (protocol.startsWith('rtsp:')) return VideoPlayerType.flv;

    const reg = /[^.]\w*$/;
    const mathValue = pathname.match(reg) || [];
    const ext = (mathValue[0] as keyof typeof VideoPlayerType) || 'webm';
    const type = VideoPlayerType[ext];
    return type ? type : VideoPlayerType.webm;
  } catch (error) {
    console.error(`url: '${url}' is invalid!`);
    return VideoPlayerType.webm;
  }
};