Skip to content

Feature/trim #50

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 16 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.Intent;
import android.media.MediaMuxer;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.TextView;
Expand Down Expand Up @@ -61,6 +63,8 @@ public class TranscoderActivity extends AppCompatActivity implements

private ProgressBar mProgressView;
private TextView mButtonView;
private EditText mTrimStartView;
private EditText mTrimEndView;
private TextView mAudioReplaceView;

private boolean mIsTranscoding;
Expand All @@ -74,6 +78,52 @@ public class TranscoderActivity extends AppCompatActivity implements
private long mTranscodeStartTime;
private TrackStrategy mTranscodeVideoStrategy;
private TrackStrategy mTranscodeAudioStrategy;
private long mTrimStartMillis = 0;
private long mTrimEndMillis = 0;

private TextWatcher mTrimStartTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void afterTextChanged(Editable s) {
if (s.length() > 0) {
try {
mTrimStartMillis = Long.valueOf(s.toString()) * 1000;
} catch (NumberFormatException e) {
mTrimStartMillis = 0;
LOG.w("Failed to read trimStart value.");
}
}
}
};
private TextWatcher mTrimEndTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void afterTextChanged(Editable s) {
if (s.length() > 0) {
try {
mTrimEndMillis = Long.valueOf(s.toString()) * 1000;
} catch (NumberFormatException e) {
mTrimEndMillis = 0;
LOG.w("Failed to read trimEnd value.");
}
}
}
};


