illegalstateexception : Failed to add the track to the muxer while replacing audio of video
- Dominant language
- Java
- Stars
- 663
- Forks
- 92
- PR merge metrics
- No merged PRs in 30d
Description
I am Trying this code Trying to replace the audio of a Video , both the files have sound
I am getting this error
illegalstateexception : Failed to add the track to the muxer
Also how can we overlay audio keeping video's audio modifying the below code (I want to pass just the URI's, i have referred the demo code , but I have never used databinding and dnot plan to use it a simple implementation example will be helpful)
public class MainActivity extends AppCompatActivity {
private File targetVideoFile;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.frag2);
Uri videouri = Uri.parse("content://media/external/video/media/9092");
Uri audioUri = Uri.parse("content://media/external/audio/media/213");
TransformationListener tl = new TransformationListener() {
@Override
public void onStarted(String id) {
// TODO: Implement this method
Toast.makeText(MainActivity.this, "Started ", Toast.LENGTH_SHORT).show();
}
@Override
public void onProgress(String id, float progress) {
// TODO: Implement this method
Toast.makeText(MainActivity.this, "progress " + progress, Toast.LENGTH_SHORT).show();
}
@Override
public void onCompleted(String id, List trackTransformationInfos) {
//sm("ok");
Toast.makeText(MainActivity.this, "Completed ", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(String id, List trackTransformationInfos) {
// TODO: Implement this method
}
@Override
public void onError(String id, Throwable cause, List trackTransformationInfos) {
// sm("error=" + id);
Toast.makeText(MainActivity.this, cause.toString(), Toast.LENGTH_SHORT).show();
}
};
Button TransFormButton = (Button) findViewById(R.id.TransformButton);
TransFormButton.setOnClickListener(view -> {
Toast.makeText(MainActivity.this, "clicked ", Toast.LENGTH_SHORT).show();
File targetDirectory = getTargetFileDirectory();
targetVideoFile = new File(targetDirectory, "transcoded_" + "abc.mp4");
if (targetVideoFile.exists()) {
Toast.makeText(MainActivity.this, "existed", Toast.LENGTH_SHORT).show();
targetVideoFile.delete();
Toast.makeText(MainActivity.this, "Deleted ", Toast.LENGTH_SHORT).show();
}
MediaSource videoMediaSource = null;
try {
videoMediaSource = new MediaExtractorMediaSource(MainActivity.this, videouri);
} catch (MediaSourceException e) {
throw new RuntimeException(e);
}
MediaSource audioMediaSource = null;
try {
audioMediaSource = new MediaExtractorMediaSource(MainActivity.this, audioUri);
} catch (MediaSourceException e) {
throw new RuntimeException(e);
}
MediaTarget combinedMediaTarget = null;
try {
combinedMediaTarget = new MediaMuxerMediaTarget(targetVideoFile.getAbsolutePath(),
2, 0, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
} catch (MediaTargetException e) {
throw new RuntimeException(e);
}
TrackTransform videoTrackTransform = new TrackTransform.Builder(videoMediaSource, 0, combinedMediaTarget)
.setDecoder(new MediaCodecDecoder())
.setEncoder(new MediaCodecEncoder())
.setTargetTrack(0)
.setRenderer(new GlVideoRenderer(null))
.setTargetFormat(null)
.build();
TrackTransform audioTrackTransform = new TrackTransform.Builder(audioMediaSource, 0, combinedMediaTarget)
.setDecoder(new MediaCodecDecoder())
.setEncoder(new MediaCodecEncoder())
.setTargetTrack(1)
.setTargetFormat(null)
.build();
List trackTransforms = new ArrayList<>();
trackTransforms.add(videoTrackTransform);
trackTransforms.add(audioTrackTransform);
MediaTransformer mediaTransformer = new MediaTransformer(getApplicationContext());
mediaTransformer.transform("663600", trackTransforms, tl, MediaTransformer.GRANULARITY_DEFAULT);
});
}
@NonNull
private File getTargetFileDirectory() {
File targetDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)
+ File.separator
+ "LiTr");
if (!targetDirectory.exists()) {
targetDirectory.mkdirs();
}
return targetDirectory;
}
}
```
LogCat shows this error
```
Unsupported mime 'audio/mpeg'
2023-01-22 11:31:41.946 5631-8853 TransformationJob com.own.litrown E Transformation job error
java.lang.IllegalStateException: Failed to add the track to the muxer
at android.media.MediaMuxer.nativeAddTrack(Native Method)
at android.media.MediaMuxer.addTrack(MediaMuxer.java:658)
at com.linkedin.android.litr.io.MediaMuxerMediaTarget.addTrack(MediaMuxerMediaTarget.java:116)
at com.linkedin.android.litr.transcoder.PassthroughTranscoder.processNextFrame(PassthroughTranscoder.java:79)
at com.linkedin.android.litr.TransformationJob.processNextFrame(TransformationJob.java:211)
at com.linkedin.android.litr.TransformationJob.transform(TransformationJob.java:108)
at com.linkedin.android.litr.TransformationJob.run(TransformationJob.java:77)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:463)
at java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
at java.lang.Thread.run(Thread.java:1012)
```
Contributor guide
Research direction
Start with the stack-trace locations in MediaMuxerMediaTarget.java:116 and PassthroughTranscoder.java:79, then reproduce the failure using the supplied video and audio URIs. Compare the media formats reaching the muxer and determine the supported input combination. Done means the transformation completes without the reported exception and the requested audio behavior is verified.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, java
- Domain
- audio-video-rtc, mobile
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100