| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- /**
- * 测试获取视频真实播放地址功能
- * 使用方法: ts-node test-video-real-url.ts
- * 或者: npx ts-node test-video-real-url.ts
- */
- import axios from 'axios'
- import dotenv from 'dotenv'
- // 加载环境变量
- dotenv.config()
- // 配置
- const BASE_URL = process.env.BASE_URL || 'http://localhost:3010'
- const TEST_VIDEO_ID = 38230
- // 颜色输出工具
- const colors = {
- reset: '\x1b[0m',
- bright: '\x1b[1m',
- green: '\x1b[32m',
- red: '\x1b[31m',
- yellow: '\x1b[33m',
- blue: '\x1b[34m',
- cyan: '\x1b[36m'
- }
- function log(message: string, color: string = colors.reset) {
- console.log(`${color}${message}${colors.reset}`)
- }
- function logSuccess(message: string) {
- log(`✅ ${message}`, colors.green)
- }
- function logError(message: string) {
- log(`❌ ${message}`, colors.red)
- }
- function logInfo(message: string) {
- log(`ℹ️ ${message}`, colors.blue)
- }
- function logWarning(message: string) {
- log(`⚠️ ${message}`, colors.yellow)
- }
- /**
- * 测试获取视频真实播放地址
- */
- async function testGetVideoRealUrl(videoId: number) {
- try {
- logInfo(`\n开始测试获取视频真实播放地址 (视频ID: ${videoId})...`)
-
- const url = `${BASE_URL}/api/video/real-url?id=${videoId}`
- log(`请求URL: ${url}`, colors.cyan)
-
- const response = await axios.get(url, {
- timeout: 10000,
- validateStatus: () => true // 接受所有状态码
- })
-
- log(`\n响应状态码: ${response.status}`, colors.cyan)
- log(`响应数据:`, colors.cyan)
- console.log(JSON.stringify(response.data, null, 2))
-
- if (response.status === 200 && response.data.code === 1) {
- logSuccess('获取视频播放地址成功!')
- log(`\n视频ID: ${response.data.data.id}`)
- if (response.data.data.originalUrl) {
- log(`\n原始获取地址:`, colors.green)
- log(`${response.data.data.originalUrl}`, colors.bright)
- }
- if (response.data.data.proxyUrl) {
- log(`\n代理地址:`, colors.yellow)
- log(`${response.data.data.proxyUrl}`, colors.bright)
- }
- if (response.data.data.realUrl) {
- log(`\n真实地址(替换过域名):`, colors.cyan)
- log(`${response.data.data.realUrl}`, colors.bright)
- }
- if (response.data.data.bestUrl) {
- log(`\n最佳播放地址:`, colors.bright + colors.green)
- log(`${response.data.data.bestUrl}`, colors.bright)
- }
- return response.data.data
- } else {
- logError(`获取失败: ${response.data.msg || '未知错误'}`)
- return null
- }
- } catch (error: any) {
- if (axios.isAxiosError(error)) {
- if (error.response) {
- logError(`请求失败: ${error.response.status} ${error.response.statusText}`)
- logError(`错误信息: ${JSON.stringify(error.response.data, null, 2)}`)
- } else if (error.request) {
- logError('请求已发送但没有收到响应')
- logError(`请确保服务器正在运行: ${BASE_URL}`)
- } else {
- logError(`请求配置错误: ${error.message}`)
- }
- } else {
- logError(`未知错误: ${error.message || String(error)}`)
- }
- return null
- }
- }
- /**
- * 测试获取视频详情(对比用)
- */
- async function testGetVideoDetail(videoId: number) {
- try {
- logInfo(`\n开始测试获取视频详情 (视频ID: ${videoId})...`)
-
- const url = `${BASE_URL}/api/video/detail?id=${videoId}`
- log(`请求URL: ${url}`, colors.cyan)
-
- const response = await axios.get(url, {
- timeout: 10000,
- validateStatus: () => true
- })
-
- log(`\n响应状态码: ${response.status}`, colors.cyan)
-
- if (response.status === 200 && response.data.code === 1) {
- logSuccess('获取视频详情成功!')
- log(`\n视频ID: ${response.data.data.id}`)
- log(`视频标题: ${response.data.data.title}`)
- log(`播放地址(代理后): ${response.data.data.m3u8}`, colors.yellow)
- return response.data.data.m3u8
- } else {
- logError(`获取失败: ${response.data.msg || '未知错误'}`)
- return null
- }
- } catch (error: any) {
- if (axios.isAxiosError(error)) {
- if (error.response) {
- logError(`请求失败: ${error.response.status} ${error.response.statusText}`)
- } else if (error.request) {
- logError('请求已发送但没有收到响应')
- } else {
- logError(`请求配置错误: ${error.message}`)
- }
- } else {
- logError(`未知错误: ${error.message || String(error)}`)
- }
- return null
- }
- }
- /**
- * 对比三个地址
- */
- function compareUrls(urlData: { originalUrl?: string | null; proxyUrl?: string | null; realUrl?: string | null; bestUrl?: string | null } | null) {
- if (!urlData) {
- return
- }
-
- log(`\n${'='.repeat(60)}`, colors.cyan)
- log('地址对比:', colors.bright)
- log(`${'='.repeat(60)}`, colors.cyan)
-
- if (urlData.originalUrl) {
- log(`\n原始获取地址:`, colors.green)
- log(`${urlData.originalUrl}`, colors.bright)
- }
-
- if (urlData.proxyUrl) {
- log(`\n代理播放地址:`, colors.yellow)
- log(`${urlData.proxyUrl}`, colors.bright)
- }
-
- if (urlData.realUrl) {
- log(`\n真实播放地址(替换过域名):`, colors.cyan)
- log(`${urlData.realUrl}`, colors.bright)
- }
-
- if (urlData.bestUrl) {
- log(`\n最佳播放地址:`, colors.bright + colors.green)
- log(`${urlData.bestUrl}`, colors.bright)
- }
-
- // 检查地址是否不同
- if (urlData.originalUrl && urlData.realUrl && urlData.originalUrl !== urlData.realUrl) {
- logSuccess('\n✓ 真实地址已替换域名(与原始地址不同)')
- } else if (urlData.originalUrl && urlData.realUrl) {
- logWarning('\n⚠ 真实地址未替换域名(与原始地址相同,可能未配置 video_source)')
- }
-
- if (urlData.originalUrl && urlData.proxyUrl && urlData.originalUrl !== urlData.proxyUrl) {
- logSuccess('✓ 代理地址已转换(与原始地址不同)')
- } else if (urlData.originalUrl && urlData.proxyUrl) {
- logWarning('⚠ 代理地址未转换(与原始地址相同)')
- }
-
- if (urlData.bestUrl) {
- try {
- const bestUrlObj = new URL(urlData.bestUrl)
- if (bestUrlObj.protocol === 'https:') {
- logSuccess('✓ 最佳播放地址使用HTTPS,无需代理,性能最佳')
- } else if (bestUrlObj.protocol === 'http:') {
- logWarning('⚠ 最佳播放地址使用HTTP代理,可能影响性能')
- }
- } catch (e) {
- // URL解析失败,忽略
- }
- }
- }
- /**
- * 主测试函数
- */
- async function main() {
- log(`\n${'='.repeat(60)}`, colors.bright)
- log('视频真实播放地址测试', colors.bright)
- log(`${'='.repeat(60)}`, colors.bright)
- log(`\n测试配置:`, colors.cyan)
- log(`- 服务器地址: ${BASE_URL}`)
- log(`- 测试视频ID: ${TEST_VIDEO_ID}`)
-
- // 测试1: 获取完整地址信息(原始地址、代理地址、真实地址)
- const urlData = await testGetVideoRealUrl(TEST_VIDEO_ID)
-
- // 测试2: 获取视频详情(包含代理地址)
- const proxyUrl = await testGetVideoDetail(TEST_VIDEO_ID)
-
- // 对比地址
- compareUrls(urlData)
-
- log(`\n${'='.repeat(60)}`, colors.bright)
- log('测试完成', colors.bright)
- log(`${'='.repeat(60)}`, colors.bright)
- }
- // 运行测试
- main().catch(error => {
- logError(`\n测试执行失败: ${error.message}`)
- console.error(error)
- process.exit(1)
- })
|