Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8e59944

Browse files
authored
Input/output abstractions (deepmedia#13)
* Rename OutputStrategy to TrackStrategy * Rename internal validator to Checks * Small changes * Create DataSink to replace muxer * Add comments * Create TrackTypeMap * Abstract MediaExtractor into DataSource * Cleanup * Rename TranscoderEngine * Change packages * Simplify logic * Fix build
1 parent 0e9e9e9 commit 8e59944

35 files changed

+764
-648
lines changed

README.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Take a look at the demo app for a real example or keep reading below for documen
5050
- Override frames timestamp, e.g. to slow down the middle part of the video [[docs]](#time-interpolation)
5151
- Error handling [[docs]](#listening-for-events)
5252
- Configurable validators to e.g. avoid transcoding if the source is already compressed enough [[docs]](#validators)
53-
- Configurable video and audio strategies [[docs]](#output-strategies)
53+
- Configurable video and audio strategies [[docs]](#track-strategies)
5454

5555
*This project started as a fork of [ypresto/android-transcoder](https://github.com/ypresto/android-transcoder).
5656
With respect to the source project, which misses most of the functionality listed above,
@@ -94,7 +94,7 @@ simply `setDataSource(path)` in the transcoding builder.
9494

9595
## Listening for events
9696

97-
Transcoding will happen on a background thread, but we will send updates through the `MediaTranscoder.Listener`
97+
Transcoding will happen on a background thread, but we will send updates through the `TranscoderListener`
9898
interface, which can be applied when building the request:
9999

100100
```java
@@ -174,7 +174,7 @@ The TrackStatus enum contains the following values:
174174
|`TrackStatus.COMPRESSING`|This track is about to be processed and compressed in the target file.|
175175
|`TrackStatus.REMOVING`|This track will be removed in the target file.|
176176

177-
The `TrackStatus` value depends on the [output strategy](#output-strategies) that was used.
177+
The `TrackStatus` value depends on the [track strategy](#track-strategies) that was used.
178178
We provide a few validators that can be injected for typical usage.
179179

180180
#### `DefaultValidator`
@@ -193,19 +193,19 @@ A Validator that gives priority to the video track. Transcoding will not happen
193193
even if the audio track might need it. If reducing file size is your only concern, this can avoid compressing
194194
files that would not benefit so much from compressing the audio track only.
195195

196-
## Output Strategies
196+
## Track Strategies
197197

198-
Output strategies return options for each track (audio or video) for the engine to understand **how**
198+
Track strategies return options for each track (audio or video) for the engine to understand **how**
199199
and **if** this track should be transcoded, and whether the whole process should be aborted.
200200

201201
```java
202202
Transcoder.into(filePath)
203-
.setVideoOutputStrategy(videoStrategy)
204-
.setAudioOutputStrategy(audioStrategy)
203+
.setVideoTrackStrategy(videoStrategy)
204+
.setAudioTrackStrategy(audioStrategy)
205205
// ...
206206
```
207207

208-
The point of `OutputStrategy` is to inspect the input `android.media.MediaFormat` and return
208+
The point of `TrackStrategy` is to inspect the input `android.media.MediaFormat` and return
209209
the output `android.media.MediaFormat`, filled with required options.
210210

211211
This library offers track specific strategies that help with audio and video options (see
@@ -214,14 +214,14 @@ In addition, we have a few built-in strategies that can work for both audio and
214214

215215
#### `PassThroughTrackStrategy`
216216

217-
An OutputStrategy that asks the encoder to keep this track as is, by returning the same input
217+
A TrackStrategy that asks the encoder to keep this track as is, by returning the same input
218218
format. Note that this is risky, as the input track format might not be supported my the MP4 container.
219219

220220
This will set the `TrackStatus` to `TrackStatus.PASS_THROUGH`.
221221

222222
#### `RemoveTrackStrategy`
223223

224-
An OutputStrategy that asks the encoder to remove this track from the output container, by returning null.
224+
A TrackStrategy that asks the encoder to remove this track from the output container, by returning null.
225225
For instance, this can be used as an audio strategy to remove audio from video/audio streams.
226226

227227
This will set the `TrackStatus` to `TrackStatus.REMOVING`.
@@ -233,9 +233,9 @@ audio stream to AAC format with the specified number of channels.
233233

234234
```java
235235
Transcoder.into(filePath)
236-
.setAudioOutputStrategy(new DefaultAudioStrategy(1)) // or..
237-
.setAudioOutputStrategy(new DefaultAudioStrategy(2)) // or..
238-
.setAudioOutputStrategy(new DefaultAudioStrategy(DefaultAudioStrategy.AUDIO_CHANNELS_AS_IS))
236+
.setAudioTrackStrategy(new DefaultAudioStrategy(1)) // or..
237+
.setAudioTrackStrategy(new DefaultAudioStrategy(2)) // or..
238+
.setAudioTrackStrategy(new DefaultAudioStrategy(DefaultAudioStrategy.AUDIO_CHANNELS_AS_IS))
239239
// ...
240240
```
241241

@@ -406,8 +406,8 @@ We collect common presets in the `DefaultVideoStrategies` class:
406406

407407
```java
408408
Transcoder.into(filePath)
409-
.setVideoOutputStrategy(DefaultVideoStrategies.for720x1280()) // 16:9
410-
.setVideoOutputStrategy(DefaultVideoStrategies.for360x480()) // 4:3
409+
.setVideoTrackStrategy(DefaultVideoStrategies.for720x1280()) // 16:9
410+
.setVideoTrackStrategy(DefaultVideoStrategies.for360x480()) // 4:3
411411
// ...
412412
```
413413

demo/src/main/java/com/otaliastudios/transcoder/demo/TranscoderActivity.java

+5-8
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,13 @@
1111

1212
import com.otaliastudios.transcoder.Transcoder;
1313
import com.otaliastudios.transcoder.TranscoderListener;
14-
import com.otaliastudios.transcoder.engine.TrackStatus;
1514
import com.otaliastudios.transcoder.internal.Logger;
1615
import com.otaliastudios.transcoder.strategy.DefaultAudioStrategy;
1716
import com.otaliastudios.transcoder.strategy.DefaultVideoStrategy;
18-
import com.otaliastudios.transcoder.strategy.OutputStrategy;
19-
import com.otaliastudios.transcoder.strategy.RemoveTrackStrategy;
17+
import com.otaliastudios.transcoder.strategy.TrackStrategy;
2018
import com.otaliastudios.transcoder.strategy.size.AspectRatioResizer;
2119
import com.otaliastudios.transcoder.strategy.size.FractionResizer;
2220
import com.otaliastudios.transcoder.strategy.size.PassThroughResizer;
23-
import com.otaliastudios.transcoder.validator.DefaultValidator;
2421

2522
import java.io.File;
2623
import java.io.IOException;
@@ -57,8 +54,8 @@ public class TranscoderActivity extends AppCompatActivity implements
5754
private Uri mTranscodeInputUri;
5855
private File mTranscodeOutputFile;
5956
private long mTranscodeStartTime;
60-
private OutputStrategy mTranscodeVideoStrategy;
61-
private OutputStrategy mTranscodeAudioStrategy;
57+
private TrackStrategy mTranscodeVideoStrategy;
58+
private TrackStrategy mTranscodeAudioStrategy;
6259

6360
@Override
6461
protected void onCreate(Bundle savedInstanceState) {
@@ -186,8 +183,8 @@ private void transcode() {
186183
mTranscodeFuture = Transcoder.into(mTranscodeOutputFile.getAbsolutePath())
187184
.setDataSource(this, mTranscodeInputUri)
188185
.setListener(this)
189-
.setAudioOutputStrategy(mTranscodeAudioStrategy)
190-
.setVideoOutputStrategy(mTranscodeVideoStrategy)
186+
.setAudioTrackStrategy(mTranscodeAudioStrategy)
187+
.setVideoTrackStrategy(mTranscodeVideoStrategy)
191188
.setRotation(rotation)
192189
.setSpeed(speed)
193190
.transcode();

lib/src/androidTest/java/com/otaliastudios/transcoder/engine/internal/ISO6709LocationParserTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import androidx.test.ext.junit.runners.AndroidJUnit4;
44
import androidx.test.filters.SmallTest;
55

6-
import com.otaliastudios.transcoder.engine.internal.ISO6709LocationParser;
6+
import com.otaliastudios.transcoder.internal.ISO6709LocationParser;
77

88
import org.junit.Test;
99
import org.junit.runner.RunWith;

lib/src/main/java/com/otaliastudios/transcoder/Transcoder.java

+3-12
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717

1818
import android.os.Handler;
1919

20-
import com.otaliastudios.transcoder.engine.TranscoderEngine;
20+
import com.otaliastudios.transcoder.engine.Engine;
2121
import com.otaliastudios.transcoder.source.DataSource;
2222
import com.otaliastudios.transcoder.internal.Logger;
2323
import com.otaliastudios.transcoder.validator.Validator;
24-
import com.otaliastudios.transcoder.engine.ValidatorException;
24+
import com.otaliastudios.transcoder.internal.ValidatorException;
2525

26-
import java.io.IOException;
2726
import java.util.concurrent.Callable;
2827
import java.util.concurrent.Future;
2928
import java.util.concurrent.LinkedBlockingQueue;
@@ -113,14 +112,12 @@ public Future<Void> transcode(@NonNull final TranscoderOptions options) {
113112
@Override
114113
public Void call() throws Exception {
115114
try {
116-
TranscoderEngine engine = new TranscoderEngine();
117-
engine.setProgressCallback(new TranscoderEngine.ProgressCallback() {
115+
Engine engine = new Engine(options.getDataSource(), new Engine.ProgressCallback() {
118116
@Override
119117
public void onProgress(final double progress) {
120118
listenerWrapper.onTranscodeProgress(progress);
121119
}
122120
});
123-
engine.setDataSource(options.getDataSource());
124121
engine.transcode(options);
125122
listenerWrapper.onTranscodeCompleted(SUCCESS_TRANSCODED);
126123

@@ -140,12 +137,6 @@ public void onProgress(final double progress) {
140137
LOG.i("Transcode canceled.", current);
141138
listenerWrapper.onTranscodeCanceled();
142139

143-
} else if (e instanceof IOException) {
144-
LOG.w("Transcode failed: input source (" + options.getDataSource().toString() + ") not found"
145-
+ " or could not open output file ('" + options.getOutputPath() + "') .", e);
146-
listenerWrapper.onTranscodeFailed(e);
147-
throw e;
148-
149140
} else if (e instanceof RuntimeException) {
150141
LOG.e("Fatal error while transcoding, this might be invalid format or bug in engine or Android.", e);
151142
listenerWrapper.onTranscodeFailed(e);

lib/src/main/java/com/otaliastudios/transcoder/TranscoderOptions.java

+21-21
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import com.otaliastudios.transcoder.source.UriDataSource;
1212
import com.otaliastudios.transcoder.strategy.DefaultAudioStrategy;
1313
import com.otaliastudios.transcoder.strategy.DefaultVideoStrategies;
14-
import com.otaliastudios.transcoder.strategy.OutputStrategy;
14+
import com.otaliastudios.transcoder.strategy.TrackStrategy;
1515
import com.otaliastudios.transcoder.stretch.AudioStretcher;
1616
import com.otaliastudios.transcoder.stretch.DefaultAudioStretcher;
1717
import com.otaliastudios.transcoder.time.DefaultTimeInterpolator;
@@ -35,8 +35,8 @@ private TranscoderOptions() {}
3535

3636
private String outPath;
3737
private DataSource dataSource;
38-
private OutputStrategy audioOutputStrategy;
39-
private OutputStrategy videoOutputStrategy;
38+
private TrackStrategy audioTrackStrategy;
39+
private TrackStrategy videoTrackStrategy;
4040
private Validator validator;
4141
private int rotation;
4242
private TimeInterpolator timeInterpolator;
@@ -57,13 +57,13 @@ public DataSource getDataSource() {
5757
}
5858

5959
@NonNull
60-
public OutputStrategy getAudioOutputStrategy() {
61-
return audioOutputStrategy;
60+
public TrackStrategy getAudioTrackStrategy() {
61+
return audioTrackStrategy;
6262
}
6363

6464
@NonNull
65-
public OutputStrategy getVideoOutputStrategy() {
66-
return videoOutputStrategy;
65+
public TrackStrategy getVideoTrackStrategy() {
66+
return videoTrackStrategy;
6767
}
6868

6969
@NonNull
@@ -90,8 +90,8 @@ public static class Builder {
9090
private DataSource dataSource;
9191
private TranscoderListener listener;
9292
private Handler listenerHandler;
93-
private OutputStrategy audioOutputStrategy;
94-
private OutputStrategy videoOutputStrategy;
93+
private TrackStrategy audioTrackStrategy;
94+
private TrackStrategy videoTrackStrategy;
9595
private Validator validator;
9696
private int rotation;
9797
private TimeInterpolator timeInterpolator;
@@ -133,27 +133,27 @@ public Builder setDataSource(@NonNull Context context, @NonNull Uri uri) {
133133
* Sets the audio output strategy. If absent, this defaults to
134134
* {@link com.otaliastudios.transcoder.strategy.DefaultAudioStrategy}.
135135
*
136-
* @param outputStrategy the desired strategy
136+
* @param trackStrategy the desired strategy
137137
* @return this for chaining
138138
*/
139139
@NonNull
140140
@SuppressWarnings("unused")
141-
public Builder setAudioOutputStrategy(@Nullable OutputStrategy outputStrategy) {
142-
this.audioOutputStrategy = outputStrategy;
141+
public Builder setAudioTrackStrategy(@Nullable TrackStrategy trackStrategy) {
142+
this.audioTrackStrategy = trackStrategy;
143143
return this;
144144
}
145145

146146
/**
147147
* Sets the video output strategy. If absent, this defaults to the 16:9
148148
* strategy returned by {@link DefaultVideoStrategies#for720x1280()}.
149149
*
150-
* @param outputStrategy the desired strategy
150+
* @param trackStrategy the desired strategy
151151
* @return this for chaining
152152
*/
153153
@NonNull
154154
@SuppressWarnings("unused")
155-
public Builder setVideoOutputStrategy(@Nullable OutputStrategy outputStrategy) {
156-
this.videoOutputStrategy = outputStrategy;
155+
public Builder setVideoTrackStrategy(@Nullable TrackStrategy trackStrategy) {
156+
this.videoTrackStrategy = trackStrategy;
157157
return this;
158158
}
159159

@@ -262,11 +262,11 @@ public TranscoderOptions build() {
262262
if (looper == null) looper = Looper.getMainLooper();
263263
listenerHandler = new Handler(looper);
264264
}
265-
if (audioOutputStrategy == null) {
266-
audioOutputStrategy = new DefaultAudioStrategy(DefaultAudioStrategy.AUDIO_CHANNELS_AS_IS);
265+
if (audioTrackStrategy == null) {
266+
audioTrackStrategy = new DefaultAudioStrategy(DefaultAudioStrategy.AUDIO_CHANNELS_AS_IS);
267267
}
268-
if (videoOutputStrategy == null) {
269-
videoOutputStrategy = DefaultVideoStrategies.for720x1280();
268+
if (videoTrackStrategy == null) {
269+
videoTrackStrategy = DefaultVideoStrategies.for720x1280();
270270
}
271271
if (validator == null) {
272272
validator = new DefaultValidator();
@@ -282,8 +282,8 @@ public TranscoderOptions build() {
282282
options.dataSource = dataSource;
283283
options.outPath = outPath;
284284
options.listenerHandler = listenerHandler;
285-
options.audioOutputStrategy = audioOutputStrategy;
286-
options.videoOutputStrategy = videoOutputStrategy;
285+
options.audioTrackStrategy = audioTrackStrategy;
286+
options.videoTrackStrategy = videoTrackStrategy;
287287
options.validator = validator;
288288
options.rotation = rotation;
289289
options.timeInterpolator = timeInterpolator;

0 commit comments

Comments
 (0)