Skip to content

Feature/trim #50

New issue

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

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

Already on GitHub? # to your account

Merged
merged 16 commits into from
Jan 7, 2020
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
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 mTrimStartUs = 0;
private long mTrimEndUs = 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 {
mTrimStartUs = Long.valueOf(s.toString()) * 1000000;
} catch (NumberFormatException e) {
mTrimStartUs = 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 {
mTrimEndUs = Long.valueOf(s.toString()) * 1000000;
} catch (NumberFormatException e) {
mTrimEndUs = 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 (mTrimStartUs > 0 || mTrimEndUs > 0) {
if (mTranscodeInputUri1 != null) builder.addDataSource(this, mTranscodeInputUri1, mTrimStartUs, mTrimEndUs);
if (mTranscodeInputUri2 != null) builder.addDataSource(this, mTranscodeInputUri2, mTrimStartUs, mTrimEndUs);
if (mTranscodeInputUri3 != null) builder.addDataSource(this, mTranscodeInputUri3, mTrimStartUs, mTrimEndUs);
}
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 (mTrimStartUs > 0 || mTrimEndUs > 0) {
if (mTranscodeInputUri1 != null) builder.addDataSource(TrackType.VIDEO, this, mTranscodeInputUri1, mTrimStartUs, mTrimEndUs);
if (mTranscodeInputUri2 != null) builder.addDataSource(TrackType.VIDEO, this, mTranscodeInputUri2, mTrimStartUs, mTrimEndUs);
if (mTranscodeInputUri3 != null) builder.addDataSource(TrackType.VIDEO, this, mTranscodeInputUri3, mTrimStartUs, mTrimEndUs);
}
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 trimStartUs, long trimEndUs) {
return addDataSource(new TrimDataSource(new UriDataSource(context, uri), trimStartUs, trimEndUs));
}

@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 trimStartUs, long trimEndUs) {
return addDataSource(type, new TrimDataSource(new UriDataSource(context, uri), trimStartUs, trimEndUs));
}

/**
* 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 @@ -54,6 +54,14 @@ public interface DataSource {
*/
void selectTrack(@NonNull TrackType type);

/**
* Moves all selected tracks forward by the specified duration.
*
* @param durationUs requested duration
* @return the new presentation time in microseconds
*/
long seekBy(long durationUs);

/**
* Returns true if we can read the given track at this point.
* If true if returned, source should expect a {@link #readTrack(Chunk)} call.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ public void selectTrack(@NonNull TrackType type) {
mExtractor.selectTrack(mIndex.require(type));
}

@Override
public long seekBy(long durationUs) {
ensureExtractor();
final int trackCount = mExtractor.getTrackCount();
for (int i = 0; i < trackCount; i++) {
mExtractor.selectTrack(i);
}
long timestampUs = mExtractor.getSampleTime() + durationUs;
// Seeking once per track helps the extractor with Audio sampleTime issues
for (int i = 0; i < trackCount; i++) {
mExtractor.seekTo(timestampUs, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
timestampUs = mExtractor.getSampleTime();
}
return timestampUs;
}

@Override
public boolean isDrained() {
ensureExtractor();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.otaliastudios.transcoder.source;


import android.media.MediaFormat;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.otaliastudios.transcoder.engine.TrackType;
import com.otaliastudios.transcoder.internal.Logger;

import org.jetbrains.annotations.Contract;

/**
* A {@link DataSource} wrapper that trims source at both ends.
*/
public class TrimDataSource implements DataSource {
private static final String TAG = "TrimDataSource";
private static final Logger LOG = new Logger(TAG);
@NonNull
private DataSource source;
private long trimStartUs;
private long trimDurationUs;
private boolean didSeekTracks = false;

public TrimDataSource(@NonNull DataSource source, long trimStartUs, long trimEndUs) throws IllegalArgumentException {
if (trimStartUs < 0 || trimEndUs < 0) {
throw new IllegalArgumentException("Trim values cannot be negative.");
}
this.source = source;
this.trimStartUs = trimStartUs;
this.trimDurationUs = computeTrimDuration(source.getDurationUs(), trimStartUs, trimEndUs);
}

@Contract(pure = true)
private static long computeTrimDuration(long duration, long trimStart, long trimEnd) throws IllegalArgumentException {
if (trimStart + trimEnd > duration) {
throw new IllegalArgumentException("Trim values cannot be greater than media duration.");
}
return duration - trimStart - trimEnd;
}

@Override
public int getOrientation() {
return source.getOrientation();
}

@Nullable
@Override
public double[] getLocation() {
return source.getLocation();
}

@Override
public long getDurationUs() {
return trimDurationUs;
}

@Nullable
@Override
public MediaFormat getTrackFormat(@NonNull TrackType type) {
return source.getTrackFormat(type);
}

@Override
public void selectTrack(@NonNull TrackType type) {
source.selectTrack(type);
}

@Override
public long seekBy(long durationUs) {
return source.seekBy(durationUs);
}

@Override
public boolean canReadTrack(@NonNull TrackType type) {
if (!didSeekTracks) {
final long sampleTimeUs = seekBy(trimStartUs);
updateTrimValues(sampleTimeUs);
didSeekTracks = true;
}
return source.canReadTrack(type);
}

private void updateTrimValues(long timestampUs) {
trimDurationUs += trimStartUs - timestampUs;
trimStartUs = timestampUs;
}

@Override
public void readTrack(@NonNull Chunk chunk) {
source.readTrack(chunk);
}

@Override
public long getReadUs() {
return source.getReadUs();
}

@Override
public boolean isDrained() {
return source.isDrained() || getReadUs() >= getDurationUs();
}

@Override
public void releaseTrack(@NonNull TrackType type) {
source.releaseTrack(type);
}

@Override
public void rewind() {
didSeekTracks = false;
source.rewind();
}
}