3
3
import android .annotation .SuppressLint ;
4
4
import android .content .ClipData ;
5
5
import android .content .Intent ;
6
+ import android .media .MediaMuxer ;
6
7
import android .net .Uri ;
7
8
import android .os .Bundle ;
8
9
import android .os .SystemClock ;
14
15
import com .otaliastudios .transcoder .Transcoder ;
15
16
import com .otaliastudios .transcoder .TranscoderListener ;
16
17
import com .otaliastudios .transcoder .TranscoderOptions ;
18
+ import com .otaliastudios .transcoder .engine .TrackStatus ;
17
19
import com .otaliastudios .transcoder .engine .TrackType ;
18
20
import com .otaliastudios .transcoder .internal .Logger ;
21
+ import com .otaliastudios .transcoder .sink .DataSink ;
22
+ import com .otaliastudios .transcoder .sink .DefaultDataSink ;
19
23
import com .otaliastudios .transcoder .strategy .DefaultAudioStrategy ;
20
24
import com .otaliastudios .transcoder .strategy .DefaultVideoStrategy ;
25
+ import com .otaliastudios .transcoder .strategy .RemoveTrackStrategy ;
21
26
import com .otaliastudios .transcoder .strategy .TrackStrategy ;
22
27
import com .otaliastudios .transcoder .strategy .size .AspectRatioResizer ;
23
28
import com .otaliastudios .transcoder .strategy .size .FractionResizer ;
24
29
import com .otaliastudios .transcoder .strategy .size .PassThroughResizer ;
30
+ import com .otaliastudios .transcoder .validator .DefaultValidator ;
25
31
26
32
import java .io .File ;
27
33
import java .io .IOException ;
@@ -58,6 +64,7 @@ public class TranscoderActivity extends AppCompatActivity implements
58
64
private TextView mAudioReplaceView ;
59
65
60
66
private boolean mIsTranscoding ;
67
+ private boolean mIsAudioOnly ;
61
68
private Future <Void > mTranscodeFuture ;
62
69
private Uri mTranscodeInputUri1 ;
63
70
private Uri mTranscodeInputUri2 ;
@@ -111,10 +118,13 @@ protected void onCreate(Bundle savedInstanceState) {
111
118
mAudioReplaceGroup .setOnCheckedChangeListener ((group , checkedId ) -> {
112
119
mAudioReplacementUri = null ;
113
120
mAudioReplaceView .setText ("No replacement selected." );
114
- if (checkedId == R .id .replace_yes && !mIsTranscoding ) {
115
- startActivityForResult (new Intent (Intent .ACTION_GET_CONTENT )
116
- .setType ("audio/*" ), REQUEST_CODE_PICK_AUDIO );
121
+ if (checkedId == R .id .replace_yes ) {
122
+ if (!mIsTranscoding ) {
123
+ startActivityForResult (new Intent (Intent .ACTION_GET_CONTENT )
124
+ .setType ("audio/*" ), REQUEST_CODE_PICK_AUDIO );
125
+ }
117
126
}
127
+ onCheckedChanged (group , checkedId );
118
128
});
119
129
}
120
130
@@ -136,10 +146,20 @@ private void syncParameters() {
136
146
case R .id .sampleRate_48 : sampleRate = 48000 ; break ;
137
147
default : sampleRate = DefaultAudioStrategy .SAMPLE_RATE_AS_INPUT ;
138
148
}
139
- mTranscodeAudioStrategy = DefaultAudioStrategy .builder ()
140
- .channels (channels )
141
- .sampleRate (sampleRate )
142
- .build ();
149
+ boolean removeAudio ;
150
+ switch (mAudioReplaceGroup .getCheckedRadioButtonId ()) {
151
+ case R .id .replace_remove : removeAudio = true ; break ;
152
+ case R .id .replace_yes : removeAudio = false ; break ;
153
+ default : removeAudio = false ;
154
+ }
155
+ if (removeAudio ) {
156
+ mTranscodeAudioStrategy = new RemoveTrackStrategy ();
157
+ } else {
158
+ mTranscodeAudioStrategy = DefaultAudioStrategy .builder ()
159
+ .channels (channels )
160
+ .sampleRate (sampleRate )
161
+ .build ();
162
+ }
143
163
144
164
int frames ;
145
165
switch (mVideoFramesGroup .getCheckedRadioButtonId ()) {
@@ -234,7 +254,8 @@ private void transcode() {
234
254
// Launch the transcoding operation.
235
255
mTranscodeStartTime = SystemClock .uptimeMillis ();
236
256
setIsTranscoding (true );
237
- TranscoderOptions .Builder builder = Transcoder .into (mTranscodeOutputFile .getAbsolutePath ());
257
+ DataSink sink = new DefaultDataSink (mTranscodeOutputFile .getAbsolutePath ());
258
+ TranscoderOptions .Builder builder = Transcoder .into (sink );
238
259
if (mAudioReplacementUri == null ) {
239
260
if (mTranscodeInputUri1 != null ) builder .addDataSource (this , mTranscodeInputUri1 );
240
261
if (mTranscodeInputUri2 != null ) builder .addDataSource (this , mTranscodeInputUri2 );
@@ -249,6 +270,13 @@ private void transcode() {
249
270
.setAudioTrackStrategy (mTranscodeAudioStrategy )
250
271
.setVideoTrackStrategy (mTranscodeVideoStrategy )
251
272
.setVideoRotation (rotation )
273
+ .setValidator (new DefaultValidator () {
274
+ @ Override
275
+ public boolean validate (@ NonNull TrackStatus videoStatus , @ NonNull TrackStatus audioStatus ) {
276
+ mIsAudioOnly = !videoStatus .isTranscoding ();
277
+ return super .validate (videoStatus , audioStatus );
278
+ }
279
+ })
252
280
.setSpeed (speed )
253
281
.transcode ();
254
282
}
@@ -268,19 +296,16 @@ public void onTranscodeCompleted(int successCode) {
268
296
if (successCode == Transcoder .SUCCESS_TRANSCODED ) {
269
297
LOG .w ("Transcoding took " + (SystemClock .uptimeMillis () - mTranscodeStartTime ) + "ms" );
270
298
onTranscodeFinished (true , "Transcoded file placed on " + mTranscodeOutputFile );
299
+ File file = mTranscodeOutputFile ;
300
+ String type = mIsAudioOnly ? "audio/mp4" : "video/mp4" ;
271
301
Uri uri = FileProvider .getUriForFile (TranscoderActivity .this ,
272
- FILE_PROVIDER_AUTHORITY ,
273
- mTranscodeOutputFile );
302
+ FILE_PROVIDER_AUTHORITY , file );
274
303
startActivity (new Intent (Intent .ACTION_VIEW )
275
- .setDataAndType (uri , "video/mp4" )
304
+ .setDataAndType (uri , type )
276
305
.setFlags (Intent .FLAG_GRANT_READ_URI_PERMISSION ));
277
306
} else if (successCode == Transcoder .SUCCESS_NOT_NEEDED ) {
278
- // TODO: Not sure this works
279
307
LOG .i ("Transcoding was not needed." );
280
- onTranscodeFinished (true , "Transcoding not needed, source file not touched." );
281
- startActivity (new Intent (Intent .ACTION_VIEW )
282
- .setDataAndType (mTranscodeInputUri1 , "video/mp4" )
283
- .setFlags (Intent .FLAG_GRANT_READ_URI_PERMISSION ));
308
+ onTranscodeFinished (true , "Transcoding not needed, source file untouched." );
284
309
}
285
310
}
286
311
0 commit comments