This commit is contained in:
Andrey Kondratev
2025-08-28 13:41:56 +05:00
parent 57f0519a32
commit 913f833b8c
4 changed files with 66 additions and 17 deletions

View File

@@ -10,6 +10,9 @@
"start": "node dist/server.js", "start": "node dist/server.js",
"dev": "ts-node src/server.ts", "dev": "ts-node src/server.ts",
"dev:watch": "nodemon --exec ts-node src/server.ts", "dev:watch": "nodemon --exec ts-node src/server.ts",
"dev:bg": "nohup ts-node src/server.ts > server.log 2>&1 & echo $! > server.pid && echo 'Server started in background, PID saved to server.pid'",
"stop": "if [ -f server.pid ]; then kill $(cat server.pid) && rm server.pid && echo 'Server stopped'; else echo 'No PID file found'; fi",
"logs": "tail -f server.log",
"lint": "eslint src/ public/ --ext .ts,.js", "lint": "eslint src/ public/ --ext .ts,.js",
"lint:fix": "eslint src/ public/ --ext .ts,.js --fix", "lint:fix": "eslint src/ public/ --ext .ts,.js --fix",
"validate": "npm run lint && npm run build && echo '✅ All checks passed!'", "validate": "npm run lint && npm run build && echo '✅ All checks passed!'",

View File

