Skip to content

Commit c5a7f11

Browse files
authored
Add files via upload
1 parent 2a119c5 commit c5a7f11

7 files changed

Lines changed: 1737 additions & 0 deletions

YoutubeSubtitleDownloader.dpr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
program YoutubeSubtitleDownloader;
2+
3+
uses
4+
Vcl.Forms,
5+
frmMain in 'frmMain.pas' {Form1};
6+
7+
{$R *.res}
8+
9+
begin
10+
Application.Initialize;
11+
Application.MainFormOnTaskbar := True;
12+
Application.CreateForm(TForm1, Form1);
13+
Application.Run;
14+
end.

YoutubeSubtitleDownloader.dproj

Lines changed: 1109 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<BorlandProject>
3+
<Transactions>
4+
<Transaction>2024.12.26 20:40:47.830,=C:\Users\mesut\Documents\Embarcadero\Studio\Projects\Unit1.pas</Transaction>
5+
<Transaction>2024.12.26 20:41:13.938,D:\Yazılım\Delphi\Delphi Youtube Subtitle Downloader\frmMain.dfm=C:\Users\mesut\Documents\Embarcadero\Studio\Projects\Unit1.dfm</Transaction>
6+
<Transaction>2024.12.26 20:41:13.938,D:\Yazılım\Delphi\Delphi Youtube Subtitle Downloader\frmMain.pas=C:\Users\mesut\Documents\Embarcadero\Studio\Projects\Unit1.pas</Transaction>
7+
<Transaction>2024.12.26 20:41:26.984,D:\Yazılım\Delphi\Delphi Youtube Subtitle Downloader\YoutubeSubtitleDownloader.dproj=C:\Users\mesut\Documents\Embarcadero\Studio\Projects\Project1.dproj</Transaction>
8+
</Transactions>
9+
</BorlandProject>
180 Bytes
Binary file not shown.

YoutubeSubtitleDownloader.res

151 KB
Binary file not shown.

frmMain.dfm

Lines changed: 375 additions & 0 deletions
Large diffs are not rendered by default.

