Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
eshiled-new
/
funasr_web
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit fc02cfa4
authored
Sep 19, 2024
by
‘chizw’
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
[+]脚本更新
1 parent
bb849b4b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
12 deletions
utils/finetune.py
utils/finetune.py
View file @
fc02cfa
...
...
@@ -10,6 +10,8 @@ import json
import
traceback
import
sys
import
shutil
from
pydub
import
AudioSegment
from
pydub.exceptions
import
CouldntDecodeError
def
append_sys_param
(
param_name
,
default_value
=
None
):
...
...
@@ -116,6 +118,31 @@ def crop_audio(offset, duration, audio_file_path, output_path, expand=0):
]
subprocess
.
run
(
cmd
,
check
=
True
)
def
cut_audio
(
input_file
,
output_remaining_file
,
start_time_ms
,
duration_ms
):
try
:
# 加载音频文件
audio
=
AudioSegment
.
from_file
(
input_file
)
# 将秒转换为毫秒
end_time_ms
=
int
((
start_time_ms
+
duration_ms
))
# 剪切指定部分
# cut_part = audio[start_time_ms:end_time_ms]
# cut_part.export(output_cut_file, format="mp3")
# 提取剩余部分
if
start_time_ms
==
0
:
remaining_part
=
audio
[
end_time_ms
:]
else
:
remaining_part
=
audio
[:
start_time_ms
]
+
audio
[
end_time_ms
:]
remaining_part
.
export
(
output_remaining_file
,
format
=
"mp3"
)
print
(
"剪切完成!"
)
except
CouldntDecodeError
:
print
(
f
"无法解析文件:{input_file}"
)
except
Exception
as
e
:
print
(
f
"发生错误:{e}"
)
def
voice_slicing
(
id
,
generate_train
=
False
,
version
=
None
):
if
version
is
not
None
:
update_dataset_paths
(
version
)
...
...
@@ -206,36 +233,40 @@ def crop_audio_by_rule(output_duration: int):
append_sys_param
(
"env"
,
"release"
)
pattern
=
r"([一二三四五六七八九0-9]){6,}"
base_path
=
"D:/software/python/project/funasr_web/audios/train_audio/"
output_path
=
f
'{base_path}final_audio/'
down_path
=
f
'{base_path}down_audio/'
output_path
=
f
'{base_path}final_audio
1
/'
down_path
=
f
'{base_path}down_audio
1
/'
filter_text
=
""
total_duration
=
0
expand
=
0
from
mysql_service
import
funasr_db
data
=
funasr_db
.
select_funasr_audio
(
'2024-08-01'
)
with
open
(
f
"{base_path}filter
_
txt"
,
'w'
,
encoding
=
'utf-8'
)
as
file
:
with
open
(
f
"{base_path}filter
.
txt"
,
'w'
,
encoding
=
'utf-8'
)
as
file
:
for
item
in
data
:
try
:
duration
=
item
[
"recording_duration"
]
*
1000
id
=
item
[
"
id"
]
task_id
=
item
[
"task_
id"
]
recording_url
=
item
[
"recording_url"
]
down_file
=
f
"{down_path}{id}.mp3"
output_file
=
f
"{output_path}{id}.mp3"
down_file
=
f
"{down_path}{
task_
id}.mp3"
output_file
=
f
"{output_path}{
task_
id}.mp3"
download_audio
(
recording_url
,
down_file
)
output_data
=
item
[
"output_data"
]
if
output_data
is
None
:
continue
jsonobj
=
json
.
loads
(
output_data
)
for
obj
in
jsonobj
:
content
=
obj
.
get
(
"content"
,
""
)
match
=
re
.
search
(
pattern
,
content
)
if
bool
(
match
):
c
rop_audio
(
obj
.
get
(
"offset"
),
obj
.
get
(
"duration"
),
down_file
,
output_file
,
10
)
c
ut_audio
(
down_file
,
output_file
,
obj
.
get
(
"offset"
),
obj
.
get
(
"duration"
),
)
copy_and_overwrite
(
output_file
,
down_file
)
duration
-=
obj
.
get
(
"duration"
)
+
(
10
*
2
*
1000
)
filter_text
+=
f
"{id}:{content}
\n
"
file
.
write
(
filter_text
)
duration
-=
obj
.
get
(
"duration"
)
file
.
write
(
f
"{task_id}:{content}
\n
"
)
total_duration
+=
duration
log
.
info
(
f
"当前累计时长为:{total_duration} ms"
)
if
total_duration
>
output_duration
:
return
break
except
Exception
as
e
:
print
(
f
"crop_audio_by_rule item:{item}"
)
print
(
f
"crop_audio_by_rule error:{traceback.format_exc()}"
)
continue
log_text
=
f
"执行完成,文件总时长:{total_duration / 1000 / 3600} hour"
...
...
@@ -258,4 +289,4 @@ def upload_files(name, file_path):
upload_file
(
f
"{index}.wav"
,
f
"D:/software/python/project/funasr_web/dataset/temp/0830_{name}.wav"
)
crop_audio_by_rule
(
5
5
0
*
1000
*
3600
)
crop_audio_by_rule
(
5
0
0
*
1000
*
3600
)
Write
Preview
Markdown
is supported
Attach a file
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 post a comment