Хорошо, после нескольких дней, пробовавших разные методы, я нашел решение. Действительно легко, кстати.
вот
фотография
вот код, который я использовал.
import { WebTorrent } from 'webtorrent';
declare var WebTorrent: WebTorrent;
....
playVideo() {
const client = WebTorrent();
const magnetURL = 'https://webtorrent.io/torrents/sintel.torrent';
client.add(magnetURL, function (torrent) {
// document.getElementById('hash').textContent = 'Client downloading: ' + torrent.infoHash;
torrent.files.forEach(function (file) {
torrent.on('download', function (bytes) {
document.getElementById('download').textContent = 'just downloaded: ' + bytesToSize(bytes);
document.getElementById('tdownload').textContent = 'total downloaded: ' + bytesToSize(torrent.downloaded);
document.getElementById('sdownload').textContent = 'download speed: ' + bytesToSize(torrent.downloadSpeed);
document.getElementById('pdownload').textContent = toPercentage(torrent.progress);
});
torrent.files.find(function (file) {
return file.name.endsWith('.mp4') || file.name.endsWith('.avi') || file.name.endsWith('.mkv') || file.name.endsWith('.mpeg');
});
file.renderTo('#video', function (err, element) {
presentToast(magnetURL);
});
});
});
function presentToast(text: string) {
this.presentToast(text);
}
function bytesToSize(bytes) {
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes === 0) { return '0 Bytes'; }
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
}
function toPercentage(dec) {
dec = dec.toString();
const a = dec.split('.');
dec = a[1];
dec = dec.substr(0, 4);
return dec = (dec / 100) + '%';
}
}
У меня есть 2 проблемы с этим, хотя. Я могу играть в sintel.mp4 только после того, как он достигнет 99,89%, но я хочу иметь возможность транслировать его во время загрузки.
Вторая проблема в том, что я могу загружать и играть только в Sintel.torrent. Я пытался использовать другие магнитные ссылки, и это ничего не делает. Я предполагаю, что это как-то связано с тем, как генерируется URL-адрес магнита.