working!
This commit is contained in:
@@ -60,7 +60,7 @@ app.post('/api/search', async (req: Request, res: Response) => {
|
||||
// Convert video to MP3
|
||||
app.post('/api/convert', async (req: Request, res: Response) => {
|
||||
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 });
|
||||
|
||||
if (!videoId) {
|
||||
@@ -84,7 +84,7 @@ app.post('/api/convert', async (req: Request, res: Response) => {
|
||||
try {
|
||||
// Get audio stream from YouTube
|
||||
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...');
|
||||
|
||||
// Convert to MP3 using ffmpeg
|
||||
|
||||
@@ -43,12 +43,38 @@ export class SoundCloudService {
|
||||
console.log(`Searching SoundCloud for: ${query}`);
|
||||
|
||||
// Search for tracks on SoundCloud
|
||||
const tracks = await scdl.search({
|
||||
const searchResult = await scdl.search({
|
||||
query: query,
|
||||
limit: maxResults,
|
||||
resourceType: 'tracks'
|
||||
}) 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) {
|
||||
console.log('No tracks found');
|
||||
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 {
|
||||
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;
|
||||
|
||||
if (!trackInfo.streamable) {
|
||||
@@ -104,8 +138,8 @@ export class SoundCloudService {
|
||||
console.log(`Artist: ${trackInfo.user?.username || 'Unknown'}`);
|
||||
console.log(`Duration: ${Math.floor(trackInfo.duration / 1000)}s`);
|
||||
|
||||
// Get audio stream
|
||||
const stream = await scdl.download(String(trackId));
|
||||
// Use the permalink_url from track info
|
||||
const stream = await scdl.download(trackInfo.permalink_url);
|
||||
|
||||
console.log('Audio stream obtained successfully from SoundCloud');
|
||||
return stream;
|
||||
@@ -113,18 +147,28 @@ export class SoundCloudService {
|
||||
} catch (error: any) {
|
||||
console.error('SoundCloud download failed:', error.message);
|
||||
|
||||
// Try alternative approach
|
||||
// Try alternative approaches
|
||||
try {
|
||||
console.log('Trying alternative SoundCloud method...');
|
||||
const trackUrl = `https://soundcloud.com/track/${trackId}`;
|
||||
const stream = await scdl.download(trackUrl);
|
||||
console.log('Trying alternative SoundCloud methods...');
|
||||
|
||||
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;
|
||||
|
||||
} catch (fallbackError: any) {
|
||||
console.error('Alternative method also failed:', fallbackError.message);
|
||||
throw new Error(`SoundCloud download failed: ${error.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}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user