spotube/lib/services/youtube_engine/yt_dlp_engine.dart
Alessio fece073def This pull request primarily involves the removal of several configuration files and assets, as well as minor updates to documentation. The most significant changes are the deletion of various .vscode configuration files and the removal of unused assets from the project.
Configuration File Removals:

    .vscode/c_cpp_properties.json: Removed the entire configuration for C/C++ properties.
    .vscode/launch.json: Removed the Dart launch configurations for different environments and modes.
    .vscode/settings.json: Removed settings related to CMake, spell checking, file nesting, and Dart Flutter SDK path.
    .vscode/snippets.code-snippets: Removed code snippets for Dart, including PaginatedState and PaginatedNotifier templates.
    .vscode/tasks.json: Removed the tasks configuration file.

Documentation Updates:

    CONTRIBUTION.md: Removed heart emoji from the introductory text.
    README.md: Updated the logo image and made minor text adjustments, including removing emojis and updating section titles. [1] [2] [3] [4] [5]

Asset Removals:

    lib/collections/assets.gen.dart: Removed multiple unused asset references, including images related to Spotube logos and banners. [1] [2] [3]

Minor Code Cleanups:

    cli/commands/build/linux.dart, cli/commands/build/windows.dart, cli/commands/translated.dart, cli/commands/untranslated.dart: Adjusted import statements for consistency. [1] [2] [3] [4]
    integration_test/app_test.dart: Removed an unnecessary blank line.
    lib/collections/routes.dart: Commented out the TrackRoute configuration.
2025-04-13 18:40:37 +02:00

150 lines
4.1 KiB
Dart

import 'dart:convert';
import 'package:collection/collection.dart';
// ignore: depend_on_referenced_packages
import 'package:http_parser/http_parser.dart';
import 'package:spotube/services/youtube_engine/youtube_engine.dart';
import 'package:spotube/utils/platform.dart';
import 'package:youtube_explode_dart/youtube_explode_dart.dart';
import 'package:yt_dlp_dart/yt_dlp_dart.dart';
class YtDlpEngine implements YouTubeEngine {
StreamManifest _parseFormats(List formats, videoId) {
final audioOnlyStreams = formats
.where(
(f) => f["resolution"] == "audio only" && f["manifest_url"] == null,
)
.sorted((a, b) => a["quality"] > b["quality"] ? 1 : -1)
.map((f) {
final filesize = f["filesize"] ?? f["filesize_approx"];
return AudioOnlyStreamInfo(
VideoId(videoId),
0,
Uri.parse(f["url"]),
StreamContainer.parse(
f["container"]?.replaceAll("_dash", "").replaceAll("m4a", "mp4"),
),
filesize != null ? FileSize(filesize) : FileSize.unknown,
Bitrate(
(((f["abr"] ?? f["tbr"] ?? 0) * 1000) as num).toInt(),
),
f["acodec"] ?? "webm",
f["format_note"],
[],
MediaType.parse(
"audio/${f["audio_ext"]}",
),
null,
);
});
return StreamManifest(audioOnlyStreams);
}
Video _parseInfo(Map<String, dynamic> info) {
final publishDate = info["upload_date"] != null
? DateTime.fromMillisecondsSinceEpoch(
int.parse(info["upload_date"]) * 1000,
)
: DateTime.now();
return Video(
VideoId(info["id"]),
info["title"],
info["channel"],
ChannelId(info["channel_id"]),
publishDate,
info["upload_date"] as String? ?? DateTime.now().toString(),
publishDate,
info["description"] ?? "",
Duration(seconds: (info["duration"] as num).toInt()),
ThumbnailSet(info["id"]),
info["tags"]?.cast<String>() ?? <String>[],
Engagement(
info["view_count"],
info["like_count"],
null,
),
info["is_live"] ?? false,
);
}
static bool get isAvailableForPlatform => kIsDesktop;
static Future<bool> isInstalled() async {
return isAvailableForPlatform &&
await YtDlp.instance.checkAvailableInPath();
}
@override
Future<StreamManifest> getStreamManifest(String videoId) async {
final formats = await YtDlp.instance.extractInfo(
"https://www.youtube.com/watch?v=$videoId",
formatSpecifiers: "%(formats)j",
extraArgs: [
"--no-check-certificate",
"--geo-bypass",
"--quiet",
"--ignore-errors"
],
) as List;
return _parseFormats(formats, videoId);
}
@override
Future<Video> getVideo(String videoId) async {
final info = await YtDlp.instance.extractInfo(
"https://www.youtube.com/watch?v=$videoId",
formatSpecifiers: "%()j",
extraArgs: [
"--skip-download",
"--no-check-certificate",
"--geo-bypass",
"--quiet",
"--ignore-errors",
],
) as Map<String, dynamic>;
return _parseInfo(info);
}
@override
Future<(Video, StreamManifest)> getVideoWithStreamInfo(String videoId) async {
final info = await YtDlp.instance.extractInfo(
"https://www.youtube.com/watch?v=$videoId",
formatSpecifiers: "%()j",
extraArgs: [
"--no-check-certificate",
"--geo-bypass",
"--quiet",
"--ignore-errors",
],
) as Map<String, dynamic>;
return (_parseInfo(info), _parseFormats(info["formats"], videoId));
}
@override
Future<List<Video>> searchVideos(String query) async {
final stdout = await YtDlp.instance.extractInfoString(
"ytsearch10:$query",
formatSpecifiers: "%()j",
extraArgs: [
"--skip-download",
"--no-check-certificate",
"--geo-bypass",
"--quiet",
"--ignore-errors",
"--flat-playlist",
"--no-playlist",
],
);
final json = jsonDecode(
"[${stdout.split("\n").where((s) => s.trim().isNotEmpty).join(",")}]",
) as List;
return json.map((e) => _parseInfo(e)).toList();
}
}