dlxj commited on
Commit
5f865aa
·
1 Parent(s): ae559e0

已经实现本地 m3u8 ts 播放了

Browse files
Files changed (2) hide show
  1. core.js +86 -0
  2. index.html +3 -1
core.js CHANGED
@@ -6,6 +6,7 @@
6
  const channelsContainer = document.getElementById("channelsContainer");
7
  const fileInput = document.getElementById("m3uFile");
8
  const tsInput = document.getElementById("tsFile");
 
9
 
10
  let channels = []; // Array degli elementi canale
11
  let currentSelectedIndex = -1;
@@ -152,6 +153,91 @@
152
 
153
  reader.readAsArrayBuffer(file);
154
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
 
156
  function updateSelection() {
157
  channels.forEach((channel, index) => {
 
6
  const channelsContainer = document.getElementById("channelsContainer");
7
  const fileInput = document.getElementById("m3uFile");
8
  const tsInput = document.getElementById("tsFile");
9
+ const localM3u8Input = document.getElementById("localM3u8File");
10
 
11
  let channels = []; // Array degli elementi canale
12
  let currentSelectedIndex = -1;
 
153
 
154
  reader.readAsArrayBuffer(file);
155
  });
156
+
157
+ // Event listener per la cartella locale contenente m3u8 e ts
158
+ localM3u8Input.addEventListener("change", function(event) {
159
+ const files = Array.from(event.target.files);
160
+ const m3u8File = files.find(f => f.name.endsWith('.m3u8'));
161
+
162
+ if (!m3u8File) {
163
+ alert("No .m3u8 file found in the selected folder.");
164
+ return;
165
+ }
166
+
167
+ // Reset player
168
+ if (hls) {
169
+ hls.destroy();
170
+ hls = null;
171
+ }
172
+
173
+ const reader = new FileReader();
174
+ reader.onload = function(e) {
175
+ const content = e.target.result;
176
+ const lines = content.split('\n');
177
+ const tsFilenames = lines
178
+ .map(line => line.trim())
179
+ .filter(line => line && !line.startsWith('#'));
180
+
181
+ if (tsFilenames.length === 0) {
182
+ alert("No TS segments found in m3u8 file.");
183
+ return;
184
+ }
185
+
186
+ playLocalSegments(files, tsFilenames);
187
+ };
188
+ reader.readAsText(m3u8File);
189
+ });
190
+
191
+ async function playLocalSegments(allFiles, tsFilenames) {
192
+ const transmuxer = new muxjs.mp4.Transmuxer();
193
+ const mime = 'video/mp4; codecs="avc1.42E01E,mp4a.40.2"';
194
+ const mediaSource = new MediaSource();
195
+ video.src = URL.createObjectURL(mediaSource);
196
+
197
+ mediaSource.addEventListener('sourceopen', async function() {
198
+ const sourceBuffer = mediaSource.addSourceBuffer(mime);
199
+
200
+ transmuxer.on('data', (segment) => {
201
+ const data = new Uint8Array(segment.initSegment.byteLength + segment.data.byteLength);
202
+ data.set(segment.initSegment, 0);
203
+ data.set(segment.data, segment.initSegment.byteLength);
204
+
205
+ // Append buffer safely
206
+ if (!sourceBuffer.updating) {
207
+ try {
208
+ sourceBuffer.appendBuffer(data);
209
+ } catch (e) {
210
+ console.error("Buffer append error:", e);
211
+ }
212
+ } else {
213
+ // Simple queue mechanism could be added here for robustness
214
+ console.warn("Buffer updating, dropping frame for simplicity in demo");
215
+ }
216
+ });
217
+
218
+ // Sequentially load and process TS files
219
+ for (const filename of tsFilenames) {
220
+ const tsFile = allFiles.find(f => f.name === filename);
221
+ if (tsFile) {
222
+ const arrayBuffer = await tsFile.arrayBuffer();
223
+ const data = new Uint8Array(arrayBuffer);
224
+ transmuxer.push(data);
225
+ transmuxer.flush();
226
+ // Small delay to allow buffer to process
227
+ await new Promise(r => setTimeout(r, 100));
228
+ } else {
229
+ console.warn(`File ${filename} not found in selected folder.`);
230
+ }
231
+ }
232
+
233
+ // Signal end of stream
234
+ if (mediaSource.readyState === 'open') {
235
+ mediaSource.endOfStream();
236
+ }
237
+ });
238
+
239
+ video.play().catch(console.error);
240
+ }
241
 
242
  function updateSelection() {
243
  channels.forEach((channel, index) => {
index.html CHANGED
@@ -18,10 +18,12 @@
18
  <h1>IPTV WEB Player</h1>
19
  </header>
20
  <div class="file-input">
21
- <p>Upload the .m3u or .m3u8 file of your IPTV channels:</p>
22
  <input type="file" id="m3uFile" accept=".m3u, .m3u8">
23
  <p>Or select a local .ts file to play directly:</p>
24
  <input type="file" id="tsFile" accept=".ts">
 
 
25
  <p>(You can interact with the player using mouse, touch, keyboard, gamepad, or remote control).</p>
26
  </div>
27
  <div class="main-container">
 
18
  <h1>IPTV WEB Player</h1>
19
  </header>
20
  <div class="file-input">
21
+ <p>Upload the .m3u or .m3u8 file of your Online IPTV channels:</p>
22
  <input type="file" id="m3uFile" accept=".m3u, .m3u8">
23
  <p>Or select a local .ts file to play directly:</p>
24
  <input type="file" id="tsFile" accept=".ts">
25
+ <p>Or select a local .m3u8 file and its folder to play entire stream:</p>
26
+ <input type="file" id="localM3u8File" webkitdirectory directory>
27
  <p>(You can interact with the player using mouse, touch, keyboard, gamepad, or remote control).</p>
28
  </div>
29
  <div class="main-container">