@@ -25,6 +25,7 @@ interface VideoResult {
channel: string; channel: string;
thumbnail: string; thumbnail: string;
duration: number; duration: number;
url: string;
} }
interface SearchResponse { interface SearchResponse {
@@ -124,7 +125,7 @@ class QuixoticApp {
} }
this.results.innerHTML = videos.map(video => ` this.results.innerHTML = videos.map(video => `
<div class="video-item" onclick="app.convertVideo('${video.id}', '${this.escapeHtml(video.title)}')"> <div class="video-item" onclick="app.convertVideo('${video.id}', '${this.escapeHtml(video.title)}', '${this.escapeHtml(video.url)}')">
<img class="video-thumbnail" src="${video.thumbnail}" alt="${this.escapeHtml(video.title)}" loading="lazy"> <img class="video-thumbnail" src="${video.thumbnail}" alt="${this.escapeHtml(video.title)}" loading="lazy">
<div class="video-info"> <div class="video-info">
<div class="video-title">${this.escapeHtml(video.title)}</div> <div class="video-title">${this.escapeHtml(video.title)}</div>
@@ -144,8 +145,8 @@ class QuixoticApp {
this.noResults.classList.remove('hidden'); this.noResults.classList.remove('hidden');
} }
public async convertVideo(videoId: string, title: string): Promise<void> { public async convertVideo(videoId: string, title: string, url: string): Promise<void> {
console.log('Convert video called:', { videoId, title }); console.log('Convert video called:', { videoId, title, url });
const videoElement = (event as any)?.currentTarget as HTMLElement; const videoElement = (event as any)?.currentTarget as HTMLElement;
if (videoElement) { if (videoElement) {
videoElement.classList.add('converting'); videoElement.classList.add('converting');
@@ -161,6 +162,7 @@ class QuixoticApp {
body: JSON.stringify({ body: JSON.stringify({
videoId, videoId,
title, title,
url,
userId: this.tg?.initDataUnsafe?.user?.id || 'demo' userId: this.tg?.initDataUnsafe?.user?.id || 'demo'
}) })
}); });

View File

@@ -60,7 +60,7 @@ app.post('/api/search', async (req: Request, res: Response) => {
// Convert video to MP3 // Convert video to MP3
app.post('/api/convert', async (req: Request, res: Response) => { app.post('/api/convert', async (req: Request, res: Response) => {
try { try {
const { videoId, title, userId }: { videoId?: string; title?: string; userId?: string } = req.body; const { videoId, title, userId, url }: { videoId?: string; title?: string; userId?: string; url?: string } = req.body;
console.log('Convert request received:', { videoId, title, userId }); console.log('Convert request received:', { videoId, title, userId });
if (!videoId) { if (!videoId) {
@@ -84,7 +84,7 @@ app.post('/api/convert', async (req: Request, res: Response) => {
try { try {
// Get audio stream from YouTube // Get audio stream from YouTube
console.log(`Attempting to get audio stream for: ${videoId}`); console.log(`Attempting to get audio stream for: ${videoId}`);
const audioStream = await soundcloud.getAudioStream(videoId); const audioStream = await soundcloud.getAudioStream(videoId, url);
console.log('Audio stream obtained, starting FFmpeg conversion...'); console.log('Audio stream obtained, starting FFmpeg conversion...');
// Convert to MP3 using ffmpeg // Convert to MP3 using ffmpeg

View File

@@ -43,12 +43,38 @@ export class SoundCloudService {
console.log(`Searching SoundCloud for: ${query}`); console.log(`Searching SoundCloud for: ${query}`);
// Search for tracks on SoundCloud // Search for tracks on SoundCloud
const tracks = await scdl.search({ const searchResult = await scdl.search({
query: query, query: query,
limit: maxResults, limit: maxResults,
resourceType: 'tracks' resourceType: 'tracks'
}) as any; }) as any;
console.log('Search result type:', typeof searchResult);
console.log('Search result:', searchResult);
// Handle different response formats
let tracks: any[] = [];
if (Array.isArray(searchResult)) {
tracks = searchResult;
} else if (searchResult && searchResult.collection && Array.isArray(searchResult.collection)) {
tracks = searchResult.collection;
} else if (searchResult && searchResult.tracks && Array.isArray(searchResult.tracks)) {
tracks = searchResult.tracks;
} else if (searchResult && typeof searchResult === 'object') {
// Try to find any array property that might contain tracks
const keys = Object.keys(searchResult);
for (const key of keys) {
if (Array.isArray(searchResult[key]) && searchResult[key].length > 0) {
const firstItem = searchResult[key][0];
if (firstItem && (firstItem.id || firstItem.title || firstItem.permalink_url)) {
tracks = searchResult[key];
break;
}
}
}
}
if (!tracks || tracks.length === 0) { if (!tracks || tracks.length === 0) {
console.log('No tracks found'); console.log('No tracks found');
return []; return [];
@@ -89,11 +115,19 @@ export class SoundCloudService {
} }
} }
async getAudioStream(trackId: string | number): Promise<Readable> { async getAudioStream(trackId: string | number, trackUrl?: string): Promise<Readable> {
try { try {
console.log(`Getting audio stream for track: ${trackId}`); console.log(`Getting audio stream for track: ${trackId}`);
// Get track info first // If trackUrl is provided, use it directly
if (trackUrl) {
console.log(`Using provided track URL: ${trackUrl}`);
const stream = await scdl.download(trackUrl);
console.log('Audio stream obtained successfully from SoundCloud using URL');
return stream;
}
// Get track info first if no URL provided
const trackInfo = await scdl.getInfo(String(trackId)) as SearchTrack; const trackInfo = await scdl.getInfo(String(trackId)) as SearchTrack;
if (!trackInfo.streamable) { if (!trackInfo.streamable) {
@@ -104,8 +138,8 @@ export class SoundCloudService {
console.log(`Artist: ${trackInfo.user?.username || 'Unknown'}`); console.log(`Artist: ${trackInfo.user?.username || 'Unknown'}`);
console.log(`Duration: ${Math.floor(trackInfo.duration / 1000)}s`); console.log(`Duration: ${Math.floor(trackInfo.duration / 1000)}s`);
// Get audio stream // Use the permalink_url from track info
const stream = await scdl.download(String(trackId)); const stream = await scdl.download(trackInfo.permalink_url);
console.log('Audio stream obtained successfully from SoundCloud'); console.log('Audio stream obtained successfully from SoundCloud');
return stream; return stream;
@@ -113,19 +147,29 @@ export class SoundCloudService {
} catch (error: any) { } catch (error: any) {
console.error('SoundCloud download failed:', error.message); console.error('SoundCloud download failed:', error.message);
// Try alternative approach // Try alternative approaches
try { try {
console.log('Trying alternative SoundCloud method...'); console.log('Trying alternative SoundCloud methods...');
const trackUrl = `https://soundcloud.com/track/${trackId}`;
const stream = await scdl.download(trackUrl);
console.log('Audio stream obtained with alternative method'); // Try with track ID directly
const stream = await scdl.download(String(trackId));
console.log('Audio stream obtained with track ID method');
return stream; return stream;
} catch (fallbackError: any) { } catch (fallbackError: any) {
console.error('Alternative method also failed:', fallbackError.message); console.error('Track ID method failed, trying URL construction...');
// Final fallback - try constructing different URL formats
try {
const trackUrl = `https://soundcloud.com/${trackId}`;
const stream = await scdl.download(trackUrl);
console.log('Audio stream obtained with constructed URL method');
return stream;
} catch (finalError: any) {
console.error('All methods failed:', finalError.message);
throw new Error(`SoundCloud download failed: ${error.message}`); throw new Error(`SoundCloud download failed: ${error.message}`);
} }
} }
} }
} }
}