frmMain.pas

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
unit frmMain;
2+
3+
interface
4+
5+
uses
6+
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
7+
System.Classes, Vcl.Graphics,
8+
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, ShellAPI,
9+
System.StrUtils, System.RegularExpressions;
10+
11+
type
12+
TDownloadResult = record
13+
Success: Boolean;
14+
Message: string;
15+
FileNames: TStringList;
16+
end;
17+
18+
type
19+
TForm1 = class(TForm)
20+
btnDownload: TBitBtn;
21+
txtYoutubeUrl: TEdit;
22+
Memo1: TMemo;
23+
mmLang: TMemo;
24+
chkforChatbot: TCheckBox;
25+
BitBtn1: TBitBtn;
26+
procedure btnDownloadClick(Sender: TObject);
27+
procedure BitBtn1Click(Sender: TObject);
28+
private
29+
{ Private declarations }
30+
public
31+
{ Public declarations }
32+
end;
33+
34+
var
35+
Form1: TForm1;
36+
37+
implementation
38+
39+
{$R *.dfm}
40+
41+
42+
43+
procedure CleanSubtitleFile_FormatSentences(const InputFile, OutputFile: string);
44+
var
45+
SLIn, SLOut: TStringList;
46+
Line, LastLine, Sentence: string;
47+
I, TempInt: Integer;
48+
begin
49+
SLIn := TStringList.Create;
50+
SLOut := TStringList.Create;
51+
try
52+
SLIn.LoadFromFile(InputFile);
53+
LastLine := '';
54+
55+
for I := 0 to SLIn.Count - 1 do
56+
begin
57+
Line := Trim(SLIn[I]);
58+
59+
// Sayý ya da zaman satýrýysa atla
60+
if (Line = '') or TryStrToInt(Line, TempInt) or (Pos('-->', Line) > 0) then
61+
Continue;
62+
63+
// Aynýysa atla
64+
if Line = LastLine then
65+
Continue;
66+
67+
// Noktadan sonra satýr bölme iþlemi
68+
Sentence := StringReplace(Line, '. ', '.' + sLineBreak, [rfReplaceAll]);
69+
SLOut.Add(Sentence);
70+
71+
LastLine := Line;
72+
end;
73+
74+
SLOut.SaveToFile(OutputFile);
75+
finally
76+
SLIn.Free;
77+
SLOut.Free;
78+
end;
79+
end;
80+
81+
82+
function validateYoutubeUrl(const URL: string): Boolean;
83+
const
84+
YouTubeUrlRegExp =
85+
'(?P<YouTubeURL>(?P<Protocol>(?:https?:)?\/\/)?(?P<Subdomain>(?:www|m)\.)?(?P<Domain>(?:youtube\.com|youtu.be))(?P<Path>\/(?:[\w\-]+\?v=|embed\/|v\/)?)(?P<VideoID>[a-zA-Z0-9_-]{11})+)';
86+
var
87+
RegEx: TRegEx;
88+
Match: TMatch;
89+
begin
90+
RegEx := TRegEx.Create(YouTubeUrlRegExp, [roIgnoreCase]);
91+
Match := RegEx.Match(URL);
92+
Result := Match.Success;
93+
end;
94+
95+
function DownloadSubtitle(const VideoURL: string;
96+
const Languages: array of string): TDownloadResult;
97+
var
98+
ExecInfo: TShellExecuteInfo;
99+
ExitCode: DWORD;
100+
CmdLine, LangStr: string;
101+
SubtitlesPath: string;
102+
i: Integer;
103+
SearchRec: TSearchRec;
104+
begin
105+
ZeroMemory(@ExecInfo, SizeOf(ExecInfo));
106+
ExecInfo.cbSize := SizeOf(ExecInfo);
107+
108+
SubtitlesPath := ExtractFilePath(ParamStr(0)) + 'Downloads\Subtitles';
109+
ForceDirectories(SubtitlesPath);
110+
111+
LangStr := '';
112+
for i := Low(Languages) to High(Languages) do
113+
begin
114+
if i > Low(Languages) then
115+
LangStr := LangStr + ',';
116+
LangStr := LangStr + Languages[i];
117+
end;
118+
119+
if LangStr = '' then
120+
LangStr := 'tr,en';
121+
122+
CmdLine := Format(
123+
'Tools\yt-dlp.exe --write-sub --write-auto-sub --convert-subs srt --skip-download --sub-lang %s -o "%s\%%(title)s.%%(ext)s" "%s"',
124+
[LangStr, SubtitlesPath, VideoURL]);
125+
126+
with ExecInfo do
127+
begin
128+
fMask := SEE_MASK_NOCLOSEPROCESS;
129+
lpVerb := 'open';
130+
lpFile := 'cmd.exe';
131+
lpParameters := PChar('/c ' + CmdLine);
132+
nShow := SW_HIDE;
133+
end;
134+
135+
Result.FileNames := TStringList.Create;
136+
137+
try
138+
if ShellExecuteEx(@ExecInfo) then
139+
begin
140+
WaitForSingleObject(ExecInfo.hProcess, INFINITE);
141+
GetExitCodeProcess(ExecInfo.hProcess, ExitCode);
142+
CloseHandle(ExecInfo.hProcess);
143+
144+
Result.Success := (ExitCode = 0);
145+
if Result.Success then
146+
begin
147+
// Yeni oluþturulan .srt dosyalarýný topla
148+
if FindFirst(SubtitlesPath + '\*.srt', faAnyFile, SearchRec) = 0 then
149+
begin
150+
repeat
151+
Result.FileNames.Add(SearchRec.Name);
152+
until FindNext(SearchRec) <> 0;
153+
System.SysUtils.FindClose(SearchRec);
154+
end;
155+
156+
Result.Message := 'Subtitle(s) downloaded successfully.';
157+
end
158+
else
159+
Result.Message := 'An error occurred while downloading the subtitle.';
160+
end
161+
else
162+
begin
163+
Result.Success := False;
164+
Result.Message := SysErrorMessage(GetLastError);
165+
end;
166+
except
167+
on E: Exception do
168+
begin
169+
Result.Success := False;
170+
Result.Message := E.Message;
171+
end;
172+
end;
173+
end;
174+
175+
procedure OpenDownloadsFolder;
176+
var
177+
DownloadsFolder: string;
178+
begin
179+
// Downloads klasörünün yolunu belirleyin (uygulamanýn bulunduðu dizinin altýndaki Downloads klasörü)
180+
DownloadsFolder := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0)))
181+
+ 'Downloads\Subtitles\';
182+
// Downloads klasörünü açma
183+
ShellExecute(0, 'open', PChar(DownloadsFolder), nil, nil, SW_SHOW);
184+
end;
185+
186+
procedure TForm1.BitBtn1Click(Sender: TObject);
187+
begin
188+
OpenDownloadsFolder;
189+
end;
190+
191+
procedure TForm1.btnDownloadClick(Sender: TObject);
192+
var
193+
Languages: array of string;
194+
i: Integer;
195+
Result: TDownloadResult;
196+
SubtitlesPath, SearchPattern: string;
197+
SR: TSearchRec;
198+
SubtitleFile, OutputFile: string;
199+
begin
200+
if validateYoutubeUrl(txtYoutubeUrl.Text) then
201+
begin
202+
SetLength(Languages, mmLang.Lines.Count);
203+
for i := 0 to mmLang.Lines.Count - 1 do
204+
Languages[i] := Trim(mmLang.Lines[i]);
205+
206+
Result := DownloadSubtitle(txtYoutubeUrl.Text, Languages);
207+
ShowMessage(Result.Message);
208+
209+
if Result.Success and chkforChatbot.Checked then
210+
begin
211+
SubtitlesPath := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) + 'Downloads\Subtitles\';
212+
SearchPattern := SubtitlesPath + '*.srt';
213+
214+
if FindFirst(SearchPattern, faAnyFile, SR) = 0 then
215+
begin
216+
repeat
217+
SubtitleFile := SubtitlesPath + SR.Name;
218+
OutputFile := ChangeFileExt(SubtitleFile, '.txt');
219+
CleanSubtitleFile_FormatSentences(SubtitleFile, OutputFile);
220+
until FindNext(SR) <> 0;
221+
FindClose(SR);
222+
end;
223+
end;
224+
225+
OpenDownloadsFolder;
226+
end
227+
else
228+
ShowMessage('Incorrect YouTube URL format.');
229+
end;
230+
end.

0 commit comments

Comments
 (0)