youtube_explode_dart icon indicating copy to clipboard operation
youtube_explode_dart copied to clipboard

[Bug]: Youtube ban IP

Open hanan0007 opened this issue 5 months ago • 5 comments

What happened?

after some videos youtube ban the ip i need to change the internet

Stacktrace / error logs.


Package (youtube_explode_dart) version

No response

Checklist

  • [x] I have provided a descriptive title
  • [x] I have searched the issues of this repository and believe that this is not a duplicate
  • [x] I have tested with the latest version of the library
  • [x] (If relevant) I can open the video/playlist in a incognito/private window without being logged in.
  • [ ] I have sponsored the project

Additional notes

No response

hanan0007 avatar Jun 16 '25 06:06 hanan0007

Same problem

nimmalasujith avatar Jun 17 '25 17:06 nimmalasujith

Unfortunately there is nothing I can do against it; the only long-term solution would be to include the cookies of a logged-in user when requesting the video. If I have time I'll write a guide on how to do that in the coming days/week

Hexer10 avatar Jun 17 '25 17:06 Hexer10

Thank you very much! Yes, please prepare some guidance as soon as possible @Hexer10

hanan0007 avatar Jun 18 '25 10:06 hanan0007

How can I add cookies can you give me the code

nimmalasujith avatar Jun 18 '25 10:06 nimmalasujith

class CookieYT extends YoutubeHttpClient {

 @override 
   Map<String, String> get headers =>
     {
    ...super.defaultHeaders
   //... add your headers here.
   // NOTE. defaultHeaders has a 'cookies' key, you  may want to remove/ replace it
      }
}

Now pass the CookieYT in YouTubeExplode instance.

final cookieExplode = YouTubeExplode(CookieYT());

cookieYT.videos.get("...");

khaled-0 avatar Jun 19 '25 15:06 khaled-0

If I only get the video subtitles, will it cause my IP to be blocked?

patiekip avatar Jun 24 '25 13:06 patiekip

Same problem!!!

secret3579 avatar Jun 26 '25 02:06 secret3579

@khaled-0 Can you give full code for adding cookies

nimmalasujith avatar Jun 27 '25 07:06 nimmalasujith

@khaled-0 Can you give full code for adding cookies

https://github.com/khaled-0/Syncara/blob/93e9ab6502bce3c5892f48bafd13ec86f93ae3e1/lib/clients/yt_media_client.dart

but weirdly I'm still not getting videos in some cases

khaled-0 avatar Jul 16 '25 14:07 khaled-0

@khaled-0 it's mean there is no solution for this ?

hanan0007 avatar Jul 16 '25 14:07 hanan0007

@khaled-0 can we pass cookies like this

### 1. Youtube Login with webview 2. Extract Cookies 3. Repalce with Library 4. Use the library

"Is this works or Is their any other way"

late final WebViewController _controller; String _cookieString = ""; bool _loadingVideos = false; List<yt.Video> _videos = [];

@override void initState() { super.initState(); _controller = WebViewController() ..setJavaScriptMode(JavaScriptMode.unrestricted) ..setNavigationDelegate( NavigationDelegate( onProgress: (int progress) { debugPrint('WebView is loading (progress: $progress%)'); }, onPageStarted: (String url) { debugPrint('Page started loading: $url'); }, onPageFinished: (String url) { debugPrint('Page finished loading: $url'); // Optionally auto-extract cookies when page finishes loading // _getCookies(); }, onWebResourceError: (WebResourceError error) { debugPrint(''' Page resource error: Code: ${error.errorCode} Description: ${error.description} For diagnosing, look at: ${error.url} '''); }, ), ) ..loadRequest(Uri.parse('https://youtube.com')); }

Future _getCookies() async { try { final cookies = await _controller.runJavaScriptReturningResult('document.cookie'); if (cookies is String) { setState(() { _cookieString = cookies; }); debugPrint("Cookies extracted:\n$_cookieString"); } } catch (e) { debugPrint("Failed to get cookies: $e"); } }

Future _searchVideos() async { if (_cookieString.isEmpty) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('No cookies found! Please login first.')), ); return; }

setState(() {
  _loadingVideos = true;
  _videos = [];
});

// --- FIX STARTS HERE ---
final client = _CookieClient(_cookieString);
final ytClient = yt.YoutubeExplode(client); // This line now works correctly
// --- FIX ENDS HERE ---

try {
  final aaa = await ytClient.videos.streamsClient.getManifest("FbXOsVByKmk");
  print(aaa.audio.withHighestBitrate().url);

  final results = await ytClient.search.search("pawan kalyan");
  
  final limited = results;

  setState(() {
    _videos = limited;
  });
} catch (e) {
  debugPrint("Search error: $e");
  ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(content: Text('Error fetching videos: ${e.toString()}')),
  );
} finally {
  setState(() {
    _loadingVideos = false;
  });
  ytClient.close();
}

}

