Skip to content

Commit

Permalink
add hls example
Browse files Browse the repository at this point in the history
  • Loading branch information
vintikzzz committed Apr 3, 2021
1 parent b71489f commit 3442f7a
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 33 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions example/dist/index.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
<title>Webtor Platform SDK Example</title>
</head>
<body>
<video id="webtor" class="video-js" controls preload="auto" />
<a href="#" data-magnet="magnet:?xt=urn:btih:08ada5a7a6183aae1e09d831df6748d566095a10">Sintel.mp4</a><br />
<a href="#" data-magnet="magnet:?xt=urn:btih:72c83366e95dd44cc85f26198ecc55f0f4576ad4">The Fanimatrix.avi</a>
<video id="webtor" class="video-js" controls preload="auto"></video>
<a href="#" id="download"></a>
<div id="status"></div>
<script src="./dist/index.js" charset="utf-8"></script>
</body>
</html>
177 changes: 175 additions & 2 deletions example/package-lock.json

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

6 changes: 4 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
"build": "webpack",
"watch": "webpack --watch"
},
"keywords": [],
"author": "",
Expand All @@ -21,6 +22,7 @@
"@babel/preset-env": "^7.7.7",
"@webtor/platform-sdk-js": "^0.1.0",
"babel-loader": "^8.0.6",
"video.js": "^7.11.4"
"video.js": "^7.11.4",
"videojs-contrib-hls": "^5.15.0"
}
}
38 changes: 29 additions & 9 deletions example/src/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
import webtor from '@webtor/platform-sdk-js';
import 'video.js/dist/video-js.css';
import videojs from 'video.js';
// import hls from 'videojs-contrib-hls';
const parseTorrent = require('parse-torrent');

async function main() {
const status = document.createElement('div');
const link = document.createElement('a');
const links = document.querySelectorAll('a[data-magnet]');
for (const l of links) {
l.addEventListener('click', function (e) {
const m = e.target.getAttribute('data-magnet');
run(m);
e.preventDefault();
return false;
});
}
}

async function run(magnetUri) {
const link = document.querySelector('#download');
link.setAttribute('target', '_blank');
link.innerHTML = 'loading...';
document.body.appendChild(link);
document.body.appendChild(status);
const status = document.querySelector('#status')
status.innerHTML = '';

const sdk = webtor({
apiUrl: 'http://127.0.0.1:32476', // you should change this
});

const magnetUri = 'magnet:?xt=urn:btih:08ada5a7a6183aae1e09d831df6748d566095a10';

let torrent = parseTorrent(magnetUri);

torrent = await sdk.torrent.pull(torrent.infoHash);
try {
torrent = await sdk.torrent.pull(torrent.infoHash);
} catch (e) {
console.log(e);
torrent = null;
}

if (!torrent) {
torrent = await sdk.magnet.fetchTorrent(magnetUri);
Expand All @@ -31,13 +46,18 @@ async function main() {

const seeder = sdk.seeder.get(torrent.infoHash);

const filePath = torrent.files[5].path;
let filePath = null;

for (const f of torrent.files) {
if (sdk.util.getMediaType(f.path) == 'video') {
filePath = f.path;
}
}

const url = await seeder.streamUrl(filePath);

const v = videojs("webtor");
v.src({
type: 'video/mp4',
src: url.toString(),
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@webtor/platform-sdk-js",
"version": "0.2.12",
"version": "0.2.13",
"description": "SDK for online torrent streaming",
"main": "src/index.js",
"repository": {
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const defaultParams = {
vod: false,
pools: {
cache: [],
worker: [],
seeder: [],
transcoder: [],
},
subdomains: false,
async getToken() {
Expand Down
Loading

0 comments on commit 3442f7a

Please sign in to comment.