@SuppressLint("SetTextI18n")
@Override
Expand All @@ -97,6 +147,8 @@ protected void onCreate(Bundle savedInstanceState) {
mProgressView = findViewById(R.id.progress);
mProgressView.setMax(PROGRESS_BAR_MAX);

mTrimStartView = findViewById(R.id.trim_start);
mTrimEndView = findViewById(R.id.trim_end);
mAudioReplaceView = findViewById(R.id.replace_info);

mAudioChannelsGroup = findViewById(R.id.channels);
Expand All @@ -113,6 +165,8 @@ protected void onCreate(Bundle savedInstanceState) {
mVideoResolutionGroup.setOnCheckedChangeListener(this);
mVideoAspectGroup.setOnCheckedChangeListener(this);
mAudioSampleRateGroup.setOnCheckedChangeListener(this);
mTrimStartView.addTextChangedListener(mTrimStartTextWatcher);
mTrimEndView.addTextChangedListener(mTrimEndTextWatcher);
syncParameters();

mAudioReplaceGroup.setOnCheckedChangeListener((group, checkedId) -> {
Expand Down Expand Up @@ -257,13 +311,27 @@ private void transcode() {
DataSink sink = new DefaultDataSink(mTranscodeOutputFile.getAbsolutePath());
TranscoderOptions.Builder builder = Transcoder.into(sink);
if (mAudioReplacementUri == null) {
if (mTranscodeInputUri1 != null) builder.addDataSource(this, mTranscodeInputUri1);
if (mTranscodeInputUri2 != null) builder.addDataSource(this, mTranscodeInputUri2);
if (mTranscodeInputUri3 != null) builder.addDataSource(this, mTranscodeInputUri3);
if (mTrimStartMillis > 0 || mTrimEndMillis > 0) {
if (mTranscodeInputUri1 != null) builder.addDataSource(this, mTranscodeInputUri1, mTrimStartMillis, mTrimEndMillis);
if (mTranscodeInputUri2 != null) builder.addDataSource(this, mTranscodeInputUri2, mTrimStartMillis, mTrimEndMillis);
if (mTranscodeInputUri3 != null) builder.addDataSource(this, mTranscodeInputUri3, mTrimStartMillis, mTrimEndMillis);
}
else {
if (mTranscodeInputUri1 != null) builder.addDataSource(this, mTranscodeInputUri1);
if (mTranscodeInputUri2 != null) builder.addDataSource(this, mTranscodeInputUri2);
if (mTranscodeInputUri3 != null) builder.addDataSource(this, mTranscodeInputUri3);
}
} else {
if (mTranscodeInputUri1 != null) builder.addDataSource(TrackType.VIDEO, this, mTranscodeInputUri1);
if (mTranscodeInputUri2 != null) builder.addDataSource(TrackType.VIDEO, this, mTranscodeInputUri2);
if (mTranscodeInputUri3 != null) builder.addDataSource(TrackType.VIDEO, this, mTranscodeInputUri3);
if (mTrimStartMillis > 0 || mTrimEndMillis > 0) {
if (mTranscodeInputUri1 != null) builder.addDataSource(TrackType.VIDEO, this, mTranscodeInputUri1, mTrimStartMillis, mTrimEndMillis);
if (mTranscodeInputUri2 != null) builder.addDataSource(TrackType.VIDEO, this, mTranscodeInputUri2, mTrimStartMillis, mTrimEndMillis);
if (mTranscodeInputUri3 != null) builder.addDataSource(TrackType.VIDEO, this, mTranscodeInputUri3, mTrimStartMillis, mTrimEndMillis);
}
else {
if (mTranscodeInputUri1 != null) builder.addDataSource(TrackType.VIDEO, this, mTranscodeInputUri1);
if (mTranscodeInputUri2 != null) builder.addDataSource(TrackType.VIDEO, this, mTranscodeInputUri2);
if (mTranscodeInputUri3 != null) builder.addDataSource(TrackType.VIDEO, this, mTranscodeInputUri3);
}
builder.addDataSource(TrackType.AUDIO, this, mAudioReplacementUri);
}
mTranscodeFuture = builder.setListener(this)
Expand Down
49 changes: 49 additions & 0 deletions demo/src/main/res/layout/activity_transcoder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,55 @@
android:layout_height="wrap_content" />
</RadioGroup>

<!-- TRIM -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="Trim (seconds)" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="?attr/listPreferredItemHeightSmall"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Start:" />

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/trim_start"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:inputType="number"
android:maxLines="1"
android:gravity="center_horizontal"
android:minWidth="48dp"
android:singleLine="true"
android:text="0" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="8dp"
android:text="End:" />

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/trim_end"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:inputType="number"
android:maxLines="1"
android:gravity="center_horizontal"
android:minWidth="48dp"
android:singleLine="true"
android:text="0" />
</LinearLayout>

<!-- REPLACE AUDIO -->
<TextView
android:padding="16dp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.otaliastudios.transcoder.source.DataSource;
import com.otaliastudios.transcoder.source.FileDescriptorDataSource;
import com.otaliastudios.transcoder.source.FilePathDataSource;
import com.otaliastudios.transcoder.source.TrimDataSource;
import com.otaliastudios.transcoder.source.UriDataSource;
import com.otaliastudios.transcoder.strategy.DefaultAudioStrategy;
import com.otaliastudios.transcoder.strategy.DefaultVideoStrategies;
Expand Down Expand Up @@ -174,12 +175,24 @@ public Builder addDataSource(@NonNull Context context, @NonNull Uri uri) {
return addDataSource(new UriDataSource(context, uri));
}

@NonNull
@SuppressWarnings({"unused", "UnusedReturnValue"})
public Builder addDataSource(@NonNull Context context, @NonNull Uri uri, long trimStartMs, long trimEndMs) {
return addDataSource(new TrimDataSource(new UriDataSource(context, uri), trimStartMs, trimEndMs));
}

@NonNull
@SuppressWarnings({"unused", "UnusedReturnValue"})
public Builder addDataSource(@NonNull TrackType type, @NonNull Context context, @NonNull Uri uri) {
return addDataSource(type, new UriDataSource(context, uri));
}

@NonNull
@SuppressWarnings({"unused", "UnusedReturnValue"})
public Builder addDataSource(@NonNull TrackType type, @NonNull Context context, @NonNull Uri uri, long trimStartMs, long trimEndMs) {
return addDataSource(type, new TrimDataSource(new UriDataSource(context, uri), trimStartMs, trimEndMs));
}

/**
* Sets the audio output strategy. If absent, this defaults to
* {@link com.otaliastudios.transcoder.strategy.DefaultAudioStrategy}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,13 @@ public void transcode(@NonNull TranscoderOptions options) throws InterruptedExce
// Now step for transcoders that are not completed.
audioCompleted = isCompleted(TrackType.AUDIO);
videoCompleted = isCompleted(TrackType.VIDEO);
if (!audioCompleted) {
if (!audioCompleted && !videoCompleted) {
final TrackTranscoder videoTranscoder = getCurrentTrackTranscoder(TrackType.VIDEO, options);
final TrackTranscoder audioTranscoder = getCurrentTrackTranscoder(TrackType.AUDIO, options);
stepped |= videoTranscoder.transcode(forceVideoEos) | audioTranscoder.transcode(forceAudioEos);
} else if (!audioCompleted) {
stepped |= getCurrentTrackTranscoder(TrackType.AUDIO, options).transcode(forceAudioEos);
}
if (!videoCompleted) {
} else if (!videoCompleted) {
stepped |= getCurrentTrackTranscoder(TrackType.VIDEO, options).transcode(forceVideoEos);
}
if (++loopCount % PROGRESS_INTERVAL_STEPS == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* A DataSource implementation that uses Android's Media APIs.
*/
public abstract class DefaultDataSource implements DataSource {
public abstract class DefaultDataSource extends MediaExtractorDataSource {

private final static String TAG = DefaultDataSource.class.getSimpleName();
private final static Logger LOG = new Logger(TAG);
Expand Down Expand Up @@ -214,4 +214,10 @@ public void rewind() {
mMetadata = new MediaMetadataRetriever();
mMetadataApplied = false;
}

@Override
protected MediaExtractor requireExtractor() {
ensureExtractor();
return mExtractor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.otaliastudios.transcoder.source;

import android.media.MediaExtractor;

/**
* DataSource that allows access to its MediaExtractor.
*/
abstract class MediaExtractorDataSource implements DataSource {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope we get rid of this (see comments below) but if we don't, this should be made public because it's in the TrimDataSource constructor.

Copy link
Collaborator Author

@mudar mudar Dec 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, 👍 I added

long seekTo(long timestampUs);

to the DataSource interface. The method also returns the new timestamp. This solves the problem where TrimDataSource could not call getSampleTime() anymore since we're removing its access to the extactor.

Signature is a bit similar to https://github.com/bcgit/bc-java/blob/master/core/src/main/java/org/bouncycastle/crypto/SkippingCipher.java#L23

abstract protected MediaExtractor requireExtractor();
}
Loading