11package org .schabi .newpipe .streams ;
22
33import android .util .Log ;
4+ import android .util .Pair ;
45
56import androidx .annotation .NonNull ;
67import androidx .annotation .Nullable ;
78
8- import org .schabi .newpipe .BuildConfig ;
99import org .schabi .newpipe .extractor .stream .StreamInfo ;
1010import org .schabi .newpipe .streams .WebMReader .Cluster ;
1111import org .schabi .newpipe .streams .WebMReader .Segment ;
1717import java .io .IOException ;
1818import java .nio .ByteBuffer ;
1919import java .nio .ByteOrder ;
20- import java .time .OffsetDateTime ;
2120import java .time .format .DateTimeFormatter ;
21+ import java .util .ArrayList ;
22+ import java .util .List ;
2223import java .util .stream .Collectors ;
2324
2425/**
@@ -284,24 +285,22 @@ private byte[] makeMetadata() {
284285 Log .d ("OggFromWebMWriter" , "Downloading media with codec ID " + webmTrack .codecId );
285286
286287 if ("A_OPUS" .equals (webmTrack .codecId )) {
287- var metadata = "" ;
288- metadata += String .format ("COMMENT=Downloaded using NewPipe %s on %s\n " ,
289- BuildConfig .VERSION_NAME ,
290- OffsetDateTime .now ().toString ());
288+ final var metadata = new ArrayList <Pair <String , String >>();
291289 if (streamInfo != null ) {
292- metadata += String .format ("COMMENT=URL: %s\n " , streamInfo .getUrl ());
293- metadata += String .format ("GENRE=%s\n " , streamInfo .getCategory ());
294- metadata += String .format ("ARTIST=%s\n " , streamInfo .getUploaderName ());
295- metadata += String .format ("TITLE=%s\n " , streamInfo .getName ());
296- metadata += String .format ("DATE=%s\n " ,
297- streamInfo
298- .getUploadDate ()
299- .getLocalDateTime ()
300- .format (DateTimeFormatter .ISO_DATE ));
290+ metadata .add (Pair .create ("COMMENT" , streamInfo .getUrl ()));
291+ metadata .add (Pair .create ("GENRE" , streamInfo .getCategory ()));
292+ metadata .add (Pair .create ("ARTIST" , streamInfo .getUploaderName ()));
293+ metadata .add (Pair .create ("TITLE" , streamInfo .getName ()));
294+ metadata .add (Pair .create ("DATE" , streamInfo
295+ .getUploadDate ()
296+ .getLocalDateTime ()
297+ .format (DateTimeFormatter .ISO_DATE )));
301298 }
302299
303300 Log .d ("OggFromWebMWriter" , "Creating metadata header with this data:" );
304- Log .d ("OggFromWebMWriter" , metadata );
301+ metadata .forEach (p -> {
302+ Log .d ("OggFromWebMWriter" , p .first + "=" + p .second );
303+ });
305304
306305 return makeOpusTagsHeader (metadata );
307306 } else if ("A_VORBIS" .equals (webmTrack .codecId )) {
@@ -322,17 +321,13 @@ private byte[] makeMetadata() {
322321 * byte string length field and includes the string as-is. This cannot be used independently,
323322 * but must follow a proper "OpusTags" header.
324323 *
325- * @param keyValue A key-value pair in the format "KEY=some value"
324+ * @param pair A key-value pair in the format "KEY=some value"
326325 * @return The binary data of the encoded metadata tag
327326 */
328- private static byte [] makeOpusMetadataTag (final String keyValue ) {
329- // Ensure the key is uppercase
330- final var delimiterIndex = keyValue .indexOf ('=' );
331- final var key = keyValue .substring (0 , delimiterIndex ).toUpperCase ();
332- final var value = keyValue .substring (delimiterIndex + 1 );
333- final var reconstructedKeyValue = key + "=" + value ;
334-
335- final var bytes = reconstructedKeyValue .getBytes ();
327+ private static byte [] makeOpusMetadataTag (final Pair <String , String > pair ) {
328+ final var keyValue = pair .first .toUpperCase () + "=" + pair .second .trim ();
329+
330+ final var bytes = keyValue .getBytes ();
336331 final var buf = ByteBuffer .allocate (4 + bytes .length );
337332 buf .order (ByteOrder .LITTLE_ENDIAN );
338333 buf .putInt (bytes .length );
@@ -341,20 +336,19 @@ private static byte[] makeOpusMetadataTag(final String keyValue) {
341336 }
342337
343338 /**
344- * This returns a complete "OpusTags" header, created from the provided tags string .
339+ * This returns a complete "OpusTags" header, created from the provided metadata tags .
345340 * <p>
346341 * You probably want to use makeOpusMetadata(), which uses this function to create
347342 * a header with sensible metadata filled in.
348343 *
349- * @param keyValueLines A multiline string with each line containing a key-value pair
350- * in the format "KEY=some value". This may also be a blank string .
344+ * @param keyValueLines A list of pairs of the tags. This can also be though of as a mapping
345+ * from one key to multiple values .
351346 * @return The binary header
352347 */
353- private static byte [] makeOpusTagsHeader (@ NonNull final String keyValueLines ) {
348+ private static byte [] makeOpusTagsHeader (final List < Pair < String , String >> keyValueLines ) {
354349 final var tags = keyValueLines
355- .lines ()
356- .map (String ::trim )
357- .filter (s -> !s .isBlank ())
350+ .stream ()
351+ .filter (p -> !p .second .isBlank ())
358352 .map (OggFromWebMWriter ::makeOpusMetadataTag )
359353 .collect (Collectors .toUnmodifiableList ());
360354
0 commit comments