File size: 11,174 Bytes
747252f
 
 
daa2f7e
747252f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4220932
747252f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
daa2f7e
 
 
 
 
 
 
747252f
 
 
 
 
 
 
 
 
 
62fff3e
747252f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76d0947
747252f
76d0947
747252f
76d0947
 
 
 
 
 
747252f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
daa2f7e
747252f
 
 
 
 
 
3e9f66f
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
from faster_whisper import WhisperModel, BatchedInferencePipeline
import time
import os
import shutil
import yt_dlp
import subprocess
from typing import Optional
import logging
from fastapi import FastAPI, File, UploadFile, HTTPException, Form
from fastapi.responses import FileResponse
import os, time
from fastapi.middleware.cors import CORSMiddleware
from pathlib import Path
import zipfile
import tempfile


app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
    allow_headers=["*"],
)

logging.basicConfig()
logging.getLogger("faster_whisper").setLevel(logging.DEBUG)

def youtube_download_video(VIDEO_URL, DOWNLOAD_DIR, output_template):
    URLS = [VIDEO_URL]
    os.makedirs(DOWNLOAD_DIR, exist_ok=True)

    ydl_opts = {
        'outtmpl': output_template,
        'format': 'bestvideo[height<=1080]+bestaudio/best',
        'merge_output_format': 'mp4',
        'verbose': True
    }

    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        try:
            print(f"Downloading from YouTube: {URLS[0]}")
            info = ydl.extract_info(URLS[0], download=True)
            if not info:
                return "Error downloading youtube video"
            
            final_filepath = None
            if 'requested_downloads' in info and info['requested_downloads']:
                final_filepath = info['requested_downloads'][0]['filepath']
            elif '_filename' in info:
                final_filepath = info['_filename']
            else:
                print("Warning: yt-dlp did not provide a clear filepath. Attempting to construct.")
                if 'title' in info and 'ext' in info:
                    guessed_filename = f"{info['title']}.{info['ext']}"
                    guessed_path = os.path.join(DOWNLOAD_DIR, guessed_filename)
                    if os.path.exists(guessed_path):
                        final_filepath = guessed_path
                    else:
                        print(f"Could not determine downloaded file path for {URLS[0]}.")

        except Exception as e:
            print(f"An error occurred during YouTube download: {e}")
            final_filepath = None
        finally:
            return final_filepath
          
def local_audio_file(DOWNLOAD_DIR, AUDIO_FILE):
    try:
        potential_path = os.path.join(DOWNLOAD_DIR, AUDIO_FILE)
        if os.path.exists(potential_path):
            final_filepath = potential_path
            print(f"Using local file: {final_filepath}")
        elif os.path.exists(AUDIO_FILE):
            final_filepath = AUDIO_FILE
            print(f"Using local file: {final_filepath}")
        else:
            print(f"Local file not found at '{potential_path}' or as '{AUDIO_FILE}'")
            final_filepath = None
    except Exception as e:
        final_path = None
        print(f"Error finding file:{e}")
    finally:
        return final_filepath

def create_subtitle_chunks(segments, max_words=8, max_duration=5.0):
            subtitle_chunks = []
            
            for segment in segments:
                if hasattr(segment, 'words') and segment.words:
                    current_chunk = []
                    chunk_start = segment.words[0].start
                    
                    for i, word in enumerate(segment.words):
                        current_chunk.append(word.word)
                        
                        if (len(current_chunk) >= max_words or 
                            word.end - chunk_start >= max_duration):
                            
                            text = ''.join(current_chunk).strip()
                            subtitle_chunks.append({
                                'start': chunk_start,
                                'end': word.end,
                                'text': text
                            })
                            
                            current_chunk = []
                            if i + 1 < len(segment.words):
                                chunk_start = segment.words[i + 1].start
                    
                    if current_chunk:
                        text = ''.join(current_chunk).strip()
                        subtitle_chunks.append({
                            'start': chunk_start,
                            'end': segment.words[-1].end,
                            'text': text
                        })
                else:
                    subtitle_chunks.append({
                        'start': segment.start,
                        'end': segment.end,
                        'text': segment.text
                    })
            
            return subtitle_chunks