nimmalasujith avatar Aug 12 '25 20:08 nimmalasujith

@khaled-0 can we pass cookies like this

### 1. Youtube Login with webview 2. Extract Cookies 3. Repalce with Library 4. Use the library

"Is this works or Is their any other way"

late final WebViewController _controller; String _cookieString = ""; bool _loadingVideos = false; List<yt.Video> _videos = [];

@OverRide void initState() { super.initState(); _controller = WebViewController() ..setJavaScriptMode(JavaScriptMode.unrestricted) ..setNavigationDelegate( NavigationDelegate( onProgress: (int progress) { debugPrint('WebView is loading (progress: $progress%)'); }, onPageStarted: (String url) { debugPrint('Page started loading: $url'); }, onPageFinished: (String url) { debugPrint('Page finished loading: $url'); // Optionally auto-extract cookies when page finishes loading // _getCookies(); }, onWebResourceError: (WebResourceError error) { debugPrint(''' Page resource error: Code: ${error.errorCode} Description: ${error.description} For diagnosing, look at: ${error.url} '''); }, ), ) ..loadRequest(Uri.parse('https://youtube.com')); }

Future _getCookies() async { try { final cookies = await _controller.runJavaScriptReturningResult('document.cookie'); if (cookies is String) { setState(() { _cookieString = cookies; }); debugPrint("Cookies extracted:\n$_cookieString"); } } catch (e) { debugPrint("Failed to get cookies: $e"); } }

Future _searchVideos() async { if (_cookieString.isEmpty) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('No cookies found! Please login first.')), ); return; }

setState(() {
  _loadingVideos = true;
  _videos = [];
});

// --- FIX STARTS HERE ---
final client = _CookieClient(_cookieString);
final ytClient = yt.YoutubeExplode(client); // This line now works correctly
// --- FIX ENDS HERE ---

try {
  final aaa = await ytClient.videos.streamsClient.getManifest("FbXOsVByKmk");
  print(aaa.audio.withHighestBitrate().url);

  final results = await ytClient.search.search("pawan kalyan");
  
  final limited = results;

  setState(() {
    _videos = limited;
  });
} catch (e) {
  debugPrint("Search error: $e");
  ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(content: Text('Error fetching videos: ${e.toString()}')),
  );
} finally {
  setState(() {
    _loadingVideos = false;
  });
  ytClient.close();
}

}

it should work but cookies expire very frequently fyi

khaled-0 avatar Aug 13 '25 09:08 khaled-0

@khaled-0 but for every time I am taking cookies from it before using library.

In web view it's is saving my login. So I am opening in background and I am using the cookies before using library

nimmalasujith avatar Aug 13 '25 09:08 nimmalasujith

@nimmalasujith is it working well ? is this method working well taking cookies and use them ?

hanan0007 avatar Aug 14 '25 06:08 hanan0007

@hanan0007 I’m not sure whether it’s actually working or not. When I pass the cookies, it doesn’t show any errors — it seems to accept them — but I can’t tell if they’re being used correctly. I’ve provided my code above in case you’d like to check it.

nimmalasujith avatar Aug 14 '25 17:08 nimmalasujith

yes please give the code , i want to check how you inject these @nimmalasujith

hanan0007 avatar Aug 21 '25 06:08 hanan0007

My plugin omni_video_player takes care of this. It includes a fallback to YouTube embedded, which always works, so you can guarantee no service interruptions in production.

Definitely worth checking out!

leonardmatasel avatar Sep 25 '25 17:09 leonardmatasel

Thank you for this @leonardmatasel , but actually i need the stream link , and i want to cast to the chromecast device , not to play on the mobile phone

hanan0007 avatar Sep 26 '25 06:09 hanan0007