-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
844 lines (726 loc) · 28.2 KB
/
main.js
File metadata and controls
844 lines (726 loc) · 28.2 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
// TaxVideoConvert - Main Process (sağlamlaştırılmış sürüm)
// - Menü gizli
// - ffmpeg-static ve yt-dlp, asar.unpacked veya resources/bin altından çalıştırılır
// - Yerel proxy (CORS + Range)
// - yt-dlp: bilgi/indirme, ffmpeg: trim + encode/mux
// - Renderer ile IPC
const { app, BrowserWindow, ipcMain, dialog, shell, Menu } = require('electron');
const path = require('path');
const express = require('express');
const http = require('http');
const https = require('https');
const urlLib = require('url');
const dns = require('dns');
const os = require('os');
const fs = require('fs');
const crypto = require('crypto');
const { spawn } = require('child_process');
const packageJson = require(path.join(__dirname, "package.json"));
// electron-updater'ı güvenli şekilde yükle
let autoUpdater = null;
try {
autoUpdater = require('electron-updater').autoUpdater;
} catch (err) {
console.warn('[Updater] electron-updater yüklenemedi:', err.message);
}
const ffmpegLib = require('fluent-ffmpeg');
// ffmpeg-static yerine vendor/ffmpeg kullanıyoruz (daha stabil)
let ffmpegStatic = null;
try {
ffmpegStatic = require('ffmpeg-static');
} catch (e) {
// ffmpeg-static yoksa sorun değil, vendor kullanacağız
}
const ytDlp = require('yt-dlp-exec');
const isDev = !app.isPackaged;
// ----------------------------- Yardımcılar -----------------------------
function unpackedPath(p) {
if (!p) return p;
return p.replace(/app\.asar([/\\]?)/, 'app.asar.unpacked$1');
}
// Resmi ffmpeg binary'sini çözümle
function resolveFfmpegBinary() {
const isWin = process.platform === 'win32';
const exe = isWin ? 'ffmpeg.exe' : 'ffmpeg';
const candidates = [];
// 1) vendor/ffmpeg klasöründen (öncelikli - resmi build)
candidates.push(path.join(__dirname, 'vendor', 'ffmpeg', exe));
// 2) Paketlenmiş uygulamada resources altından
if (process.resourcesPath) {
candidates.push(path.join(process.resourcesPath, 'vendor', 'ffmpeg', exe));
candidates.push(path.join(process.resourcesPath, 'app.asar.unpacked', 'vendor', 'ffmpeg', exe));
}
// 3) ffmpeg-static modülünden (fallback)
if (ffmpegStatic) {
candidates.push(unpackedPath(ffmpegStatic));
}
for (const p of candidates) {
try {
if (p && fs.existsSync(p)) {
console.log('[FFmpeg] Using:', p);
return p;
}
} catch (_) {}
}
// PATH'ten ffmpeg kullanılabilir
console.log('[FFmpeg] Using system PATH');
return 'ffmpeg';
}
// ffmpeg yolunu ayarla
const ffmpegPath = resolveFfmpegBinary();
ffmpegLib.setFfmpegPath(ffmpegPath);
function tmpFile(ext = '') {
const name = `tvc_${Date.now()}_${crypto.randomBytes(4).toString('hex')}${ext ? '.' + ext.replace(/^\./, '') : ''}`;
return path.join(os.tmpdir(), name);
}
function buildYtDlpFormatSelection({ height, videoOnly, audioOnly }) {
if (audioOnly) return 'bestaudio/best';
if (videoOnly) {
if (height) return `bestvideo[height=${height}]/bestvideo[height<=${height}]/bestvideo`;
return 'bestvideo';
}
if (height) return `bestvideo[height=${height}]+bestaudio/bestvideo[height<=${height}]+bestaudio/best`;
return 'bestvideo+bestaudio/best';
}
// yt-dlp ikilisini çözümle (asar.unpacked / extraResources / dev node_modules / PATH)
function resolveYtDlpBinary() {
const isWin = process.platform === 'win32';
const exe = isWin ? 'yt-dlp.exe' : 'yt-dlp';
const candidates = [];
// 1) Modülün bildirdiği yolun unpacked hali
const modulePath = ytDlp.path || ytDlp.getBinaryPath?.();
if (modulePath) candidates.push(unpackedPath(modulePath));
// 2) app.asar.unpacked/node_modules/... (paketli)
const resPath = process.resourcesPath; // .../resources
const unpackedRoot = path.join(resPath, 'app.asar.unpacked');
candidates.push(
path.join(unpackedRoot, 'node_modules', 'yt-dlp-exec', 'bin', exe),
path.join(unpackedRoot, 'node_modules', 'yt-dlp-exec', exe)
);
// 3) extraResources ile kopyalanmışsa (opsiyonel öneri)
candidates.push(
path.join(resPath, 'bin', 'yt-dlp', exe),
path.join(resPath, 'bin', exe)
);
// 4) Geliştirme (kaynak) dizini
candidates.push(
path.join(__dirname, 'node_modules', 'yt-dlp-exec', 'bin', exe),
path.join(__dirname, 'node_modules', 'yt-dlp-exec', exe)
);
for (const p of candidates) {
try { if (p && fs.existsSync(p)) return p; } catch (_) {}
}
return null; // PATH fallback kullanılabilir
}
let ytdlpExec = null;
const ytdlpBin = resolveYtDlpBinary();
if (ytdlpBin && typeof ytDlp.create === 'function') {
ytdlpExec = ytDlp.create(ytdlpBin);
} else {
// Fallback: paket "yt-dlp" komut adını PATH'ten denesin
ytdlpExec = ytDlp;
}
if (isDev) {
console.log('[TaxVideoConvert] yt-dlp binary:', ytdlpBin || '(PATH fallback)');
}
// ----------------------------- Proxy (CORS + Range) -----------------------------
async function isUrlSafe(targetUrl) {
try {
const parsed = urlLib.parse(targetUrl);
if (!['http:', 'https:'].includes(parsed.protocol)) return false;
const hostname = parsed.hostname;
if (!hostname) return false;
// Check known loopback/private patterns on hostname (before DNS)
if (hostname.toLowerCase() === 'localhost') return false;
if (hostname === '::1' || hostname === '[::1]') return false;
if (hostname.startsWith('127.')) return false;
if (hostname.startsWith('10.')) return false;
if (hostname.startsWith('192.168.')) return false;
if (hostname.startsWith('169.254.')) return false;
// 172.16.x.x - 172.31.x.x
const parts = hostname.split('.');
if (parts.length === 4 && parts[0] === '172') {
const second = parseInt(parts[1], 10);
if (second >= 16 && second <= 31) return false;
}
// Resolve Hostname to IP to catch domains pointing to private IPs
return new Promise((resolve) => {
dns.lookup(hostname, { family: 4 }, (err, address) => {
if (err) {
// Determine if we should fail open or closed. Failing closed (reject) is safer.
return resolve(false);
}
if (!address) return resolve(false);
// Re-check resolved IP
if (address.startsWith('127.')) return resolve(false);
if (address.startsWith('10.')) return resolve(false);
if (address.startsWith('192.168.')) return resolve(false);
if (address.startsWith('169.254.')) return resolve(false);
const ipParts = address.split('.');
if (ipParts.length === 4 && ipParts[0] === '172') {
const second = parseInt(ipParts[1], 10);
if (second >= 16 && second <= 31) return resolve(false);
}
resolve(true);
});
});
} catch (e) {
return false;
}
}
async function startProxy() {
const exapp = express();
exapp.get('/proxy', async (req, res) => {
const target = req.query.url;
if (!target) return res.status(400).send('Missing url');
// SSRF Protection
const safe = await isUrlSafe(target);
if (!safe) {
console.warn('[Proxy] Blocked unsafe URL:', target);
return res.status(403).send('Forbidden: Access to private resources or invalid URL is denied.');
}
const headers = {};
if (req.headers['range']) headers['Range'] = req.headers['range'];
headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
headers['Referer'] = 'https://www.youtube.com/';
headers['Origin'] = 'https://www.youtube.com';
headers['Accept'] = '*/*';
headers['Accept-Language'] = 'en-US,en;q=0.9';
const parsed = urlLib.parse(target);
const client = parsed.protocol === 'https:' ? https : http;
console.log('[Proxy] Fetching:', target.substring(0, 80) + '...');
const proxReq = client.get(target, { headers }, proxRes => {
console.log('[Proxy] Response status:', proxRes.statusCode, 'Content-Type:', proxRes.headers['content-type']);
// Content-Type'ı düzelt (video için)
const contentType = proxRes.headers['content-type'] || 'video/mp4';
// Response header'larını oluştur (undefined olanları filtrele)
const responseHeaders = {
'Content-Type': contentType,
'Accept-Ranges': proxRes.headers['accept-ranges'] || 'bytes',
'Access-Control-Allow-Origin': '*',
'Access-Control-Expose-Headers': 'Content-Range, Accept-Ranges, Content-Length, Content-Type',
'Cache-Control': 'no-cache'
};
// Opsiyonel header'ları sadece varsa ekle
if (proxRes.headers['content-length']) {
responseHeaders['Content-Length'] = proxRes.headers['content-length'];
}
if (proxRes.headers['content-range']) {
responseHeaders['Content-Range'] = proxRes.headers['content-range'];
}
res.writeHead(proxRes.statusCode || 200, responseHeaders);
proxRes.pipe(res);
});
proxReq.on('error', (err) => {
console.error('[Proxy] Error:', err.message);
if (!res.headersSent) res.status(502).send('Proxy error: ' + err.message);
});
req.on('close', () => {
proxReq.destroy();
});
});
return new Promise((resolve) => {
const server = exapp.listen(0, () => {
const { port } = server.address();
console.log('[Proxy] Started on port', port);
resolve({ server, port });
});
});
}
// ----------------------------- Pencere -----------------------------
let mainWindow = null;
let proxyServer = null;
let proxyPort = null;
function createMainWindow() {
const bw = new BrowserWindow({
width: 1100,
height: 740,
autoHideMenuBar: true,
icon: path.join(__dirname, 'build', 'icon.ico'),
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false,
sandbox: true
}
});
bw.removeMenu();
Menu.setApplicationMenu(null);
bw.on('closed', () => { mainWindow = null; });
bw.loadFile(path.join(__dirname, 'renderer', 'index.html'));
if (isDev) bw.webContents.openDevTools({ mode: 'detach' });
return bw;
}
// ----------------------------- Updater Ayarları -----------------------------
function setupUpdaterEvents() {
if (!autoUpdater) return; // Updater yoksa çık
autoUpdater.autoDownload = false;
autoUpdater.autoInstallOnAppQuit = true;
autoUpdater.on('checking-for-update', () => {
if (isDev) console.log('[Updater] Güncelleme kontrol ediliyor...');
});
autoUpdater.on('update-available', (info) => {
if (isDev) console.log('[Updater] Güncelleme mevcut:', info.version);
if (mainWindow) {
mainWindow.webContents.send('updateAvailable', {
version: info.version,
releaseDate: info.releaseDate,
releaseNotes: info.releaseNotes
});
}
});
autoUpdater.on('update-not-available', (info) => {
if (isDev) console.log('[Updater] Güncelleme yok, mevcut sürüm güncel.');
});
autoUpdater.on('download-progress', (progress) => {
if (mainWindow) {
mainWindow.webContents.send('updateProgress', {
percent: progress.percent,
transferred: progress.transferred,
total: progress.total,
bytesPerSecond: progress.bytesPerSecond
});
}
});
autoUpdater.on('update-downloaded', (info) => {
if (isDev) console.log('[Updater] Güncelleme indirildi:', info.version);
if (mainWindow) {
mainWindow.webContents.send('updateDownloaded', {
version: info.version
});
}
});
autoUpdater.on('error', (err) => {
console.error('[Updater] Hata:', err);
if (mainWindow) {
mainWindow.webContents.send('updateError', {
message: err.message || String(err)
});
}
});
}
// Updater IPC Handlers
ipcMain.handle('checkForUpdates', async () => {
if (!autoUpdater) return { ok: false, error: 'Updater not available' };
try {
const result = await autoUpdater.checkForUpdates();
return { ok: true, data: result };
} catch (err) {
return { ok: false, error: err.message };
}
});
ipcMain.handle('downloadUpdate', async () => {
if (!autoUpdater) return { ok: false, error: 'Updater not available' };
try {
await autoUpdater.downloadUpdate();
return { ok: true };
} catch (err) {
return { ok: false, error: err.message };
}
});
ipcMain.handle('installUpdate', () => {
if (!autoUpdater) return;
// Silent install - arka planda kurulum yapar, kurulum ekranı göstermez
autoUpdater.quitAndInstall(true, true);
});
ipcMain.handle('getAppVersion', () => {
return app.getVersion();
});
app.whenReady().then(async () => {
const { server, port } = await startProxy();
proxyServer = server;
proxyPort = port;
mainWindow = createMainWindow();
// Updater olaylarını ayarla
setupUpdaterEvents();
// Uygulama başladıktan 3 saniye sonra güncelleme kontrol et (sadece paketli uygulamada)
if (!isDev && autoUpdater) {
setTimeout(() => {
autoUpdater.checkForUpdates().catch(err => {
console.error('[Updater] Kontrol hatası:', err);
});
}, 3000);
}
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) mainWindow = createMainWindow();
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});
app.on('quit', () => {
if (proxyServer) proxyServer.close();
});
// ----------------------------- Oturum Durumu -----------------------------
const windowState = new Map(); // Map<webContentsId, { lastVideoInfo, lastVideoUrl }>
// ----------------------------- IPC: Video Bilgisi -----------------------------
ipcMain.handle('fetchVideoInfo', async (event, url, cookiesOptions) => {
const wcId = event.sender.id;
try {
// yt-dlp seçenekleri
const ytdlpOptions = {
dumpSingleJson: true,
noCheckCertificates: true,
noWarnings: true,
preferFreeFormats: false
};
// Cookies desteği
if (cookiesOptions) {
if (cookiesOptions.cookiesFrom && cookiesOptions.cookiesFrom !== 'none') {
if (cookiesOptions.cookiesFrom === 'file' && cookiesOptions.cookiesFilePath) {
// cookies.txt dosyasından çerezleri kullan
ytdlpOptions.cookies = cookiesOptions.cookiesFilePath;
} else {
// Tarayıcıdan çerezleri al (chrome, firefox, edge, brave, opera)
ytdlpOptions.cookiesFromBrowser = cookiesOptions.cookiesFrom;
}
}
}
const info = await ytdlpExec(url, ytdlpOptions);
const formats = info.formats || [];
// Tarayıcıda oynatılabilir codec ve format kontrolü
const isPlayableFormat = (f) => {
if (!f.url || !f.vcodec || f.vcodec === 'none') return false;
const vc = f.vcodec.toLowerCase();
const ext = (f.ext || '').toLowerCase();
// Sadece H.264/AVC codec'li MP4 formatları - en uyumlu
const isH264 = vc.includes('avc') || vc.includes('h264');
const isMp4 = ext === 'mp4' || ext === 'm4v';
// VP9 WebM de denenebilir ama H264 MP4 daha güvenilir
const isVp9Webm = (vc.includes('vp9') || vc.includes('vp09')) && ext === 'webm';
return (isH264 && isMp4) || isVp9Webm;
};
// Önce progressive formatları dene (hem video hem ses)
const progressive = formats
.filter(f => isPlayableFormat(f) && f.acodec && f.acodec !== 'none')
.sort((a, b) => {
// H264 MP4'ü en üste al, sonra kaliteye göre sırala
const aH264 = (a.vcodec?.toLowerCase().includes('avc')) ? 1000 : 0;
const bH264 = (b.vcodec?.toLowerCase().includes('avc')) ? 1000 : 0;
return (bH264 + (b.height || 0)) - (aH264 + (a.height || 0));
});
// Progressive yoksa video-only formatları dene
const videoOnly = formats
.filter(f => isPlayableFormat(f) && f.height && f.height <= 720)
.sort((a, b) => {
const aH264 = (a.vcodec?.toLowerCase().includes('avc')) ? 1000 : 0;
const bH264 = (b.vcodec?.toLowerCase().includes('avc')) ? 1000 : 0;
return (bH264 + (b.height || 0)) - (aH264 + (a.height || 0));
});
const previewFormat = progressive[0] || videoOnly[0] || null;
const previewUrl = previewFormat ? `http://127.0.0.1:${proxyPort}/proxy?url=${encodeURIComponent(previewFormat.url)}` : null;
console.log('[Preview] All formats count:', formats.length);
console.log('[Preview] Progressive (playable) formats:', progressive.length);
console.log('[Preview] Video-only (playable) formats:', videoOnly.length);
if (previewFormat) {
console.log('[Preview] Selected:', previewFormat.format_id, previewFormat.height + 'p', previewFormat.ext, 'vcodec:', previewFormat.vcodec, 'acodec:', previewFormat.acodec);
} else {
console.log('[Preview] No suitable format found! Available formats:');
formats.slice(0, 10).forEach(f => {
console.log(` - ${f.format_id}: ${f.ext} ${f.height}p vcodec=${f.vcodec} acodec=${f.acodec}`);
});
}
const slimFormats = formats.map(f => ({
format_id: f.format_id,
ext: f.ext,
vcodec: f.vcodec,
acodec: f.acodec,
fps: f.fps,
height: f.height,
width: f.width,
format_note: f.format_note,
tbr: f.tbr,
abr: f.abr,
asr: f.asr,
url: f.url || null,
container: f.container || null
}));
const data = {
id: info.id,
title: info.title,
duration: info.duration,
thumbnail: info.thumbnail,
thumbnails: info.thumbnails,
webpage_url: info.webpage_url || url,
uploader: info.uploader,
channel: info.channel,
formats: slimFormats,
preview: {
url: previewUrl,
format_id: previewFormat ? previewFormat.format_id : null,
height: previewFormat ? previewFormat.height : null,
ext: previewFormat ? previewFormat.ext : null
}
};
windowState.set(wcId, { lastVideoInfo: data, lastVideoUrl: url });
return { ok: true, data };
} catch (err) {
console.error('fetchVideoInfo error:', err);
return { ok: false, error: String(err?.stderr || err?.message || err) };
}
});
ipcMain.handle('getLastVideoInfo', (event) => {
const wcId = event.sender.id;
const s = windowState.get(wcId);
if (!s || !s.lastVideoInfo) return { ok: false, error: 'Oturumda video bulunamadı.' };
return { ok: true, data: s.lastVideoInfo };
});
ipcMain.handle('openEditorPage', async (event) => {
const bw = BrowserWindow.fromWebContents(event.sender);
await bw.loadFile(path.join(__dirname, 'renderer', 'editor.html'));
return { ok: true };
});
ipcMain.handle('chooseOutputPath', async (event, { defaultName, extension, defaultPath }) => {
const bw = BrowserWindow.fromWebContents(event.sender);
const documentsPath = app.getPath('documents');
const basePath = defaultPath || documentsPath;
const fullDefaultPath = path.join(basePath, `${defaultName || 'output'}.${extension || 'mp4'}`);
const { canceled, filePath } = await dialog.showSaveDialog(bw, {
title: 'Çıkış dosyasını kaydet',
defaultPath: fullDefaultPath,
filters: [
{ name: 'Video/Audio', extensions: ['mp4', 'mkv', 'webm', 'mp3', 'm4a', 'wav', 'flac', 'ogg', 'mka'] },
{ name: 'Tümü', extensions: ['*'] }
]
});
if (canceled || !filePath) return { ok: false, canceled: true };
return { ok: true, path: filePath };
});
// ----------------------------- IPC: Dışa Aktarım -----------------------------
ipcMain.handle('startExport', async (event, params) => {
const {
url,
startSec,
endSec,
qualityHeight,
container,
videoCodec,
audioCodec,
mode, // 'both' | 'video' | 'audio'
outPath,
cookiesFrom, // Cookies kaynağı
cookiesFilePath // Cookies dosya yolu
} = params;
const needAudio = mode === 'both' || mode === 'audio';
const needVideo = mode === 'both' || mode === 'video';
const ytFormat = buildYtDlpFormatSelection({
height: qualityHeight || null,
videoOnly: mode === 'video',
audioOnly: mode === 'audio'
});
let tempDir = null;
try {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'tvcjob_'));
// yt-dlp indirme seçenekleri
const ytdlpDownloadOptions = {
format: ytFormat,
'no-warnings': true,
'no-check-certificates': true,
output: path.join(tempDir, 'dl.%(ext)s')
};
// Cookies desteği
if (cookiesFrom && cookiesFrom !== 'none') {
if (cookiesFrom === 'file' && cookiesFilePath) {
ytdlpDownloadOptions.cookies = cookiesFilePath;
} else {
ytdlpDownloadOptions.cookiesFromBrowser = cookiesFrom;
}
}
// İndir (ayrı akışlarda olabilir)
await ytdlpExec(url, ytdlpDownloadOptions);
// Çıkan dosyaları belirle
const files = fs.readdirSync(tempDir).map(f => path.join(tempDir, f));
let videoFile = null;
let audioFile = null;
for (const f of files) {
const ext = path.extname(f).slice(1).toLowerCase();
if (['m4a', 'webm', 'opus', 'mp3', 'aac', 'wav', 'flac', 'ogg'].includes(ext)) {
if (!audioFile) audioFile = f;
} else if (['mp4', 'mkv', 'webm'].includes(ext)) {
if (!videoFile) videoFile = f;
}
}
if (needVideo && !videoFile && files.length > 0) videoFile = files[0];
if (needAudio && !audioFile && files.length > 1) audioFile = files.find(f => f !== videoFile) || files[0];
// FFmpeg komutu - doğrudan spawn ile çalıştır (daha stabil)
await new Promise((resolve, reject) => {
const args = [];
// Input options
args.push('-nostdin');
args.push('-hide_banner');
// Trim - input'tan önce (daha hızlı seek için)
const hasStart = typeof startSec === 'number' && startSec >= 0;
const hasEnd = typeof endSec === 'number' && endSec > 0;
if (hasStart) {
args.push('-ss', String(startSec));
}
// Inputs
if (needVideo && videoFile) {
args.push('-i', videoFile);
}
if (needAudio && audioFile && audioFile !== videoFile) {
args.push('-i', audioFile);
}
if (!needVideo && audioFile && !videoFile) {
args.push('-i', audioFile);
}
// Duration (input'tan sonra)
if (hasStart && hasEnd && endSec > startSec) {
args.push('-t', String(endSec - startSec));
}
// Video codec
if (needVideo) {
if (!videoCodec || videoCodec === 'copy') {
args.push('-c:v', 'copy');
} else {
args.push('-c:v', videoCodec);
// Codec-specific options
if (videoCodec.includes('x264') || videoCodec === 'h264' || videoCodec === 'libx264') {
args.push('-crf', '18', '-preset', 'veryfast');
} else if (videoCodec.includes('x265') || videoCodec === 'hevc' || videoCodec === 'libx265') {
args.push('-crf', '24', '-preset', 'medium');
} else if (videoCodec === 'libvpx-vp9') {
args.push('-b:v', '0', '-crf', '28', '-row-mt', '1');
} else if (videoCodec === 'libaom-av1' || videoCodec === 'av1') {
args.push('-crf', '28', '-b:v', '0');
} else if (videoCodec === 'prores_ks') {
args.push('-profile:v', '3');
}
}
} else {
args.push('-vn');
}
// Audio codec
if (needAudio) {
if (!audioCodec || audioCodec === 'copy') {
args.push('-c:a', 'copy');
} else {
args.push('-c:a', audioCodec);
if (audioCodec === 'aac') args.push('-b:a', '192k');
if (audioCodec === 'libopus') args.push('-b:a', '160k');
if (audioCodec === 'libmp3lame') args.push('-b:a', '192k');
}
} else {
args.push('-an');
}
// Stabilite ayarları
args.push('-max_muxing_queue_size', '16384');
args.push('-threads', '4'); // Sabit thread sayısı
// Output
args.push('-y'); // Overwrite
args.push('-progress', 'pipe:1'); // Progress to stdout
args.push(outPath);
console.log('[FFmpeg] Command:', ffmpegPath, args.join(' '));
const ffmpegProcess = spawn(ffmpegPath, args, {
stdio: ['ignore', 'pipe', 'pipe'], // stdin kapalı
windowsHide: true,
detached: false
});
let lastProgress = {};
const bw = BrowserWindow.fromWebContents(event.sender);
// Progress parsing
ffmpegProcess.stdout.on('data', (data) => {
const lines = data.toString().split('\n');
for (const line of lines) {
if (line.startsWith('frame=')) lastProgress.frames = parseInt(line.split('=')[1]);
if (line.startsWith('fps=')) lastProgress.fps = parseFloat(line.split('=')[1]);
if (line.startsWith('out_time=')) lastProgress.timemark = line.split('=')[1].trim();
if (line.startsWith('total_size=')) lastProgress.targetSize = parseInt(line.split('=')[1]);
if (line.startsWith('speed=')) {
// Her speed satırında progress gönder
if (bw && !bw.isDestroyed()) {
bw.webContents.send('exportProgress', {
frames: lastProgress.frames,
fps: lastProgress.fps,
targetSize: lastProgress.targetSize,
timemark: lastProgress.timemark
});
}
}
}
});
let stderrOutput = '';
ffmpegProcess.stderr.on('data', (data) => {
stderrOutput += data.toString();
// Console'a son satırı yazdır (progress için)
const lastLine = data.toString().trim().split('\n').pop();
if (lastLine && lastLine.includes('frame=')) {
process.stdout.write('\r' + lastLine);
}
});
ffmpegProcess.on('error', (err) => {
console.error('[FFmpeg] Process error:', err);
reject(new Error(`FFmpeg process error: ${err.message}`));
});
ffmpegProcess.on('close', (code) => {
console.log('\n[FFmpeg] Process exited with code:', code);
if (code === 0) {
resolve();
} else {
// Hata durumunda son 500 karakteri göster
const errorTail = stderrOutput.slice(-500);
reject(new Error(`ffmpeg exited with code ${code}: ${errorTail}`));
}
});
});
return { ok: true, outPath };
} catch (err) {
console.error('startExport error', err);
return { ok: false, error: String(err?.message || err) };
} finally {
if (tempDir) {
try { fs.rmSync(tempDir, { recursive: true, force: true }); } catch (_) {}
}
}
});
// ----------------------------- IPC: Dosyayı Klasörde Göster -----------------------------
ipcMain.handle('revealInFolder', async (event, filePath) => {
try {
await shell.showItemInFolder(filePath);
return { ok: true };
} catch (e) {
return { ok: false, error: String(e) };
}
});
// ----------------------------- IPC: Harici URL Aç -----------------------------
ipcMain.handle('openExternal', async (event, url) => {
try {
await shell.openExternal(url);
return { ok: true };
} catch (e) {
return { ok: false, error: String(e) };
}
});
// ----------------------------- IPC: Klasör Seçimi -----------------------------
ipcMain.handle('chooseFolder', async (event) => {
const bw = BrowserWindow.fromWebContents(event.sender);
const { canceled, filePaths } = await dialog.showOpenDialog(bw, {
title: 'Klasör Seç',
properties: ['openDirectory', 'createDirectory']
});
if (canceled || !filePaths || filePaths.length === 0) {
return { ok: false, canceled: true };
}
return { ok: true, path: filePaths[0] };
});
// ----------------------------- IPC: Cookies Dosyası Seçimi -----------------------------
ipcMain.handle('chooseCookiesFile', async (event) => {
const bw = BrowserWindow.fromWebContents(event.sender);
const { canceled, filePaths } = await dialog.showOpenDialog(bw, {
title: 'cookies.txt Dosyası Seç',
properties: ['openFile'],
filters: [
{ name: 'Cookies File', extensions: ['txt'] },
{ name: 'All Files', extensions: ['*'] }
]
});
if (canceled || !filePaths || filePaths.length === 0) {
return { ok: false, canceled: true };
}
return { ok: true, path: filePaths[0] };
});
// ----------------------------- IPC: Varsayılan Belgeler Yolu -----------------------------
ipcMain.handle('getDefaultDocumentsPath', async () => {
const documentsPath = app.getPath('documents');
return { ok: true, path: documentsPath };
});