Skip to content

Commit 5d112aa

Browse files
authored
Merge pull request #1074 from TeamNewPipe/imp/media-formats
Add more audio media formats and MediaFormat.getAllForMimeType(mimeType)
2 parents 5eb8862 + 5809904 commit 5d112aa

1 file changed

Lines changed: 47 additions & 2 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/MediaFormat.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@
2222
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
2323
*/
2424

25+
import javax.annotation.Nonnull;
26+
import javax.annotation.Nullable;
2527
import java.util.Arrays;
28+
import java.util.List;
2629
import java.util.function.Function;
30+
import java.util.stream.Collectors;
2731

2832
/**
2933
* Static data about various media formats support by NewPipe, eg mime type, extension
@@ -41,9 +45,18 @@ public enum MediaFormat {
4145
M4A (0x100, "m4a", "m4a", "audio/mp4"),
4246
WEBMA (0x200, "WebM", "webm", "audio/webm"),
4347
MP3 (0x300, "MP3", "mp3", "audio/mpeg"),
48+
MP2 (0x310, "MP2", "mp2", "audio/mpeg"),
4449
OPUS (0x400, "opus", "opus", "audio/opus"),
4550
OGG (0x500, "ogg", "ogg", "audio/ogg"),
4651
WEBMA_OPUS(0x200, "WebM Opus", "webm", "audio/webm"),
52+
AIFF (0x600, "AIFF", "aiff", "audio/aiff"),
53+
/**
54+
* Same as {@link MediaFormat.AIFF}, just with the shorter suffix/file extension
55+
*/
56+
AIF (0x600, "AIFF", "aif", "audio/aiff"),
57+
WAV (0x700, "WAV", "wav", "audio/wav"),
58+
FLAC (0x800, "FLAC", "flac", "audio/flac"),
59+
ALAC (0x900, "ALAC", "alac", "audio/alac"),
4760
// subtitles formats
4861
VTT (0x1000, "WebVTT", "vtt", "text/vtt"),
4962
TTML (0x2000, "Timed Text Markup Language", "ttml", "application/ttml+xml"),
@@ -54,11 +67,15 @@ public enum MediaFormat {
5467
// @formatter:on
5568

5669
public final int id;
70+
@Nonnull
5771
public final String name;
72+
@Nonnull
5873
public final String suffix;
74+
@Nonnull
5975
public final String mimeType;
6076

61-
MediaFormat(final int id, final String name, final String suffix, final String mimeType) {
77+
MediaFormat(final int id, @Nonnull final String name,
78+
@Nonnull final String suffix, @Nonnull final String mimeType) {
6279
this.id = id;
6380
this.name = name;
6481
this.suffix = suffix;
@@ -82,6 +99,7 @@ private static <T> T getById(final int id,
8299
* @return the friendly name of the MediaFormat associated with this ids,
83100
* or an empty String if none match it.
84101
*/
102+
@Nonnull
85103
public static String getNameById(final int id) {
86104
return getById(id, MediaFormat::getName, "");
87105
}
@@ -93,6 +111,7 @@ public static String getNameById(final int id) {
93111
* @return the file extension of the MediaFormat associated with this ids,
94112
* or an empty String if none match it.
95113
*/
114+
@Nonnull
96115
public static String getSuffixById(final int id) {
97116
return getById(id, MediaFormat::getSuffix, "");
98117
}
@@ -104,33 +123,56 @@ public static String getSuffixById(final int id) {
104123
* @return the MIME type of the MediaFormat associated with this ids,
105124
* or an empty String if none match it.
106125
*/
126+
@Nullable
107127
public static String getMimeById(final int id) {
108128
return getById(id, MediaFormat::getMimeType, null);
109129
}
110130

111131
/**
112-
* Return the MediaFormat with the supplied mime type
132+
* Return the first {@link MediaFormat} with the supplied mime type.
133+
* There might be more formats which have the same mime type.
134+
* To retrieve those, use {@link #getAllFromMimeType(String)}.
113135
*
114136
* @return MediaFormat associated with this mime type,
115137
* or null if none match it.
116138
*/
139+
@Nullable
117140
public static MediaFormat getFromMimeType(final String mimeType) {
118141
return Arrays.stream(MediaFormat.values())
119142
.filter(mediaFormat -> mediaFormat.mimeType.equals(mimeType))
120143
.findFirst()
121144
.orElse(null);
122145
}
123146

147+
/**
148+
* Get all media formats which have the given mime type.
149+
* @param mimeType the mime type to search for
150+
* @return a modifiable {@link List} which contains the {@link MediaFormat}s
151+
* that have the given mime type.
152+
*/
153+
@Nonnull
154+
public static List<MediaFormat> getAllFromMimeType(final String mimeType) {
155+
return Arrays.stream(MediaFormat.values())
156+
.filter(mediaFormat -> mediaFormat.mimeType.equals(mimeType))
157+
.collect(Collectors.toList());
158+
}
159+
124160
/**
125161
* Get the media format by its id.
126162
*
127163
* @param id the id
128164
* @return the id of the media format or null.
129165
*/
166+
@Nullable
130167
public static MediaFormat getFormatById(final int id) {
131168
return getById(id, mediaFormat -> mediaFormat, null);
132169
}
133170

171+
/**
172+
* Get the first media format that has the given suffix/file extension.
173+
* @return the matching {@link MediaFormat} or {@code null} if no associated format is found
174+
*/
175+
@Nullable
134176
public static MediaFormat getFromSuffix(final String suffix) {
135177
return Arrays.stream(MediaFormat.values())
136178
.filter(mediaFormat -> mediaFormat.suffix.equals(suffix))
@@ -143,6 +185,7 @@ public static MediaFormat getFromSuffix(final String suffix) {
143185
*
144186
* @return the name of the format
145187
*/
188+
@Nonnull
146189
public String getName() {
147190
return name;
148191
}
@@ -152,6 +195,7 @@ public String getName() {
152195
*
153196
* @return the filename extension
154197
*/
198+
@Nonnull
155199
public String getSuffix() {
156200
return suffix;
157201
}
@@ -161,6 +205,7 @@ public String getSuffix() {
161205
*
162206
* @return the mime type
163207
*/
208+
@Nonnull
164209
public String getMimeType() {
165210
return mimeType;
166211
}

0 commit comments

Comments
 (0)