forked from Franky1/Streamlit-ffmpeg-Test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit_app.py
68 lines (58 loc) · 2.39 KB
/
streamlit_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import pathlib
import subprocess
import ffmpeg
import streamlit as st
import whisper
import tempfile
# Load the Whisper model
model = whisper.load_model("base")
@st.experimental_memo
def convert_mp4_to_wav_ffmpeg_bytes2bytes(input_data: bytes) -> bytes:
"""
It converts mp3 to wav using ffmpeg
:param input_data: bytes object of a mp3 file
:return: A bytes object of a wav file.
"""
# print('convert_mp3_to_wav_ffmpeg_bytes2bytes')
args = (ffmpeg
.input('pipe:', format='mp4')
.output('pipe:', format='wav')
.global_args('-loglevel', 'error')
.get_args()
)
# print(args)
proc = subprocess.Popen(
['ffmpeg'] + args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
return proc.communicate(input=input_data)[0]
def on_file_change(uploaded_file):
"""
Handles file upload changes by converting the uploaded MP4 file to WAV format.
:param uploaded_file: Uploaded file object
:return: WAV file as bytes
"""
try:
return convert_mp4_to_wav_ffmpeg_bytes2bytes(uploaded_file.getvalue())
except Exception as e:
st.error(f"Error converting file: {e}")
return None
if __name__ == '__main__':
st.title('MP4 to WAV Converter & Transcriber')
st.markdown("""This app converts an MP4 file to WAV format, transcribes it using Whisper, and displays the transcription.""")
uploaded_mp4_file = st.file_uploader('Upload Your MP4 File', type=['mp4'])
if uploaded_mp4_file:
filename = pathlib.Path(uploaded_mp4_file.name).stem
try:
converted_wav = on_file_change(uploaded_mp4_file)
if converted_wav:
# Lưu dữ liệu WAV vào một file tạm thời
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmpfile:
tmpfile.write(converted_wav)
tmpfile_path = tmpfile.name
# Sử dụng file tạm thời với Whisper để transcribe
transcription = model.transcribe(tmpfile_path)
st.sidebar.success("Transcription Complete")
st.markdown(transcription["text"])
# Xóa file tạm thời sau khi đã sử dụng
pathlib.Path(tmpfile_path).unlink()
except Exception as e:
st.error(f"An error occurred during file processing: {e}")