Java源码示例:com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo
示例1
private AudioTrack extractVideoUrlFromPage(AudioReference reference) {
try (final CloseableHttpResponse response = getHttpInterface().execute(new HttpGet(reference.identifier))) {
final String html = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
final Document document = Jsoup.parse(html);
final AudioTrackInfo trackInfo = AudioTrackInfoBuilder.empty()
.setUri(reference.identifier)
.setAuthor("Unknown")
.setIsStream(false)
.setIdentifier(document.selectFirst("meta[property=og:video:secure_url]").attr("content"))
.setTitle(document.selectFirst("meta[property=og:title]").attr("content"))
.build();
return createTrack(trackInfo);
} catch (IOException e) {
throw new FriendlyException("Failed to load info for yarn clip", SUSPICIOUS, null);
}
}
示例2
private Mono<Consumer<EmbedCreateSpec>> getPlaylistEmbed(AudioPlaylist playlist, String avatarUrl) {
final String choices = FormatUtils.numberedList(Config.MUSIC_SEARCHES, playlist.getTracks().size(),
count -> {
final AudioTrackInfo info = playlist.getTracks().get(count - 1).getInfo();
return String.format("\t**%d.** [%s](%s)", count, FormatUtils.trackName(info), info.uri);
});
final String playlistName = StringUtils.abbreviate(playlist.getName(), MAX_PLAYLIST_NAME_LENGTH);
return DatabaseManager.getGuilds()
.getDBGuild(this.guildId)
.map(DBGuild::getSettings)
.map(Settings::getPrefix)
.map(prefix -> ShadbotUtils.getDefaultEmbed()
.andThen(embed -> embed.setAuthor(playlistName, null, avatarUrl)
.setThumbnail("https://i.imgur.com/IG3Hj2W.png")
.setDescription("**Select a music by typing the corresponding number.**"
+ "\nYou can choose several musics by separating them with a comma."
+ "\nExample: 1,3,4"
+ "\n\n" + choices)
.setFooter(String.format("Use %scancel to cancel the selection (Automatically " +
"canceled in %ds).", prefix, Config.MUSIC_CHOICE_DURATION), null)));
}
示例3
/**
* @param info The {@link AudioTrackInfo} to format.
* @return A string representing the provided info formatted.
*/
public static String trackName(AudioTrackInfo info) {
final StringBuilder strBuilder = new StringBuilder();
if ("Unknown artist".equals(info.author) || info.title.startsWith(info.author)) {
strBuilder.append(info.title.trim());
} else {
strBuilder.append(String.format("%s - %s", info.author.trim(), info.title.trim()));
}
if (info.isStream) {
strBuilder.append(" (Stream)");
} else {
strBuilder.append(String.format(" (%s)", FormatUtils.formatDuration(info.length)));
}
return strBuilder.toString();
}
示例4
@Test
public void testTrackName() {
assertEquals("author - title (1:00)", FormatUtils.trackName(
new AudioTrackInfo("title", "author", 60 * 1000,
"identifier", false, "uri")));
assertEquals("title (1:00)", FormatUtils.trackName(
new AudioTrackInfo("title", "Unknown artist", 60 * 1000,
"identifier", false, "uri")));
assertEquals("author - title (Stream)", FormatUtils.trackName(
new AudioTrackInfo("title", "author", 60 * 1000,
"identifier", true, "uri")));
assertEquals("title (Stream)", FormatUtils.trackName(
new AudioTrackInfo("title", "Unknown artist", 60 * 1000,
"identifier", true, "uri")));
assertEquals("author - title (Stream)", FormatUtils.trackName(
new AudioTrackInfo(" title ", " author ", 60 * 1000,
"identifier", true, "uri")));
assertEquals("author - title (1:00)", FormatUtils.trackName(
new AudioTrackInfo("author - title", "author", 60 * 1000,
"identifier", false, "uri")));
}
示例5
@Nullable
public static AudioTrackInfo videoToTrackInfo(@Nullable Video video) {
if (video == null) {
return null;
}
final VideoSnippet snippet = video.getSnippet();
final VideoContentDetails details = video.getContentDetails();
return new AudioTrackInfo(
snippet.getTitle(),
snippet.getChannelTitle(),
Duration.parse(details.getDuration()).toMillis(),
video.getId(),
false,
"https://www.youtube.com/watch?v=" + video.getId()
);
}
示例6
@Override
public TrackStartRequestMessage decode(DataInput in, int version) throws IOException {
long executorId = in.readLong();
AudioTrackInfo trackInfo = new AudioTrackInfo(in.readUTF(), in.readUTF(), in.readLong(), in.readUTF(), in.readBoolean(), null);
byte[] encodedTrack = new byte[in.readInt()];
in.readFully(encodedTrack);
int volume = in.readInt();
AudioConfiguration configuration = new AudioConfiguration();
configuration.setResamplingQuality(AudioConfiguration.ResamplingQuality.valueOf(in.readUTF()));
configuration.setOpusEncodingQuality(in.readInt());
if (version >= VERSION_WITH_FORMAT) {
AudioDataFormat format = createFormat(in.readInt(), in.readInt(), in.readInt(), in.readUTF());
configuration.setOutputFormat(format);
}
long position = 0;
if (version >= VERSION_WITH_POSITION) {
position = in.readLong();
}
return new TrackStartRequestMessage(executorId, trackInfo, encodedTrack, volume, configuration, position);
}
示例7
@Override
public AudioPlaylist load(HttpInterface httpInterface, String playlistId, String selectedVideoId, Function<AudioTrackInfo, AudioTrack> trackFactory) {
try {
final YoutubePlaylistMetadata firstPage = getPlaylistPageById(playlistId, this.apiKey, null, true);
if (firstPage == null) {
throw new FriendlyException("This playlist does not exist", COMMON, null);
}
return buildPlaylist(firstPage, playlistId, selectedVideoId, trackFactory);
}
catch (IOException e) {
Sentry.capture(e);
throw ExceptionTools.wrapUnfriendlyExceptions(e);
}
}
示例8
@Override
public MediaContainerDetectionResult probe(AudioReference reference, SeekableInputStream inputStream) throws IOException {
if (!checkNextBytes(inputStream, WAV_RIFF_HEADER)) {
return null;
}
log.debug("Track {} is a WAV file.", reference.identifier);
WavFileInfo fileInfo = new WavFileLoader(inputStream).parseHeaders();
return supportedFormat(this, null, new AudioTrackInfo(
defaultOnNull(reference.title, UNKNOWN_TITLE),
UNKNOWN_ARTIST,
fileInfo.getDuration(),
reference.identifier,
false,
reference.identifier
));
}
示例9
@Override
public MediaContainerDetectionResult probe(AudioReference reference, SeekableInputStream inputStream) throws IOException {
if (!checkNextBytes(inputStream, EBML_TAG)) {
return null;
}
log.debug("Track {} is a matroska file.", reference.identifier);
MatroskaStreamingFile file = new MatroskaStreamingFile(inputStream);
file.readFile();
if (!hasSupportedAudioTrack(file)) {
return unsupportedFormat(this, "No supported audio tracks present in the file.");
}
return supportedFormat(this, null, new AudioTrackInfo(UNKNOWN_TITLE, UNKNOWN_ARTIST,
(long) file.getDuration(), reference.identifier, false, reference.identifier));
}
示例10
@Override
public void encodeTrack(MessageOutput stream, AudioTrack track) throws IOException {
DataOutput output = stream.startMessage();
output.write(TRACK_INFO_VERSION);
AudioTrackInfo trackInfo = track.getInfo();
output.writeUTF(trackInfo.title);
output.writeUTF(trackInfo.author);
output.writeLong(trackInfo.length);
output.writeUTF(trackInfo.identifier);
output.writeBoolean(trackInfo.isStream);
DataFormatTools.writeNullableText(output, trackInfo.uri);
encodeTrackDetails(track, output);
output.writeLong(track.getPosition());
stream.commitMessage(TRACK_INFO_VERSIONED);
}
示例11
@Override
public MediaContainerDetectionResult probe(AudioReference reference, SeekableInputStream inputStream) throws IOException {
if (!checkNextBytes(inputStream, FlacFileLoader.FLAC_CC)) {
return null;
}
log.debug("Track {} is a FLAC file.", reference.identifier);
FlacTrackInfo fileInfo = new FlacFileLoader(inputStream).parseHeaders();
AudioTrackInfo trackInfo = AudioTrackInfoBuilder.create(reference, inputStream)
.setTitle(fileInfo.tags.get(TITLE_TAG))
.setAuthor(fileInfo.tags.get(ARTIST_TAG))
.setLength(fileInfo.duration)
.build();
return supportedFormat(this, null, trackInfo);
}
示例12
/**
* @param query Search query.
* @return Playlist of the first page of results.
*/
@Override
public AudioItem loadSearchResult(String query, Function<AudioTrackInfo, AudioTrack> trackFactory) {
log.debug("Performing a search with query {}", query);
try (HttpInterface httpInterface = httpInterfaceManager.getInterface()) {
URI url = new URIBuilder("https://www.youtube.com/results").addParameter("search_query", query).build();
try (CloseableHttpResponse response = httpInterface.execute(new HttpGet(url))) {
int statusCode = response.getStatusLine().getStatusCode();
if (!HttpClientTools.isSuccessWithContent(statusCode)) {
throw new IOException("Invalid status code for search response: " + statusCode);
}
Document document = Jsoup.parse(response.getEntity().getContent(), StandardCharsets.UTF_8.name(), "");
return extractSearchResults(document, query, trackFactory);
}
} catch (Exception e) {
throw ExceptionTools.wrapUnfriendlyExceptions(e);
}
}
示例13
private MediaContainerDetectionResult(AudioTrackInfo trackInfo, MediaContainerProbe containerProbe,
String probeSettings, AudioReference reference, String unsupportedReason) {
this.trackInfo = trackInfo;
this.containerProbe = containerProbe;
this.probeSettings = probeSettings;
this.reference = reference;
this.unsupportedReason = unsupportedReason;
}
示例14
@Override
public AudioTrack decodeTrack(AudioTrackInfo trackInfo, DataInput input) throws IOException {
MediaContainerDescriptor containerTrackFactory = decodeTrackFactory(input);
if (containerTrackFactory != null) {
return new HttpAudioTrack(trackInfo, containerTrackFactory, this);
}
return null;
}
示例15
@Override
public Mono<Void> execute(Context context) {
final AudioTrackInfo trackInfo = context.requireGuildMusic()
.getTrackScheduler()
.getAudioPlayer()
.getPlayingTrack()
.getInfo();
return context.getChannel()
.flatMap(channel -> DiscordUtils.sendMessage(
String.format(Emoji.MUSICAL_NOTE + " (**%s**) Currently playing: **%s**",
context.getUsername(), FormatUtils.trackName(trackInfo)), channel))
.then();
}
示例16
private JSONObject trackToJSON(AudioTrack audioTrack) {
AudioTrackInfo trackInfo = audioTrack.getInfo();
return new JSONObject()
.put("title", trackInfo.title)
.put("author", trackInfo.author)
.put("length", trackInfo.length)
.put("identifier", trackInfo.identifier)
.put("uri", trackInfo.uri)
.put("isStream", trackInfo.isStream)
.put("isSeekable", audioTrack.isSeekable())
.put("position", audioTrack.getPosition());
}
示例17
@Override
public MediaContainerDetectionResult probe(AudioReference reference, SeekableInputStream inputStream) throws IOException {
if (!checkNextBytes(inputStream, ISO_TAG)) {
return null;
}
log.debug("Track {} is an MP4 file.", reference.identifier);
MpegFileLoader file = new MpegFileLoader(inputStream);
file.parseHeaders();
MpegTrackInfo audioTrack = getSupportedAudioTrack(file);
if (audioTrack == null) {
return unsupportedFormat(this, "No supported audio format in the MP4 file.");
}
MpegTrackConsumer trackConsumer = new MpegNoopTrackConsumer(audioTrack);
MpegFileTrackProvider fileReader = file.loadReader(trackConsumer);
if (fileReader == null) {
return unsupportedFormat(this, "MP4 file uses an unsupported format.");
}
AudioTrackInfo trackInfo = AudioTrackInfoBuilder.create(reference, inputStream)
.setTitle(file.getTextMetadata("Title"))
.setAuthor(file.getTextMetadata("Artist"))
.setLength(fileReader.getDuration())
.build();
return supportedFormat(this, null, trackInfo);
}
示例18
private String fetchNextPage(String nextPageKey, String playlistId, Function<AudioTrackInfo, AudioTrack> trackFactory,
List<AudioTrack> tracks) throws IOException {
final YoutubePlaylistMetadata nextPage = getPlaylistPageById(playlistId, this.apiKey, nextPageKey, false);
if (nextPage == null) {
return null;
}
nextPage.getTracks()
.stream()
.map(trackFactory)
.forEach(tracks::add);
return nextPage.getNextPageKey();
}
示例19
public static PlaylistItem findPlaylist(Playlist playlist, AudioTrackInfo info) {
info = normalizeInfo(info);
for (PlaylistItem item : playlist.getItems()) {
if (item != null &&
Objects.equals(item.getTitle(), info.title) &&
Objects.equals(item.getAuthor(), info.author) &&
Objects.equals(item.getLength(), info.length) &&
Objects.equals(item.getIdentifier(), info.identifier) &&
Objects.equals(item.getUri(), info.uri)) {
return item;
}
}
return null;
}
示例20
public static String getArtwork(AudioTrack track) {
AudioTrackInfo info = track.getInfo();
if (StringUtils.isNotBlank(info.getArtworkUrl())) {
return info.getArtworkUrl();
}
TrackData trackData = get(track);
if (trackData.getPlaylistItem() != null && StringUtils.isNotBlank(trackData.getPlaylistItem().getArtworkUri())) {
return trackData.getPlaylistItem().getArtworkUri();
}
return null;
}
示例21
private AudioItem extractSearchResults(Document document, String query,
Function<AudioTrackInfo, AudioTrack> trackFactory) {
List<AudioTrack> tracks = new ArrayList<>();
Elements resultsSelection = document.select("#page > #content #results");
if (!resultsSelection.isEmpty()) {
for (Element results : resultsSelection) {
for (Element result : results.select(".yt-lockup-video")) {
if (!result.hasAttr("data-ad-impressions") && result.select(".standalone-ypc-badge-renderer-label").isEmpty()) {
extractTrackFromResultEntry(tracks, result, trackFactory);
}
}
}
} else {
log.debug("Attempting to parse results page as polymer");
try {
tracks = polymerExtractTracks(document, trackFactory);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (tracks.isEmpty()) {
return AudioReference.NO_TRACK;
} else {
return new BasicAudioPlaylist("Search results for: " + query, tracks, null, true);
}
}
示例22
private String extractPlaylistTracks(JsonBrowser playlistVideoList, List<AudioTrack> tracks,
Function<AudioTrackInfo, AudioTrack> trackFactory) {
JsonBrowser trackArray = playlistVideoList.get("contents");
if (trackArray.isNull()) return null;
for (JsonBrowser track : trackArray.values()) {
JsonBrowser item = track.get("playlistVideoRenderer");
JsonBrowser shortBylineText = item.get("shortBylineText");
// If the isPlayable property does not exist, it means the video is removed or private
// If the shortBylineText property does not exist, it means the Track is Region blocked
if (!item.get("isPlayable").isNull() && !shortBylineText.isNull()) {
String videoId = item.get("videoId").text();
String title = item.get("title").get("simpleText").text();
String author = shortBylineText.get("runs").index(0).get("text").text();
JsonBrowser lengthSeconds = item.get("lengthSeconds");
long duration = Units.secondsToMillis(lengthSeconds.asLong(Units.DURATION_SEC_UNKNOWN));
AudioTrackInfo info = new AudioTrackInfo(title, author, duration, videoId, false,
"https://www.youtube.com/watch?v=" + videoId);
tracks.add(trackFactory.apply(info));
}
}
JsonBrowser continuations = playlistVideoList.get("continuations");
if (!continuations.isNull()) {
String continuationsToken = continuations.index(0).get("nextContinuationData").get("continuation").text();
return "/browse_ajax?continuation=" + continuationsToken + "&ctoken=" + continuationsToken + "&hl=en";
}
return null;
}
示例23
private AudioTrack decodeTrackDetails(AudioTrackInfo trackInfo, DataInput input) throws IOException {
String sourceName = input.readUTF();
for (AudioSourceManager sourceManager : sourceManagers) {
if (sourceName.equals(sourceManager.getSourceName())) {
return sourceManager.decodeTrack(trackInfo, input);
}
}
return null;
}
示例24
/**
* @return Audio track info instance.
*/
public AudioTrackInfo build() {
long finalLength = DataFormatTools.defaultOnNull(length, DURATION_MS_UNKNOWN);
return new AudioTrackInfo(
title,
author,
finalLength,
identifier,
DataFormatTools.defaultOnNull(isStream, finalLength == DURATION_MS_UNKNOWN),
uri
);
}
示例25
@Override
public AudioTrack decodeTrack(AudioTrackInfo trackInfo, DataInput input) throws IOException {
MediaContainerDescriptor containerTrackFactory = decodeTrackFactory(input);
if (containerTrackFactory != null) {
return new LocalAudioTrack(trackInfo, containerTrackFactory, this);
}
return null;
}
示例26
/**
* @param trackInfo Track info
* @param sourceManager Source manager which was used to find this track
*/
public TwitchStreamAudioTrack(AudioTrackInfo trackInfo, TwitchStreamAudioSourceManager sourceManager) {
super(trackInfo);
this.sourceManager = sourceManager;
this.segmentUrlProvider = new TwitchStreamSegmentUrlProvider(getChannelName(), sourceManager);
}
示例27
private AudioTrack extractTrack(JsonBrowser trackInfo, String bandUrl, String artist) {
String trackPageUrl = bandUrl + trackInfo.get("title_link").text();
return new BandcampAudioTrack(new AudioTrackInfo(
trackInfo.get("title").text(),
artist,
(long) (trackInfo.get("duration").as(Double.class) * 1000.0),
bandUrl + trackInfo.get("title_link").text(),
false,
trackPageUrl
), this);
}
示例28
private AudioTrack extractTrackFromXml(String videoId, Document document) {
for (Element element : document.select(":root > thumb")) {
String uploader = element.select("user_nickname").first().text();
String title = element.select("title").first().text();
long duration = DataFormatTools.durationTextToMillis(element.select("length").first().text());
return new NicoAudioTrack(new AudioTrackInfo(title, uploader, duration, videoId, false, getWatchUrl(videoId)), this);
}
return null;
}
示例29
/**
* @param trackInfo Track info
* @param containerTrackFactory Container track factory - contains the probe with its parameters.
* @param sourceManager Source manager used to load this track
*/
public HttpAudioTrack(AudioTrackInfo trackInfo, MediaContainerDescriptor containerTrackFactory,
HttpAudioSourceManager sourceManager) {
super(trackInfo);
this.containerTrackFactory = containerTrackFactory;
this.sourceManager = sourceManager;
}
示例30
@Override
public AudioTrack decodeTrack(AudioTrackInfo trackInfo, DataInput input) throws IOException {
MediaContainerDescriptor containerTrackFactory = decodeTrackFactory(input);
if (containerTrackFactory != null) {
return new HttpAudioTrack(trackInfo, containerTrackFactory, this);
}
return null;
}