Skip to content

Commit c221bba

Browse files
author
levywang
committed
feat(index.html): Support more playback video formats
fix(index.html): Player adaptation issues feat(api): Add self-built api method fix(readme): change docs
1 parent 20e1f66 commit c221bba

File tree

4 files changed

+100793
-14
lines changed

4 files changed

+100793
-14
lines changed

README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Random Web Player
2-
配合接口实现类似抖音效果的随机在线播放器,内置接口提供了 20W+ 随机地址播放
2+
配合接口实现类似抖音效果的随机在线播放器,内置接口提供了 50W+ 随机地址播放
3+
4+
5+
Cloudflare Page: https://random-web-player.pages.dev/
36

4-
Demo 地址: http://43.154.146.172/
57

6-
Cloudflare Pages: https://fuli.wwlww.org/
78

89
[![GitHub license](https://img.shields.io/github/license/levywang/random_web_player?label=License&logo=github)](https://github.com/levywang/random_web_player "Click to view the repo on Github")
910
[![Release Version](https://img.shields.io/github/release/levywang/random_web_player?include_prereleases&label=Release&logo=github)](https://github.com/levywang/random_web_player/releases/latest "Click to view the repo on Github")
@@ -22,7 +23,7 @@ Cloudflare Pages: https://fuli.wwlww.org/
2223
## 使用方法
2324
直接下载 `index.html` 文件,复制到网站根目录,配置好域名即可访问
2425

25-
你也可以设置自己的接口地址,默认接口地址为:`https://api.imwlw.com/v1/get_video`
26+
你也可以设置自己的接口地址,默认接口地址为:`https://api.wwlww.org/v1/get_video`
2627
直接查找 `index.html` 文件替换默认接口关键字替换即可
2728

2829
支持以下两种返回格式:
@@ -43,6 +44,21 @@ https://txmov2.a.yximgs.com/upic/2021/07/06/11/BMjAyMTA3MDYxMTIxNDZfMjM2MzkyOTY3
4344

4445
**PS:请确保请求地址支持跨域,否则无法正常播放**
4546

47+
## 自建API教程
48+
**注意:Python Version >= 3.7**
49+
50+
拷贝项目中的 `random_api.py` 到本地,新建目录,修改为 `main.py`
51+
52+
注意 `random_api.py` 文件中的路径地址,为爬取到的播放源地址,参考 `video_urls.txt`
53+
54+
```bash
55+
pip3 install fastapi uvicorn
56+
uvicorn main:app --host 0.0.0.0 --reload
57+
```
58+
59+
默认运行的API地址:`http://127.0.0.1:8000/`
60+
61+
可以配置反代和域名,替换 `index.html` 中的 `https://api.wwlww.org/v1/get_video`
4662

4763

4864
## 爬虫说明
@@ -67,5 +83,6 @@ pip install requests beautifulsoup4
6783
![zoomeye](./zoomeye.png)
6884

6985

86+
7087
## License
7188
This project is provided under a GNU Affero General Public License v3.0 license that can be found in the [LICENSE](LICENSE) file. By using, distributing, or contributing to this project, you agree to the terms and conditions of this license.

index.html

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<title>视频播放器</title>
77
<script src="https://s2.pstatp.com/cdn/expire-1-M/jquery/1.7.1/jquery.min.js"></script>
88
<script src="https://s0.pstatp.com/cdn/expire-1-M/hls.js/0.10.1/hls.min.js"></script>
9-
<script src="https://s2.pstatp.com/cdn/expire-1-M/dplayer/1.26.0/DPlayer.min.js"></script>
10-
<link href="https://s0.pstatp.com/cdn/expire-1-M/dplayer/1.26.0/DPlayer.min.css" rel="stylesheet" />
9+
<script src="https://s2.pstatp.com/cdn/expire-1-M/dplayer/1.25.0/DPlayer.min.js"></script>
10+
<link href="https://s0.pstatp.com/cdn/expire-1-M/dplayer/1.25.0/DPlayer.min.css" rel="stylesheet" />
1111
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
1212
<style>
1313
* {
@@ -39,7 +39,7 @@
3939
}
4040

4141
video {
42-
object-fit: cover !important;
42+
object-fit: cover;
4343
}
4444

4545
.right-controls {
@@ -223,7 +223,7 @@
223223
<div class="bottom-controls">
224224
<button class="next-button" onclick="debounceNext()">
225225
<i class="fas fa-forward"></i>
226-
下一个视频
226+
下一个
227227
</button>
228228
</div>
229229
<div class="progress-bar">
@@ -259,7 +259,7 @@
259259
// 获取视频地址
260260
async function getVideoUrl() {
261261
try {
262-
const response = await fetch('https://api.imwlw.com/v1/get_video', {
262+
const response = await fetch('https://api.wwlww.org/v1/get_video', {
263263
method: 'GET',
264264
mode: 'cors'
265265
});
@@ -297,13 +297,26 @@
297297
dp.destroy();
298298
}
299299

300+
// 检查 URL 的格式
301+
let videoType;
302+
if (url.endsWith('.mp4')) {
303+
videoType = 'auto'; // MP4 格式
304+
} else if (url.endsWith('.flv')) {
305+
videoType = 'flv'; // FLV 格式
306+
} else if (url.endsWith('.m3u8')) {
307+
videoType = 'hls'; // HLS 格式
308+
} else {
309+
console.error('不支持的视频格式');
310+
return;
311+
}
312+
300313
dp = new DPlayer({
301314
container: document.getElementById('player'),
302315
autoplay: true,
303316
theme: '#FADFA3',
304317
video: {
305318
url: url,
306-
type: 'hls',
319+
type: videoType, // 根据视频类型设置
307320
customType: {
308321
'hls': function (video, player) {
309322
const hls = new Hls({
@@ -323,17 +336,27 @@
323336
console.log("播放失败:", e);
324337
});
325338
});
339+
},
340+
'flv': function (video, player) {
341+
// FLV 播放逻辑(需要引入 flv.js)
342+
if (flvjs.isSupported()) {
343+
const flvPlayer = flvjs.createPlayer({
344+
type: 'flv',
345+
url: url
346+
});
347+
flvPlayer.attachMediaElement(video);
348+
flvPlayer.load();
349+
flvPlayer.play();
350+
} else {
351+
console.error('FLV 不被支持');
352+
}
326353
}
327354
}
328355
},
329356
preload: 'auto',
330357
volume: 1
331358
});
332359

333-
// 重置点赞数
334-
likeCount = 0;
335-
document.querySelector('.like span').textContent = '0';
336-
337360
// 更新进度条
338361
dp.on('timeupdate', () => {
339362
const percent = (dp.video.currentTime / dp.video.duration) * 100;
@@ -345,6 +368,10 @@
345368
debounceNext();
346369
});
347370

371+
// 重置点赞数
372+
likeCount = 0;
373+
document.querySelector('.like span').textContent = '0';
374+
348375
// 移除之前的事件监听器
349376
const playerElement = document.getElementById('player');
350377
playerElement.removeEventListener('touchstart', handleTouchStart);

random_api.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import sys
2+
import os
3+
from typing import Union
4+
from fastapi import FastAPI
5+
from fastapi.responses import JSONResponse
6+
from fastapi.middleware.cors import CORSMiddleware
7+
from fastapi import FastAPI, HTTPException
8+
import random
9+
10+
11+
app = FastAPI()
12+
13+
# 跨域处理
14+
app.add_middleware(
15+
CORSMiddleware,
16+
allow_origins=["*"],
17+
allow_credentials=True,
18+
allow_methods=["*"],
19+
allow_headers=["*"],
20+
)
21+
22+
def read_random_line(file_path: str) -> str:
23+
"""Reads a random line from a given file."""
24+
if not os.path.isfile(file_path):
25+
raise HTTPException(status_code=404, detail="File not found")
26+
27+
with open(file_path, 'r') as file:
28+
lines = file.readlines()
29+
30+
if not lines:
31+
raise HTTPException(status_code=400, detail="File is empty")
32+
33+
return random.choice(lines).strip()
34+
35+
36+
@app.get("/")
37+
async def get_random_video_url():
38+
"""Returns a random video URL from the video.txt file."""
39+
try:
40+
file_path = "./video_urls.txt"
41+
random_line = read_random_line(file_path)
42+
# Assuming each line contains only one URL
43+
return {"url": random_line}
44+
except Exception as e:
45+
raise HTTPException(status_code=500, detail=str(e))

0 commit comments

Comments
 (0)