refactor: Explicitly preserve network-related options

This commit is contained in:
Peifan Li
2026-01-01 12:13:19 -05:00
parent a56de30dd1
commit 90a24454f6
2 changed files with 50 additions and 4 deletions

View File

@@ -37,6 +37,7 @@ export function prepareDownloadFlags(
} }
// Prepare base flags from user config (excluding output options we manage) // Prepare base flags from user config (excluding output options we manage)
// Explicitly preserve network-related options like proxy
const { const {
output: _output, // Ignore user output template (we manage this) output: _output, // Ignore user output template (we manage this)
o: _o, o: _o,
@@ -50,9 +51,18 @@ export function prepareDownloadFlags(
convertSubs: userConvertSubs, convertSubs: userConvertSubs,
// Extract user merge output format (use it if provided) // Extract user merge output format (use it if provided)
mergeOutputFormat: userMergeOutputFormat, mergeOutputFormat: userMergeOutputFormat,
proxy: _proxy, // Proxy is handled separately in networkOptions to ensure it's preserved
...safeUserConfig ...safeUserConfig
} = config; } = config;
// Explicitly preserve proxy and other network options to ensure they're not lost
// This is critical for download operations that need proxy settings
const networkOptions: Record<string, any> = {};
if (config.proxy) {
networkOptions.proxy = config.proxy;
logger.debug("Preserving proxy in networkOptions:", config.proxy);
}
// Get format sort option if user specified it // Get format sort option if user specified it
const formatSortValue = userFormatSort || userFormatSort2; const formatSortValue = userFormatSort || userFormatSort2;
@@ -73,8 +83,10 @@ export function prepareDownloadFlags(
const mergeOutputFormat = userMergeOutputFormat || defaultMergeFormat; const mergeOutputFormat = userMergeOutputFormat || defaultMergeFormat;
// Prepare flags - defaults first, then user config to allow overrides // Prepare flags - defaults first, then user config to allow overrides
// Network options (like proxy) are applied last to ensure they're not overridden
const flags: YtDlpFlags = { const flags: YtDlpFlags = {
...safeUserConfig, // Apply user config ...safeUserConfig, // Apply user config
...networkOptions, // Explicitly apply network options (proxy, etc.) to ensure they're preserved
output: outputPath, // Always use our output path with correct extension output: outputPath, // Always use our output path with correct extension
format: defaultFormat, format: defaultFormat,
// Use user preferences if provided, otherwise use defaults // Use user preferences if provided, otherwise use defaults
@@ -99,7 +111,9 @@ export function prepareDownloadFlags(
"bestvideo[ext=mp4][vcodec^=avc1]+bestaudio[ext=m4a]/bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"; "bestvideo[ext=mp4][vcodec^=avc1]+bestaudio[ext=m4a]/bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best";
} }
// Ensure merge output format is mp4 (already handled above, but log it) // Ensure merge output format is mp4 (already handled above, but log it)
logger.info("Twitter/X URL detected - using MP4 format for Safari compatibility"); logger.info(
"Twitter/X URL detected - using MP4 format for Safari compatibility"
);
} }
// Add YouTube specific flags if it's a YouTube URL // Add YouTube specific flags if it's a YouTube URL
@@ -153,6 +167,16 @@ export function prepareDownloadFlags(
delete flags.extractorArgs; delete flags.extractorArgs;
} }
// Log proxy in final flags for debugging
if (flags.proxy) {
logger.debug("Proxy in final flags:", flags.proxy);
} else if (config.proxy) {
logger.warn(
"Proxy was in config but not in final flags. Config proxy:",
config.proxy
);
}
logger.debug("Final yt-dlp flags:", flags); logger.debug("Final yt-dlp flags:", flags);
return { return {

View File

@@ -11,6 +11,7 @@ import { ProgressTracker } from "../../../utils/progressTracker";
import { import {
executeYtDlpJson, executeYtDlpJson,
executeYtDlpSpawn, executeYtDlpSpawn,
getNetworkConfigFromUserConfig,
getUserYtDlpConfig, getUserYtDlpConfig,
} from "../../../utils/ytDlpUtils"; } from "../../../utils/ytDlpUtils";
import * as storageService from "../../storageService"; import * as storageService from "../../storageService";
@@ -86,8 +87,13 @@ export async function downloadVideo(
try { try {
const PROVIDER_SCRIPT = getProviderScript(); const PROVIDER_SCRIPT = getProviderScript();
// Get user's yt-dlp configuration for network options (including proxy)
const userConfig = getUserYtDlpConfig(videoUrl);
const networkConfig = getNetworkConfigFromUserConfig(userConfig);
// Get video info first // Get video info first
const info = await executeYtDlpJson(videoUrl, { const info = await executeYtDlpJson(videoUrl, {
...networkConfig,
noWarnings: true, noWarnings: true,
preferFreeFormats: true, preferFreeFormats: true,
...(PROVIDER_SCRIPT ...(PROVIDER_SCRIPT
@@ -142,16 +148,32 @@ export async function downloadVideo(
}); });
} }
// Get user's yt-dlp configuration // Get user's yt-dlp configuration (reuse from above if available, otherwise fetch again)
const userConfig = getUserYtDlpConfig(videoUrl); // Note: userConfig was already fetched above, but we need to ensure it's still valid
const downloadUserConfig = userConfig || getUserYtDlpConfig(videoUrl);
// Log proxy configuration for debugging
if (downloadUserConfig.proxy) {
logger.info("Using proxy for download:", downloadUserConfig.proxy);
}
// Prepare download flags // Prepare download flags
const { flags, mergeOutputFormat } = prepareDownloadFlags( const { flags, mergeOutputFormat } = prepareDownloadFlags(
videoUrl, videoUrl,
newVideoPath, newVideoPath,
userConfig downloadUserConfig
); );
// Log final flags to verify proxy is included
if (flags.proxy) {
logger.info("Proxy included in download flags:", flags.proxy);
} else {
logger.warn(
"Proxy not found in download flags. User config proxy:",
downloadUserConfig.proxy
);
}
// Update the video path to use the correct extension based on merge format // Update the video path to use the correct extension based on merge format
const videoExtension = mergeOutputFormat; const videoExtension = mergeOutputFormat;
const newVideoPathWithFormat = newVideoPath.replace( const newVideoPathWithFormat = newVideoPath.replace(