def format_time(seconds):
    seconds -= 0.2
    hours = int(seconds // 3600)
    minutes = int((seconds % 3600) // 60)
    seconds_remainder = seconds % 60
    milliseconds = int((seconds_remainder - int(seconds_remainder)) * 1000)
    
    return f"{hours:02d}:{minutes:02d}:{int(seconds_remainder):02d},{milliseconds:03d}"

def add_subtitles(media_path):
    base, ext = os.path.splitext(os.path.basename(media_path))
    dir_path = os.path.dirname(media_path)
    
    final_output = os.path.join(dir_path, f"{base}_subtitled.mp4")
    subtitle_file = os.path.join(dir_path, f"{base}.srt")

    if not os.path.exists(subtitle_file):
        print(f"Error: Subtitle file not found at {subtitle_file}")
        return

    video_formats = ['.mp4', '.webm', '.mpeg']
    
    try:
        if ext.lower() in video_formats:
            print('Found video file.')
            
            temp_output = os.path.join(dir_path, f"{base}_temp.mp4")
            cmd = ['ffmpeg', '-i', media_path, '-i', subtitle_file, '-c', 'copy', '-c:s', 'mov_text', temp_output, '-y']
            
            subprocess.run(cmd, check=True, capture_output=True)
            
            if ext.lower() == ".mp4":
                os.remove(media_path)
                os.rename(temp_output, media_path)
            else:
                os.rename(temp_output, final_output)
        else:
            print('Found audio file.')
            temp_video = os.path.join(dir_path, f"{base}_temp.mp4")
            cmd1 = ['ffmpeg', '-f', 'lavfi', '-i', 'color=c=black:s=1280x720:r=5',
                    '-i', media_path, '-c:a', 'copy', '-shortest', temp_video, '-y']
            subprocess.run(cmd1, check=True, capture_output=True)
            
            cmd2 = ['ffmpeg', '-i', temp_video, '-i', subtitle_file, '-c', 
                    'copy', '-c:s', 'mov_text', final_output, '-y']
            subprocess.run(cmd2, check=True, capture_output=True)
            os.remove(temp_video)

        return final_output
        
    except subprocess.CalledProcessError as e:
        print(f"FFmpeg Error: {e.stderr.decode()}")
    except Exception as e:
        print(f"An error occurred: {e}")

def clean_files(path):
    if os.path.isdir(path):
        shutil.rmtree(path)

    print("Log: Cleaned all files")


@app.get('/test')
async def test_endpoint():
    return {"message": "FastAPI is working!"}

@app.post('/generate-subtitles')
async def generate_subtitles(
        file: Optional[UploadFile] = File(None),
        youtube_url: Optional[str] = Form(None)
):

    upload_dir = '/tmp/audio'
    os.makedirs(upload_dir, exist_ok=True)

    if file:
        file_path = os.path.join(upload_dir, file.filename)
        with open(file_path, "wb") as f:
            f.write(await file.read())
        final_filepath = file_path
        print(f"Uploaded file saved to {final_filepath}")
    elif youtube_url:
        output_template = os.path.join(upload_dir, "%(title)s.%(ext)s")
        final_filepath = youtube_download_video(youtube_url, upload_dir, output_template)
    else:
        raise HTTPException(status_code=400, detail="You must provide either a file or youtube URL.")


    if final_filepath and os.path.exists(final_filepath):
        print(f"Processing audio file: {final_filepath}")
        print(f"File size: {os.path.getsize(final_filepath) / 1024 / 1024:.2f} MB")

        base_name = os.path.basename(final_filepath)
        file_name_without_extension, _ = os.path.splitext(base_name)

        FILE_NAME_FOR_TXT = file_name_without_extension
        model_size = "small"

        print(f"\nLoading Whisper model: {model_size}...")
        try:
            model = WhisperModel(
                model_size,
                device="cpu",
                compute_type="int8",
                download_root="/app/models"
            )
            batched_model = BatchedInferencePipeline(model=model)
            print("Model loaded successfully.")

            print("\nStarting transcription...")
            start_time = time.time()

            segments, info = batched_model.transcribe(
                final_filepath, 
                batch_size=8,
                beam_size=5,
                word_timestamps=True
            )
            
            os.makedirs(upload_dir, exist_ok=True)
            transcript_filename = os.path.join(upload_dir, f"{FILE_NAME_FOR_TXT}.srt")

            subtitle_chunks = create_subtitle_chunks(segments, max_words=12, max_duration=4.0)

            full_transcript_text = []
            for chunk in subtitle_chunks:
                start_time_formatted = format_time(chunk['start'])
                end_time_formatted = format_time(chunk['end'])

                line = f"{start_time_formatted} --> {end_time_formatted}\n{chunk['text']}"
                full_transcript_text.append(line)
                

            with open(transcript_filename, "w", encoding="utf-8") as f:
                count = 1
                for line in full_transcript_text:
                    f.write(f"{count}\n{line}\n\n")
                    count += 1


            end_time = time.time()
            processed_time = end_time - start_time
            
            print(f"\nTranscription complete and saved to {transcript_filename}.")
            print(f"Processed in {processed_time:.2f} seconds")

            video_output = Path(final_filepath).resolve()
            subtitle_output = Path(transcript_filename).resolve()
            
            files_to_send = [video_output, subtitle_output]

            with tempfile.NamedTemporaryFile(delete=False, suffix=".zip") as tmp:
                with zipfile.ZipFile(tmp, "w", zipfile.ZIP_DEFLATED) as zf:
                    for f in files_to_send:
                        zf.write(f, arcname=f.name)
                tmp_path = tmp.name

            
            return FileResponse(tmp_path, media_type="application/zip", filename="subtitles.zip")

        except Exception as e:
            raise HTTPException(status_code=400, detail=str(e))
        
        finally:
            if 'model' in locals():
                del model
            if 'batched_model' in locals():
                del batched_model
            print("Model resources released.")
            clean_files(upload_dir)
            import gc
            gc.collect()

    else:
        raise HTTPException(status_code=400, detail="Failed to process the file.")