Commit
·
2b0ce36
1
Parent(s):
8cdcb92
audio preprocessing enhancement. fix dirs. add zip archive response.
Browse files- cli.py +18 -11
- main.py +23 -11
- utils/convert_mp4_to_mp3.py +16 -0
- utils/process_video.py +7 -3
- utils/zip_response.py +10 -0
cli.py
CHANGED
@@ -2,6 +2,7 @@ from argparse import ArgumentParser
|
|
2 |
from utils.download_video import download_video
|
3 |
from utils.transcriber import transcriber
|
4 |
from utils.subtitler import subtitler
|
|
|
5 |
import logging, os
|
6 |
from tqdm import tqdm
|
7 |
|
@@ -20,26 +21,32 @@ def main(video_url:str,
|
|
20 |
text_color:str
|
21 |
):
|
22 |
INVIDEO_DIR = os.path.join('data/',invideo_filename)
|
23 |
-
|
24 |
-
os.makedirs(INVIDEO_DIR)
|
25 |
SRT_PATH = os.path.join(INVIDEO_DIR, f"{invideo_filename}.srt")
|
26 |
OUTVIDEO_PATH = os.path.join(INVIDEO_DIR, f"result_{invideo_filename}.mp4")
|
27 |
with tqdm(total=100, desc="Overall Progress") as pbar:
|
28 |
if video_url != None:
|
29 |
-
|
30 |
-
pbar.update(
|
|
|
|
|
|
|
31 |
if not os.path.exists(SRT_PATH):
|
32 |
-
transcriber(
|
33 |
-
pbar.update(
|
34 |
-
subtitler(
|
35 |
-
pbar.update(
|
36 |
return
|
37 |
INVIDEO_PATH = os.path.join(INVIDEO_DIR, f"{invideo_filename}.mp4")
|
|
|
|
|
|
|
|
|
38 |
if not os.path.exists(SRT_PATH):
|
39 |
-
transcriber(
|
40 |
-
pbar.update(
|
41 |
subtitler(INVIDEO_PATH, SRT_PATH, OUTVIDEO_PATH, fontsize, font, bg_color, text_color)
|
42 |
-
pbar.update(
|
43 |
|
44 |
if __name__ == '__main__':
|
45 |
parser = ArgumentParser()
|
|
|
2 |
from utils.download_video import download_video
|
3 |
from utils.transcriber import transcriber
|
4 |
from utils.subtitler import subtitler
|
5 |
+
from utils.convert_mp4_to_mp3 import convert_mp4_to_mp3
|
6 |
import logging, os
|
7 |
from tqdm import tqdm
|
8 |
|
|
|
21 |
text_color:str
|
22 |
):
|
23 |
INVIDEO_DIR = os.path.join('data/',invideo_filename)
|
24 |
+
os.makedirs(INVIDEO_DIR, exist_ok=True)
|
|
|
25 |
SRT_PATH = os.path.join(INVIDEO_DIR, f"{invideo_filename}.srt")
|
26 |
OUTVIDEO_PATH = os.path.join(INVIDEO_DIR, f"result_{invideo_filename}.mp4")
|
27 |
with tqdm(total=100, desc="Overall Progress") as pbar:
|
28 |
if video_url != None:
|
29 |
+
INVIDEO_PATH = download_video(video_url, INVIDEO_DIR, invideo_filename)
|
30 |
+
pbar.update(25)
|
31 |
+
INAUDIO_PATH = os.path.join(INVIDEO_DIR, f"{invideo_filename}.mp3")
|
32 |
+
convert_mp4_to_mp3(INVIDEO_PATH,INAUDIO_PATH)
|
33 |
+
pbar.update(25)
|
34 |
if not os.path.exists(SRT_PATH):
|
35 |
+
transcriber(INAUDIO_PATH, SRT_PATH, max_words_per_line)
|
36 |
+
pbar.update(25)
|
37 |
+
subtitler(INVIDEO_PATH, SRT_PATH, OUTVIDEO_PATH,fontsize, font, bg_color, text_color)
|
38 |
+
pbar.update(25)
|
39 |
return
|
40 |
INVIDEO_PATH = os.path.join(INVIDEO_DIR, f"{invideo_filename}.mp4")
|
41 |
+
INAUDIO_PATH = os.path.join(INVIDEO_DIR, f"{invideo_filename}.mp3")
|
42 |
+
if not os.path.exists(INAUDIO_PATH):
|
43 |
+
convert_mp4_to_mp3(INVIDEO_PATH,INAUDIO_PATH)
|
44 |
+
pbar.update(50)
|
45 |
if not os.path.exists(SRT_PATH):
|
46 |
+
transcriber(INAUDIO_PATH, SRT_PATH, max_words_per_line)
|
47 |
+
pbar.update(25)
|
48 |
subtitler(INVIDEO_PATH, SRT_PATH, OUTVIDEO_PATH, fontsize, font, bg_color, text_color)
|
49 |
+
pbar.update(25)
|
50 |
|
51 |
if __name__ == '__main__':
|
52 |
parser = ArgumentParser()
|
main.py
CHANGED
@@ -2,7 +2,15 @@ from fastapi import FastAPI, UploadFile, File, HTTPException, Form
|
|
2 |
from fastapi.responses import FileResponse, HTMLResponse
|
3 |
from typing import Optional
|
4 |
from utils.process_video import process_video
|
5 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
app = FastAPI()
|
8 |
|
@@ -42,17 +50,19 @@ async def process_video_api(video_file: UploadFile = File(...),
|
|
42 |
try:
|
43 |
if not str(video_file.filename).endswith('.mp4'):
|
44 |
raise HTTPException(status_code=400, detail="Invalid file type. Please upload an MP4 file.")
|
45 |
-
|
46 |
temp_dir = os.path.join(os.getcwd(),"temp")
|
47 |
os.makedirs(temp_dir, exist_ok=True)
|
48 |
-
|
49 |
-
|
|
|
|
|
50 |
with open(temp_input_path, 'wb') as buffer:
|
51 |
try:
|
52 |
shutil.copyfileobj(video_file.file, buffer)
|
53 |
finally:
|
54 |
video_file.file.close()
|
55 |
-
|
56 |
if srt_file.size > 0:
|
57 |
SRT_PATH = os.path.abspath(f"{temp_input_path.split('.')[0]}.srt")
|
58 |
with open(SRT_PATH, 'wb') as buffer:
|
@@ -60,14 +70,16 @@ async def process_video_api(video_file: UploadFile = File(...),
|
|
60 |
shutil.copyfileobj(srt_file.file, buffer)
|
61 |
finally:
|
62 |
srt_file.file.close()
|
63 |
-
|
64 |
output_path = process_video(temp_input_path, SRT_PATH, max_words_per_line, fontsize, font, bg_color, text_color)
|
65 |
-
|
66 |
-
|
|
|
|
|
67 |
output_path = process_video(temp_input_path, None, max_words_per_line, fontsize, font, bg_color, text_color)
|
68 |
-
|
69 |
-
|
70 |
-
return
|
71 |
|
72 |
except Exception as e:
|
73 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
2 |
from fastapi.responses import FileResponse, HTMLResponse
|
3 |
from typing import Optional
|
4 |
from utils.process_video import process_video
|
5 |
+
from utils.zip_response import zip_response
|
6 |
+
import shutil, os, logging
|
7 |
+
|
8 |
+
logging.basicConfig(filename='main.log',
|
9 |
+
encoding='utf-8',
|
10 |
+
level=logging.DEBUG,
|
11 |
+
format='%(asctime)s %(levelname)s %(message)s',
|
12 |
+
datefmt='%m/%d/%Y %I:%M:%S %p')
|
13 |
+
|
14 |
|
15 |
app = FastAPI()
|
16 |
|
|
|
50 |
try:
|
51 |
if not str(video_file.filename).endswith('.mp4'):
|
52 |
raise HTTPException(status_code=400, detail="Invalid file type. Please upload an MP4 file.")
|
53 |
+
logging.info("Creating temporary directories")
|
54 |
temp_dir = os.path.join(os.getcwd(),"temp")
|
55 |
os.makedirs(temp_dir, exist_ok=True)
|
56 |
+
temp_vid_dir = os.path.join(temp_dir,video_file.filename.split('.')[0])
|
57 |
+
os.makedirs(temp_vid_dir, exist_ok=True)
|
58 |
+
temp_input_path = os.path.join(temp_vid_dir, video_file.filename)
|
59 |
+
logging.info("Copying video UploadFile to the temp_input_path")
|
60 |
with open(temp_input_path, 'wb') as buffer:
|
61 |
try:
|
62 |
shutil.copyfileobj(video_file.file, buffer)
|
63 |
finally:
|
64 |
video_file.file.close()
|
65 |
+
logging.info("Copying SRT UploadFile to the temp_input_path")
|
66 |
if srt_file.size > 0:
|
67 |
SRT_PATH = os.path.abspath(f"{temp_input_path.split('.')[0]}.srt")
|
68 |
with open(SRT_PATH, 'wb') as buffer:
|
|
|
70 |
shutil.copyfileobj(srt_file.file, buffer)
|
71 |
finally:
|
72 |
srt_file.file.close()
|
73 |
+
logging.info("Processing the video...")
|
74 |
output_path = process_video(temp_input_path, SRT_PATH, max_words_per_line, fontsize, font, bg_color, text_color)
|
75 |
+
logging.info("Archiving response...")
|
76 |
+
zip_path = zip_response(os.path.join(temp_vid_dir,"archive.zip"), [SRT_PATH, output_path])
|
77 |
+
return FileResponse(zip_path, media_type='application/zip', filename=f'result_{video_file.filename}.zip')
|
78 |
+
logging.info("Processing the video...")
|
79 |
output_path = process_video(temp_input_path, None, max_words_per_line, fontsize, font, bg_color, text_color)
|
80 |
+
logging.info("Archiving response...")
|
81 |
+
zip_path = zip_response(os.path.join(temp_vid_dir,"archive.zip"), [SRT_PATH, output_path])
|
82 |
+
return FileResponse(zip_path, media_type='application/zip', filename=f'result_{video_file.filename}.zip')
|
83 |
|
84 |
except Exception as e:
|
85 |
raise HTTPException(status_code=500, detail=str(e))
|
utils/convert_mp4_to_mp3.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from moviepy.editor import VideoFileClip
|
2 |
+
|
3 |
+
def convert_mp4_to_mp3(mp4_file_path, mp3_file_path):
|
4 |
+
# Load the video file
|
5 |
+
video_clip = VideoFileClip(mp4_file_path)
|
6 |
+
|
7 |
+
# Extract the audio from the video clip
|
8 |
+
audio_clip = video_clip.audio
|
9 |
+
|
10 |
+
# Save the audio clip as an MP3 file
|
11 |
+
audio_clip.write_audiofile(mp3_file_path)
|
12 |
+
|
13 |
+
# Close the clips
|
14 |
+
audio_clip.close()
|
15 |
+
video_clip.close()
|
16 |
+
return mp3_file_path
|
utils/process_video.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
# Import necessary modules
|
2 |
from utils.transcriber import transcriber
|
3 |
from utils.subtitler import subtitler
|
|
|
4 |
import logging, os
|
5 |
|
6 |
# Set up logging
|
@@ -19,13 +20,16 @@ def process_video(invideo_filename:str,
|
|
19 |
bg_color:str,
|
20 |
text_color:str
|
21 |
):
|
22 |
-
|
|
|
23 |
if srt_path:
|
24 |
subtitler(invideo_filename, srt_path, OUTVIDEO_PATH, fontsize, font, bg_color, text_color)
|
25 |
return OUTVIDEO_PATH
|
26 |
-
|
|
|
|
|
27 |
if not os.path.exists(SRT_PATH):
|
28 |
-
transcriber(
|
29 |
logging.info("Transcription Complete")
|
30 |
subtitler(invideo_filename, SRT_PATH, OUTVIDEO_PATH, fontsize, font, bg_color, text_color)
|
31 |
logging.info("Subtitling Complete")
|
|
|
1 |
# Import necessary modules
|
2 |
from utils.transcriber import transcriber
|
3 |
from utils.subtitler import subtitler
|
4 |
+
from utils.convert_mp4_to_mp3 import convert_mp4_to_mp3
|
5 |
import logging, os
|
6 |
|
7 |
# Set up logging
|
|
|
20 |
bg_color:str,
|
21 |
text_color:str
|
22 |
):
|
23 |
+
VIDEO_NAME = invideo_filename.split('\\')[-1]
|
24 |
+
OUTVIDEO_PATH = os.path.join(invideo_filename.split('\\')[-3], invideo_filename.split('\\')[-2], f"result_{VIDEO_NAME}")
|
25 |
if srt_path:
|
26 |
subtitler(invideo_filename, srt_path, OUTVIDEO_PATH, fontsize, font, bg_color, text_color)
|
27 |
return OUTVIDEO_PATH
|
28 |
+
INAUDIO_PATH = os.path.abspath(f"{invideo_filename.split('.')[0]}.mp3")
|
29 |
+
convert_mp4_to_mp3(invideo_filename, INAUDIO_PATH)
|
30 |
+
SRT_PATH = os.path.abspath(f"{invideo_filename.split('.')[0]}.srt")
|
31 |
if not os.path.exists(SRT_PATH):
|
32 |
+
transcriber(INAUDIO_PATH, SRT_PATH, max_words_per_line)
|
33 |
logging.info("Transcription Complete")
|
34 |
subtitler(invideo_filename, SRT_PATH, OUTVIDEO_PATH, fontsize, font, bg_color, text_color)
|
35 |
logging.info("Subtitling Complete")
|
utils/zip_response.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import zipfile, os
|
2 |
+
|
3 |
+
def zip_response(archive_name: str, files: list):
|
4 |
+
# Create a new zip file
|
5 |
+
with zipfile.ZipFile(archive_name, 'w') as zipf:
|
6 |
+
# Add specific files to the zip file
|
7 |
+
for file in files:
|
8 |
+
zipf.write(file, arcname=os.path.basename(file))
|
9 |
+
zipf.write(file, arcname=os.path.basename(file))
|
10 |
+
return archive_name
|