Skip to content

Commit

Permalink
fix: hls
Browse files Browse the repository at this point in the history
  • Loading branch information
tymmesyde committed Feb 3, 2024
1 parent e029e80 commit da50e2b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"change-case": "^5.4.2",
"core-js": "^3.35.1",
"events": "^3.3.0",
"hat": "^0.0.3",
"hls.js": "^1.5.3",
"ionicons": "^7.2.2",
"langs": "^2.0.0",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 17 additions & 30 deletions src/services/hls.service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Hls from "hls.js";
import axios from "axios";
import Hls from 'hls.js';
import hat from 'hat';
import { STREMIO_STREAMING_SERVER } from '@/common/config';

const HlsService = {

Expand All @@ -12,34 +13,20 @@ const HlsService = {
});
},

async createPlaylist(videoUrl) {
const prefix = "stream-q-";
const qualities = [{
name: 320,
bandwith: 500000
}, {
name: 480,
bandwith: 800000
}, {
name: 720,
bandwith: 1000000
}];

const playlistLevels = (await Promise.all(qualities.map(async ({ name, bandwith}) => {
try {
const streamQ = `${videoUrl}/${prefix}${name}.m3u8`;
await axios.get(streamQ);
return `#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=${bandwith},NAME="${name}"\n${streamQ}`;
} catch(e) {
return null;
}
}))).filter(level => level);

if (!playlistLevels.length) return null;

const playlistHeader = '#EXTM3U\n#EXT-X-VERSION:4\n';
const playlist = playlistHeader.concat(playlistLevels.join('\n'));
return URL.createObjectURL(new Blob([Buffer.from(playlist)], { type: 'application/x-mpegURL' }));
async createPlaylist(mediaURL) {
const id = hat();

const queryParams = new URLSearchParams([
['mediaURL', mediaURL],
['videoCodecs', 'h264'],
['videoCodecs', 'vp9'],
['audioCodecs', 'aac'],
['audioCodecs', 'mp3'],
['audioCodecs', 'opus'],
['maxAudioChannels', 2],
]);

return `${STREMIO_STREAMING_SERVER}/hlsv2/${id}/master.m3u8?${queryParams.toString()}`;
},

loadHls(playlistUrl, videoElement) {
Expand Down

0 comments on commit da50e2b

Please sign in to comment.