Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
doudian-py
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
baiquan
doudian-py
Commits
9dbcf762
Commit
9dbcf762
authored
Jun 30, 2025
by
baiquan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加对上传视频的长宽比检测
parent
dc1934f9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
72 additions
and
11 deletions
+72
-11
service/upload_video.py
+72
-11
No files found.
service/upload_video.py
View file @
9dbcf762
...
@@ -10,7 +10,8 @@ from datetime import datetime
...
@@ -10,7 +10,8 @@ from datetime import datetime
import
cv2
import
cv2
import
requests
import
requests
from
loguru
import
logger
from
loguru
import
logger
from
tenacity
import
retry
,
stop_after_attempt
,
wait_fixed
from
retrying
import
retry
from
tqdm
import
tqdm
from
tqdm
import
tqdm
from
utils.common
import
check_proxy
from
utils.common
import
check_proxy
...
@@ -237,7 +238,7 @@ class Upload:
...
@@ -237,7 +238,7 @@ class Upload:
self
.
headers
=
task
.
get
(
'headers'
)
self
.
headers
=
task
.
get
(
'headers'
)
self
.
file_path_
=
task
.
get
(
'file_path_'
)
self
.
file_path_
=
task
.
get
(
'file_path_'
)
@retry
(
stop
=
stop_after_attempt
(
5
),
wait
=
wait_fixed
(
1
)
)
@retry
(
stop
_max_attempt_number
=
3
,
wait_fixed
=
1000
)
def
get_upload_token
(
self
,
upload_id
=
None
):
def
get_upload_token
(
self
,
upload_id
=
None
):
"""
"""
获取上传token
获取上传token
...
@@ -256,12 +257,15 @@ class Upload:
...
@@ -256,12 +257,15 @@ class Upload:
}
}
url
=
f
'https://fxg.jinritemai.com/product/video/uploadVideoToken'
url
=
f
'https://fxg.jinritemai.com/product/video/uploadVideoToken'
response
=
requests
.
get
(
url
,
cookies
=
self
.
cookies
,
headers
=
headers
,
params
=
params
,
proxies
=
self
.
proxies
)
response
=
requests
.
get
(
url
,
cookies
=
self
.
cookies
,
headers
=
headers
,
params
=
params
,
proxies
=
self
.
proxies
)
logger
.
info
(
response
.
json
())
if
"登录信息已失效"
in
response
.
text
or
response
.
json
()
.
get
(
"code"
,
-
1
)
==
"10008"
:
raise
Exception
(
f
'获取上传token失败: 登录信息已失效,请重新登录~'
)
upload_token
=
response
.
json
()[
'data'
]
upload_token
=
response
.
json
()[
'data'
]
if
upload_token
:
if
upload_token
:
return
upload_token
return
upload_token
@retry
(
stop
=
stop_after_attempt
(
5
),
wait
=
wait_fixed
(
1
)
)
@retry
(
stop
_max_attempt_number
=
3
,
wait_fixed
=
1000
)
def
get_upload_nodes
(
self
):
def
get_upload_nodes
(
self
):
"""
"""
获取上传节点
获取上传节点
...
@@ -308,7 +312,7 @@ class Upload:
...
@@ -308,7 +312,7 @@ class Upload:
if
upload_nodes
:
if
upload_nodes
:
return
upload_nodes
return
upload_nodes
@retry
(
stop
=
stop_after_attempt
(
5
),
wait
=
wait_fixed
(
1
)
)
@retry
(
stop
_max_attempt_number
=
3
,
wait_fixed
=
1000
)
def
upload_video_init
(
self
):
def
upload_video_init
(
self
):
"""
"""
上传视频初始化
上传视频初始化
...
@@ -350,7 +354,7 @@ class Upload:
...
@@ -350,7 +354,7 @@ class Upload:
logger
.
error
(
f
'upload_video_init error {e}'
)
logger
.
error
(
f
'upload_video_init error {e}'
)
continue
continue
@retry
(
stop
=
stop_after_attempt
(
5
),
wait
=
wait_fixed
(
1
)
)
@retry
(
stop
_max_attempt_number
=
3
,
wait_fixed
=
1000
)
def
upload_video_split
(
self
,
slice_
,
chunks
,
auth
,
store_uri
):
def
upload_video_split
(
self
,
slice_
,
chunks
,
auth
,
store_uri
):
"""
"""
上传视频
上传视频
...
@@ -390,7 +394,7 @@ class Upload:
...
@@ -390,7 +394,7 @@ class Upload:
if
response
.
json
()[
'success'
]
==
0
and
response
.
text
:
if
response
.
json
()[
'success'
]
==
0
and
response
.
text
:
return
response
return
response
@retry
(
stop
=
stop_after_attempt
(
5
),
wait
=
wait_fixed
(
1
)
)
@retry
(
stop
_max_attempt_number
=
3
,
wait_fixed
=
1000
)
def
upload_video_finish
(
self
,
auth
,
store_uri
):
def
upload_video_finish
(
self
,
auth
,
store_uri
):
"""
"""
上传视频完成
上传视频完成
...
@@ -425,7 +429,7 @@ class Upload:
...
@@ -425,7 +429,7 @@ class Upload:
if
response
.
json
()[
'success'
]
==
0
:
if
response
.
json
()[
'success'
]
==
0
:
return
response
return
response
@retry
(
stop
=
stop_after_attempt
(
5
),
wait
=
wait_fixed
(
1
)
)
@retry
(
stop
_max_attempt_number
=
3
,
wait_fixed
=
1000
)
def
get_upload_result
(
self
):
def
get_upload_result
(
self
):
"""
"""
获取上传结果
获取上传结果
...
@@ -554,6 +558,54 @@ class Upload:
...
@@ -554,6 +558,54 @@ class Upload:
video_info
=
self
.
get_upload_video_info
(
video_id
)
video_info
=
self
.
get_upload_video_info
(
video_id
)
return
video_id
,
video_info
return
video_id
,
video_info
def
check_video_aspect_ratio
(
video_path
):
"""
检查视频文件的长宽比是否符合1:1、3:4或9:16的比例
参数:
video_path: 视频文件路径
返回:
tuple: (是否符合要求, 实际宽高比, 最接近的目标比例)
"""
# 检查文件是否存在
if
not
os
.
path
.
exists
(
video_path
):
raise
FileNotFoundError
(
f
"视频文件不存在: {video_path}"
)
# 尝试打开视频文件
cap
=
cv2
.
VideoCapture
(
video_path
)
if
not
cap
.
isOpened
():
raise
IOError
(
f
"无法打开视频文件: {video_path}"
)
# 获取视频尺寸
width
=
int
(
cap
.
get
(
cv2
.
CAP_PROP_FRAME_WIDTH
))
height
=
int
(
cap
.
get
(
cv2
.
CAP_PROP_FRAME_HEIGHT
))
cap
.
release
()
# 确保获取到了有效的尺寸
if
width
<=
0
or
height
<=
0
:
raise
ValueError
(
f
"获取的视频尺寸无效: {width}x{height}"
)
# 计算实际宽高比
aspect_ratio
=
width
/
height
# 定义目标比例和允许的误差范围(±2%)
target_ratios
=
{
"1:1"
:
1.0
,
"3:4"
:
0.75
,
"9:16"
:
0.5625
}
# 检查是否符合任何一个目标比例
for
name
,
target
in
target_ratios
.
items
():
# 计算误差百分比
error
=
abs
(
aspect_ratio
-
target
)
/
target
if
error
<=
0.02
:
# 2%以内的误差允许
return
True
,
aspect_ratio
,
name
# 如果都不符合,则返回最接近的比例
closest_ratio
=
min
(
target_ratios
.
items
(),
key
=
lambda
x
:
abs
(
aspect_ratio
-
x
[
1
]))
return
False
,
aspect_ratio
,
closest_ratio
[
0
]
def
upload_video
(
task
):
def
upload_video
(
task
):
if
not
os
.
path
.
exists
(
VIDEO_PATH
):
if
not
os
.
path
.
exists
(
VIDEO_PATH
):
...
@@ -567,12 +619,21 @@ def upload_video(task):
...
@@ -567,12 +619,21 @@ def upload_video(task):
logger
.
info
(
f
"文件 {file_name} 不存在,开始下载"
)
logger
.
info
(
f
"文件 {file_name} 不存在,开始下载"
)
download_video
(
task
[
'video_url'
],
file_path_
,
headers
=
task
[
'headers'
])
download_video
(
task
[
'video_url'
],
file_path_
,
headers
=
task
[
'headers'
])
logger
.
success
(
"下载完成"
)
logger
.
success
(
"下载完成"
)
video_duration
=
get_video_duration
(
file_path_
)
if
video_duration
>
60
:
logger
.
error
(
"视频时长大于60秒,上传失败"
)
raise
Exception
(
"视频时长大于60秒,上传失败"
)
else
:
else
:
logger
.
info
(
f
"文件 {file_name} 已存在,跳过下载"
)
logger
.
info
(
f
"文件 {file_name} 已存在,跳过下载"
)
video_duration
=
get_video_duration
(
file_path_
)
if
video_duration
>
60
:
logger
.
error
(
"视频时长大于60秒,上传失败"
)
raise
Exception
(
"视频时长大于60秒,上传失败"
)
is_valid
,
actual_ratio
,
best_match
=
check_video_aspect_ratio
(
file_path_
)
if
is_valid
:
logger
.
info
(
f
"视频比例验证通过! ({best_match}, 实际比例: {actual_ratio:.4f})"
)
else
:
logger
.
error
(
f
"视频比例不符合要求!"
)
logger
.
error
(
f
"实际比例: {actual_ratio:.4f} ≈ {1 / actual_ratio:.2f}:1"
)
logger
.
error
(
f
"最接近的标准比例: {best_match}"
)
logger
.
error
(
f
"请上传1:1、3:4或9:16比例的视频"
)
raise
Exception
(
"视频长宽比错误,请上传1:1比例或者3:4比例或者9:16比例的主图视频"
)
task
[
'file_path_'
]
=
file_path_
task
[
'file_path_'
]
=
file_path_
upload
=
Upload
(
task
)
upload
=
Upload
(
task
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment