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

Skip to content

Input/output abstractions #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Take a look at the demo app for a real example or keep reading below for documen
- Override frames timestamp, e.g. to slow down the middle part of the video [[docs]](#time-interpolation)
- Error handling [[docs]](#listening-for-events)
- Configurable validators to e.g. avoid transcoding if the source is already compressed enough [[docs]](#validators)
- Configurable video and audio strategies [[docs]](#output-strategies)
- Configurable video and audio strategies [[docs]](#track-strategies)

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

## Listening for events

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

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

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

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

## Output Strategies
## Track Strategies

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

```java
Transcoder.into(filePath)
.setVideoOutputStrategy(videoStrategy)
.setAudioOutputStrategy(audioStrategy)
.setVideoTrackStrategy(videoStrategy)
.setAudioTrackStrategy(audioStrategy)
// ...
```

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

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

#### `PassThroughTrackStrategy`

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

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

#### `RemoveTrackStrategy`

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

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

```java
Transcoder.into(filePath)
.setAudioOutputStrategy(new DefaultAudioStrategy(1)) // or..
.setAudioOutputStrategy(new DefaultAudioStrategy(2)) // or..
.setAudioOutputStrategy(new DefaultAudioStrategy(DefaultAudioStrategy.AUDIO_CHANNELS_AS_IS))
.setAudioTrackStrategy(new DefaultAudioStrategy(1)) // or..
.setAudioTrackStrategy(new DefaultAudioStrategy(2)) // or..
.setAudioTrackStrategy(new DefaultAudioStrategy(DefaultAudioStrategy.AUDIO_CHANNELS_AS_IS))
// ...
```

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

```java
Transcoder.into(filePath)
.setVideoOutputStrategy(DefaultVideoStrategies.for720x1280()) // 16:9
.setVideoOutputStrategy(DefaultVideoStrategies.for360x480()) // 4:3
.setVideoTrackStrategy(DefaultVideoStrategies.for720x1280()) // 16:9
.setVideoTrackStrategy(DefaultVideoStrategies.for360x480()) // 4:3
// ...
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@

import com.otaliastudios.transcoder.Transcoder;
import com.otaliastudios.transcoder.TranscoderListener;
import com.otaliastudios.transcoder.engine.TrackStatus;
import com.otaliastudios.transcoder.internal.Logger;
import com.otaliastudios.transcoder.strategy.DefaultAudioStrategy;
import com.otaliastudios.transcoder.strategy.DefaultVideoStrategy;
import com.otaliastudios.transcoder.strategy.OutputStrategy;
import com.otaliastudios.transcoder.strategy.RemoveTrackStrategy;
import com.otaliastudios.transcoder.strategy.TrackStrategy;
import com.otaliastudios.transcoder.strategy.size.AspectRatioResizer;
import com.otaliastudios.transcoder.strategy.size.FractionResizer;
import com.otaliastudios.transcoder.strategy.size.PassThroughResizer;
import com.otaliastudios.transcoder.validator.DefaultValidator;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -57,8 +54,8 @@ public class TranscoderActivity extends AppCompatActivity implements
private Uri mTranscodeInputUri;
private File mTranscodeOutputFile;
private long mTranscodeStartTime;
private OutputStrategy mTranscodeVideoStrategy;
private OutputStrategy mTranscodeAudioStrategy;
private TrackStrategy mTranscodeVideoStrategy;
private TrackStrategy mTranscodeAudioStrategy;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -186,8 +183,8 @@ private void transcode() {
mTranscodeFuture = Transcoder.into(mTranscodeOutputFile.getAbsolutePath())
.setDataSource(this, mTranscodeInputUri)
.setListener(this)
.setAudioOutputStrategy(mTranscodeAudioStrategy)
.setVideoOutputStrategy(mTranscodeVideoStrategy)
.setAudioTrackStrategy(mTranscodeAudioStrategy)
.setVideoTrackStrategy(mTranscodeVideoStrategy)
.setRotation(rotation)
.setSpeed(speed)
.transcode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;

import com.otaliastudios.transcoder.engine.internal.ISO6709LocationParser;
import com.otaliastudios.transcoder.internal.ISO6709LocationParser;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down
15 changes: 3 additions & 12 deletions lib/src/main/java/com/otaliastudios/transcoder/Transcoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@

import android.os.Handler;

import com.otaliastudios.transcoder.engine.TranscoderEngine;
import com.otaliastudios.transcoder.engine.Engine;
import com.otaliastudios.transcoder.source.DataSource;
import com.otaliastudios.transcoder.internal.Logger;
import com.otaliastudios.transcoder.validator.Validator;
import com.otaliastudios.transcoder.engine.ValidatorException;
import com.otaliastudios.transcoder.internal.ValidatorException;

import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
Expand Down Expand Up @@ -113,14 +112,12 @@ public Future<Void> transcode(@NonNull final TranscoderOptions options) {
@Override
public Void call() throws Exception {
try {
TranscoderEngine engine = new TranscoderEngine();
engine.setProgressCallback(new TranscoderEngine.ProgressCallback() {
Engine engine = new Engine(options.getDataSource(), new Engine.ProgressCallback() {
@Override
public void onProgress(final double progress) {
listenerWrapper.onTranscodeProgress(progress);
}
});
engine.setDataSource(options.getDataSource());
engine.transcode(options);
listenerWrapper.onTranscodeCompleted(SUCCESS_TRANSCODED);

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

} else if (e instanceof IOException) {
LOG.w("Transcode failed: input source (" + options.getDataSource().toString() + ") not found"
+ " or could not open output file ('" + options.getOutputPath() + "') .", e);
listenerWrapper.onTranscodeFailed(e);
throw e;

} else if (e instanceof RuntimeException) {
LOG.e("Fatal error while transcoding, this might be invalid format or bug in engine or Android.", e);
listenerWrapper.onTranscodeFailed(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.otaliastudios.transcoder.source.UriDataSource;
import com.otaliastudios.transcoder.strategy.DefaultAudioStrategy;
import com.otaliastudios.transcoder.strategy.DefaultVideoStrategies;
import com.otaliastudios.transcoder.strategy.OutputStrategy;
import com.otaliastudios.transcoder.strategy.TrackStrategy;
import com.otaliastudios.transcoder.stretch.AudioStretcher;
import com.otaliastudios.transcoder.stretch.DefaultAudioStretcher;
import com.otaliastudios.transcoder.time.DefaultTimeInterpolator;
Expand All @@ -35,8 +35,8 @@ private TranscoderOptions() {}

private String outPath;
private DataSource dataSource;
private OutputStrategy audioOutputStrategy;
private OutputStrategy videoOutputStrategy;
private TrackStrategy audioTrackStrategy;
private TrackStrategy videoTrackStrategy;
private Validator validator;
private int rotation;
private TimeInterpolator timeInterpolator;
Expand All @@ -57,13 +57,13 @@ public DataSource getDataSource() {
}

@NonNull
public OutputStrategy getAudioOutputStrategy() {
return audioOutputStrategy;
public TrackStrategy getAudioTrackStrategy() {
return audioTrackStrategy;
}

@NonNull
public OutputStrategy getVideoOutputStrategy() {
return videoOutputStrategy;
public TrackStrategy getVideoTrackStrategy() {
return videoTrackStrategy;
}

@NonNull
Expand All @@ -90,8 +90,8 @@ public static class Builder {
private DataSource dataSource;
private TranscoderListener listener;
private Handler listenerHandler;
private OutputStrategy audioOutputStrategy;
private OutputStrategy videoOutputStrategy;
private TrackStrategy audioTrackStrategy;
private TrackStrategy videoTrackStrategy;
private Validator validator;
private int rotation;
private TimeInterpolator timeInterpolator;
Expand Down Expand Up @@ -133,27 +133,27 @@ public Builder setDataSource(@NonNull Context context, @NonNull Uri uri) {
* Sets the audio output strategy. If absent, this defaults to
* {@link com.otaliastudios.transcoder.strategy.DefaultAudioStrategy}.
*
* @param outputStrategy the desired strategy
* @param trackStrategy the desired strategy
* @return this for chaining
*/
@NonNull
@SuppressWarnings("unused")
public Builder setAudioOutputStrategy(@Nullable OutputStrategy outputStrategy) {
this.audioOutputStrategy = outputStrategy;
public Builder setAudioTrackStrategy(@Nullable TrackStrategy trackStrategy) {
this.audioTrackStrategy = trackStrategy;
return this;
}

/**
* Sets the video output strategy. If absent, this defaults to the 16:9
* strategy returned by {@link DefaultVideoStrategies#for720x1280()}.
*
* @param outputStrategy the desired strategy
* @param trackStrategy the desired strategy
* @return this for chaining
*/
@NonNull
@SuppressWarnings("unused")
public Builder setVideoOutputStrategy(@Nullable OutputStrategy outputStrategy) {
this.videoOutputStrategy = outputStrategy;
public Builder setVideoTrackStrategy(@Nullable TrackStrategy trackStrategy) {
this.videoTrackStrategy = trackStrategy;
return this;
}

Expand Down Expand Up @@ -262,11 +262,11 @@ public TranscoderOptions build() {
if (looper == null) looper = Looper.getMainLooper();
listenerHandler = new Handler(looper);
}
if (audioOutputStrategy == null) {
audioOutputStrategy = new DefaultAudioStrategy(DefaultAudioStrategy.AUDIO_CHANNELS_AS_IS);
if (audioTrackStrategy == null) {
audioTrackStrategy = new DefaultAudioStrategy(DefaultAudioStrategy.AUDIO_CHANNELS_AS_IS);
}
if (videoOutputStrategy == null) {
videoOutputStrategy = DefaultVideoStrategies.for720x1280();
if (videoTrackStrategy == null) {
videoTrackStrategy = DefaultVideoStrategies.for720x1280();
}
if (validator == null) {
validator = new DefaultValidator();
Expand All @@ -282,8 +282,8 @@ public TranscoderOptions build() {
options.dataSource = dataSource;
options.outPath = outPath;
options.listenerHandler = listenerHandler;
options.audioOutputStrategy = audioOutputStrategy;
options.videoOutputStrategy = videoOutputStrategy;
options.audioTrackStrategy = audioTrackStrategy;
options.videoTrackStrategy = videoTrackStrategy;
options.validator = validator;
options.rotation = rotation;
options.timeInterpolator = timeInterpolator;
Expand Down
Loading