htmlplayer 0.1.0__tar.gz → 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: htmlplayer
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Summary: 一个简单的HTML视频播放器,使用Edge浏览器或系统默认浏览器播放视频
5
5
  Home-page: https://github.com/yourusername/htmlplayer
6
6
  Author: lenvy1
@@ -7,7 +7,7 @@ from .core import (
7
7
  calculate_window_size
8
8
  )
9
9
 
10
- __version__ = "0.1.0"
10
+ __version__ = "0.1.1"
11
11
  __all__ = [
12
12
  "play_video",
13
13
  "play_with_edge",
@@ -0,0 +1,140 @@
1
+
2
+ import subprocess
3
+ import os
4
+
5
+
6
+ def find_edge_browser():
7
+ edge_paths = [
8
+ r'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe',
9
+ r'C:\Program Files\Microsoft\Edge\Application\msedge.exe',
10
+ os.path.expanduser(r'~\AppData\Local\Microsoft\Edge\Application\msedge.exe'),
11
+ ]
12
+ for path in edge_paths:
13
+ if os.path.exists(path):
14
+ return path
15
+ return None
16
+
17
+
18
+ def calculate_window_size(width, height, scale=0.5):
19
+ """
20
+ 根据输入的宽高计算合适的窗口大小(严格等比缩放)
21
+
22
+ 参数:
23
+ width (int/float): 目标宽度(像素)
24
+ height (int/float): 目标高度(像素)
25
+ scale (float): 窗口相对于输入尺寸的缩放比例,默认0.5(即50%尺寸)
26
+
27
+ 返回:
28
+ tuple: 包含窗口宽度和高度的元组 (final_width, final_height),均为真实的浮点数
29
+ """
30
+ # 1. 跨平台自动检测屏幕分辨率
31
+ if width is None or height is None:
32
+ try:
33
+ import tkinter as tk
34
+ root = tk.Tk()
35
+ root.withdraw() # 隐藏主窗口,避免闪烁
36
+ width = root.winfo_screenwidth()
37
+ height = root.winfo_screenheight()
38
+ root.destroy()
39
+ except Exception as e:
40
+ raise RuntimeError(f"无法自动获取屏幕分辨率,请手动传入 screen_width 和 screen_height。错误信息: {e}")
41
+ # 2. 根据传入的宽高直接计算目标宽高比
42
+ target_ratio = width / height
43
+
44
+ # 3. 针对竖屏视频的视觉体验优化
45
+ # 竖屏视频(如 9:16,比例约为 0.56)若按默认比例缩放会导致画面过小,因此放大显示
46
+ if target_ratio <= 0.6:
47
+ scale = 0.9
48
+
49
+ # 4. 计算允许的最大边界
50
+ max_width = width * scale
51
+ max_height = height * scale
52
+
53
+ # 5. 核心等比缩放计算(取最小限制比例,确保不超出屏幕且保持绝对比例)
54
+ if max_width / target_ratio <= max_height:
55
+ # 宽度先触达边界,以宽度为基准计算高度
56
+ final_width = max_width
57
+ final_height = max_width / target_ratio
58
+ else:
59
+ # 高度先触达边界,以高度为基准计算宽度
60
+ final_height = max_height
61
+ final_width = max_height * target_ratio
62
+
63
+ return final_width, final_height
64
+
65
+
66
+ def play_video(url, width=None, height=None,
67
+ use_edge=True, use_browser_fallback=True):
68
+ """
69
+ 使用Edge浏览器或系统默认浏览器播放视频
70
+
71
+ 参数:
72
+ url (str): 视频URL地址
73
+ width (int, optional): 窗口宽度
74
+ height (int, optional): 窗口高度
75
+ use_edge (bool): 是否优先使用Edge浏览器,默认True
76
+ use_browser_fallback (bool): Edge不可用时是否使用默认浏览器,默认True
77
+
78
+ 返回:
79
+ bool: 播放是否成功启动
80
+ """
81
+ if not url or not isinstance(url, str):
82
+ raise ValueError("url参数无效,必须提供有效的URL字符串")
83
+
84
+ url = url.strip()
85
+ if not url.startswith('http'):
86
+ raise ValueError("url必须以http或https开头")
87
+
88
+ final_width, final_height = calculate_window_size(width=width, height=height)
89
+
90
+ edge_path = find_edge_browser() if use_edge else None
91
+
92
+ if edge_path:
93
+ subprocess.Popen([
94
+ edge_path,
95
+ '--app=' + url,
96
+ '--new-window',
97
+ f'--window-size={int(final_width)},{int(final_height)}',
98
+ '--disable-extensions',
99
+ '--disable-plugins',
100
+ '--inprivate'
101
+ ])
102
+ return True
103
+ elif use_browser_fallback:
104
+ import webbrowser
105
+ webbrowser.open(url)
106
+ return True
107
+ else:
108
+ return False
109
+
110
+
111
+ def play_with_edge(url, width=None, height=None):
112
+ """
113
+ 仅使用Edge浏览器播放视频
114
+
115
+ 参数:
116
+ url (str): 视频URL地址
117
+ width (int, optional): 窗口宽度
118
+ height (int, optional): 窗口高度
119
+
120
+ 返回:
121
+ bool: 播放是否成功启动
122
+ """
123
+ return play_video(url, width, height, use_edge=True, use_browser_fallback=False)
124
+
125
+
126
+ def play_with_browser(url, width=None, height=None):
127
+ """
128
+ 使用系统默认浏览器播放视频
129
+
130
+ 参数:
131
+ url (str): 视频URL地址
132
+ width (int, optional): 窗口宽度
133
+ height (int, optional): 窗口高度
134
+
135
+ 返回:
136
+ bool: 播放是否成功启动
137
+ """
138
+ return play_video(url, width, height,
139
+ use_edge=False, use_browser_fallback=True)
140
+
@@ -10,26 +10,18 @@ def main():
10
10
  print("HTMLPlayer 视频播放库 - 简单示例")
11
11
  print("=" * 50)
12
12
 
13
- # 示例 1: 基础用法
14
13
  print("\n1. 基础用法:")
15
14
  print(" from htmlplayer import play_video")
16
15
  print(" play_video('https://example.com/video.m3u8')")
17
16
 
18
- # 示例 2: 指定窗口大小
19
17
  print("\n2. 指定窗口大小:")
20
18
  print(" play_video('https://example.com/video.m3u8', width=1280, height=720)")
21
19
 
22
- # 示例 3: 指定视频宽高比
23
- print("\n3. 指定视频宽高比:")
24
- print(" play_video('https://example.com/video.m3u8', video_aspect_ratio=21/9)")
25
-
26
- # 示例 4: 仅使用Edge浏览器
27
- print("\n4. 仅使用Edge浏览器:")
20
+ print("\n3. 仅使用Edge浏览器:")
28
21
  print(" from htmlplayer import play_with_edge")
29
22
  print(" play_with_edge('https://example.com/video.m3u8')")
30
23
 
31
- # 示例 5: 使用系统默认浏览器
32
- print("\n5. 使用系统默认浏览器:")
24
+ print("\n4. 使用系统默认浏览器:")
33
25
  print(" from htmlplayer import play_with_browser")
34
26
  print(" play_with_browser('https://example.com/video.m3u8')")
35
27
 
@@ -39,10 +31,7 @@ def main():
39
31
 
40
32
  # 实际演示(取消注释以运行)
41
33
  # print("\n正在播放示例视频...")
42
- # play_video(
43
- # 'https://jx.xmflv.com/?url=https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8',
44
- # video_aspect_ratio=16/9
45
- # )
34
+ # play_video('https://jx.xmflv.com/?url=https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8')
46
35
 
47
36
 
48
37
  if __name__ == '__main__':
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: htmlplayer
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Summary: 一个简单的HTML视频播放器,使用Edge浏览器或系统默认浏览器播放视频
5
5
  Home-page: https://github.com/yourusername/htmlplayer
6
6
  Author: lenvy1
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
5
5
 
6
6
  [project]
7
7
  name = "htmlplayer"
8
- version = "0.1.0"
8
+ version = "0.1.1"
9
9
  authors = [
10
10
  { name = "lenvy1", email = "lenvy1@163.com" }
11
11
  ]
@@ -3,7 +3,7 @@ from setuptools import setup, find_packages
3
3
 
4
4
  setup(
5
5
  name="htmlplayer",
6
- version="0.1.0",
6
+ version="0.1.1",
7
7
  packages=find_packages(),
8
8
  include_package_data=True,
9
9
  description="一个简单的HTML视频播放器,使用Edge浏览器或系统默认浏览器播放视频",
@@ -1,121 +0,0 @@
1
-
2
- import subprocess
3
- import os
4
-
5
-
6
- def find_edge_browser():
7
- edge_paths = [
8
- r'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe',
9
- r'C:\Program Files\Microsoft\Edge\Application\msedge.exe',
10
- os.path.expanduser(r'~\AppData\Local\Microsoft\Edge\Application\msedge.exe'),
11
- ]
12
- for path in edge_paths:
13
- if os.path.exists(path):
14
- return path
15
- return None
16
-
17
-
18
- def calculate_window_size(screen_width=None, screen_height=None,
19
- video_aspect_ratio=16/9, scale=0.5):
20
- if screen_width is None or screen_height is None:
21
- import ctypes
22
- user32 = ctypes.windll.user32
23
- screen_width = user32.GetSystemMetrics(0)
24
- screen_height = user32.GetSystemMetrics(1)
25
-
26
- if video_aspect_ratio <= 0.6:
27
- scale = 0.9
28
-
29
- max_width = screen_width * scale
30
- max_height = screen_height * scale
31
-
32
- width = max_width
33
- height = width / video_aspect_ratio
34
-
35
- if height > max_height:
36
- height = max_height
37
- width = height * video_aspect_ratio
38
-
39
- return int(width), int(height)
40
-
41
-
42
- def play_video(url, width=None, height=None, video_aspect_ratio=16/9,
43
- use_edge=True, use_browser_fallback=True):
44
- """
45
- 使用Edge浏览器或系统默认浏览器播放视频
46
-
47
- 参数:
48
- url (str): 视频URL地址
49
- width (int, optional): 窗口宽度
50
- height (int, optional): 窗口高度
51
- video_aspect_ratio (float): 视频宽高比,默认16:9
52
- use_edge (bool): 是否优先使用Edge浏览器,默认True
53
- use_browser_fallback (bool): Edge不可用时是否使用默认浏览器,默认True
54
-
55
- 返回:
56
- bool: 播放是否成功启动
57
- """
58
- if not url or not isinstance(url, str):
59
- raise ValueError("url参数无效,必须提供有效的URL字符串")
60
-
61
- url = url.strip()
62
- if not url.startswith('http'):
63
- raise ValueError("url必须以http或https开头")
64
-
65
- if width is None or height is None:
66
- width, height = calculate_window_size(video_aspect_ratio=video_aspect_ratio)
67
-
68
- edge_path = find_edge_browser() if use_edge else None
69
-
70
- if edge_path:
71
- subprocess.Popen([
72
- edge_path,
73
- '--app=' + url,
74
- '--new-window',
75
- f'--window-size={width},{height}',
76
- '--disable-extensions',
77
- '--disable-plugins',
78
- '--inprivate'
79
- ])
80
- return True
81
- elif use_browser_fallback:
82
- import webbrowser
83
- webbrowser.open(url)
84
- return True
85
- else:
86
- return False
87
-
88
-
89
- def play_with_edge(url, width=None, height=None, video_aspect_ratio=16/9):
90
- """
91
- 仅使用Edge浏览器播放视频
92
-
93
- 参数:
94
- url (str): 视频URL地址
95
- width (int, optional): 窗口宽度
96
- height (int, optional): 窗口高度
97
- video_aspect_ratio (float): 视频宽高比,默认16:9
98
-
99
- 返回:
100
- bool: 播放是否成功启动
101
- """
102
- return play_video(url, width, height, video_aspect_ratio,
103
- use_edge=True, use_browser_fallback=False)
104
-
105
-
106
- def play_with_browser(url, width=None, height=None, video_aspect_ratio=16/9):
107
- """
108
- 使用系统默认浏览器播放视频
109
-
110
- 参数:
111
- url (str): 视频URL地址
112
- width (int, optional): 窗口宽度
113
- height (int, optional): 窗口高度
114
- video_aspect_ratio (float): 视频宽高比,默认16:9
115
-
116
- 返回:
117
- bool: 播放是否成功启动
118
- """
119
- return play_video(url, width, height, video_aspect_ratio,
120
- use_edge=False, use_browser_fallback=True)
121
-
File without changes
File without changes
File without changes