com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo Java Examples

The following examples show how to use com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AudioLoadResultListener.java    From Shadbot with GNU General Public License v3.0 6 votes vote down vote up
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)));
}
 
Example #2
Source File: YoutubeUtils.java    From SkyBot with GNU Affero General Public License v3.0 6 votes vote down vote up
@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()
    );
}
 
Example #3
Source File: TrackStartRequestCodec.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #4
Source File: MatroskaContainerProbe.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #5
Source File: FormatUtilsTest.java    From Shadbot with GNU General Public License v3.0 6 votes vote down vote up
@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")));
}
 
Example #6
Source File: YoutubeSearchProvider.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
/**
 * @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);
  }
}
 
Example #7
Source File: DefaultAudioPlayerManager.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #8
Source File: FormatUtils.java    From Shadbot with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @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();
}
 
Example #9
Source File: WavContainerProbe.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
@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
  ));
}
 
Example #10
Source File: YoutubeApiPlaylistLoader.java    From SkyBot with GNU Affero General Public License v3.0 6 votes vote down vote up
@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);
    }
}
 
Example #11
Source File: GetyarnAudioSourceManager.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
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);
  }
}
 
Example #12
Source File: FlacContainerProbe.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #13
Source File: HttpAudioTrack.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
/**
 * @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;
}
 
Example #14
Source File: SafeHttpAudioSourceManager.java    From kyoko with MIT License 5 votes vote down vote up
@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;
}
 
Example #15
Source File: AudioLoaderRestHandler.java    From Lavalink with MIT License 5 votes vote down vote up
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());
}
 
Example #16
Source File: DefaultSoundCloudPlaylistLoader.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
@Override
public AudioPlaylist load(
    String identifier,
    HttpInterfaceManager httpInterfaceManager,
    Function<AudioTrackInfo, AudioTrack> trackFactory
) {
  String url = SoundCloudHelper.nonMobileUrl(identifier);

  if (playlistUrlPattern.matcher(url).matches()) {
    return loadFromSet(httpInterfaceManager, url, trackFactory);
  } else {
    return null;
  }
}
 
Example #17
Source File: YoutubeApiPlaylistLoader.java    From SkyBot with GNU Affero General Public License v3.0 5 votes vote down vote up
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();
}
 
Example #18
Source File: AudioUtils.java    From JuniperBot with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example #19
Source File: MediaContainerDetectionResult.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #20
Source File: NameCmd.java    From Shadbot with GNU General Public License v3.0 5 votes vote down vote up
@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();
}
 
Example #21
Source File: TrackData.java    From JuniperBot with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example #22
Source File: HttpAudioSourceManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #23
Source File: TwitchStreamAudioTrack.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
/**
 * @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);
}
 
Example #24
Source File: YoutubeSearchProvider.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example #25
Source File: DefaultYoutubePlaylistLoader.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #26
Source File: DefaultAudioPlayerManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #27
Source File: AudioTrackInfoBuilder.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
/**
 * @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
  );
}
 
Example #28
Source File: NicoAudioSourceManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #29
Source File: LocalAudioSourceManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #30
Source File: BandcampAudioSourceManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
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);
}