I am trying to change a subtitle for a video in Exoplayer. Video can already have a subtitle or user can add a new subtitle from internal storage. I know that before starting the player we can create Mediasource for both subtitle and video and merge using MergingMediaSource. However, I am not sure how to replace/add new subtitles to the currently playing video? Is it possible using DynamicConcatenatingMediaSource or any other way to do so?
MediaSource[] subTitleMediaSources = new MediaSource[uris.length];
Format subtitleFormat = Format.createTextSampleFormat(
null,
MimeTypes.APPLICATION_SUBRIP,
C.SELECTION_FLAG_DEFAULT,
null);
for (int i = 0; i < uris.length; i++) {
String subTitle = getSubtitleFile(subs, uris[i]);
if (subTitle != null) {
subTitleMediaSources[i] = new SingleSampleMediaSource.Factory(mediaDataSourceFactory).
createMediaSource(Uri.parse(subTitle), subtitleFormat,
C.TIME_UNSET);
} else {
subTitleMediaSources[i] = new SingleSampleMediaSource.Factory(mediaDataSourceFactory).
createMediaSource(Uri.parse("dummy"), subtitleFormat, C.TIME_UNSET);
}
}
MediaSource mediaSource = mediaSources.length == 1 ? mediaSources[0]
: new ConcatenatingMediaSource(mediaSources);
MediaSource subsMediaSource = subTitleMediaSources.length == 1 ? subTitleMediaSources[0]
: new ConcatenatingMediaSource(subTitleMediaSources);
MediaSource mergedSource;
if (subsMediaSource == null) {
mergedSource = mediaSource;
} else {
mergedSource = new MergingMediaSource(mediaSource, subsMediaSource);
}
I was able to get this to work by removing the Mediasource and adding it again. I used a ConcatenatingMediaSource. I think there should be a better/easy way than this by just adding new subtitle source to existing MergingMediasource. Suggestions are most welcome.
public void onSubSelected(String path) {
final long position = player.getCurrentPosition();
if (path == null) {
return;
}
List<MediaSource> subtitleSource = new ArrayList<>();
Uri uri = uris[currentIndex];
MediaSource trackSource = buildMediaSource(uri);
List<String> subPaths;
if (!pathSubtitleMapping.containsKey(uri)) {
subPaths = new ArrayList<>();
subPaths.add(path);
subtitleSource.add(buildSubtitleSource(path));
pathSubtitleMapping.put(uris[currentIndex], subPaths);
} else {
subPaths = pathSubtitleMapping.get(uri);
if (!subPaths.contains(path)) {
subPaths.add(path);
}
for (String path1 : subPaths) {
subtitleSource.add(buildSubtitleSource(path1));
}
}
MediaSource mediaSources[] = new MediaSource[subtitleSource.size() + 1];
mediaSources[0] = trackSource;
int index = 1;
for (MediaSource source : subtitleSource) {
mediaSources[index] = source;
index++;
}
finalMediaSource.removeMediaSource(currentIndex);
finalMediaSource.addMediaSource(currentIndex, new MergingMediaSource(mediaSources), new Runnable() {
#Override
public void run() {
player.seekTo(currentIndex, position);
}
});
}
private Map<Uri, List<String>> pathSubtitleMapping = new HashMap<>(); // (uri, list of subs)
private MediaSource buildMediaSource(Uri uri) {
MediaSource newSource = new ExtractorMediaSource.Factory(mediaDataSourceFactory).createMediaSource(uri);
return newSource;
}
private MediaSource buildSubtitleSource(String path) {
Format subtitleFormat = Format.createTextSampleFormat(
null,
MimeTypes.APPLICATION_SUBRIP,
C.SELECTION_FLAG_DEFAULT,
null);
MediaSource mediaSource = new SingleSampleMediaSource.Factory(mediaDataSourceFactory).
createMediaSource(Uri.parse(path), subtitleFormat, C.TIME_UNSET);
return mediaSource;
}
I have a problem related to the merging of two videos. When I'm trying to merge two videos from different landscape modes (left/right) one of the videos is displaying upside down. I'm using MP4Parser as a library to manage the trimming and merging
protected void combineClips() throws IOException {
MediaMetadataRetriever m = new MediaMetadataRetriever();
for (int i = 0; i < paths.size(); i++) {
m.setDataSource(paths.get(i));
if (Build.VERSION.SDK_INT >= 17) {
String s = m.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
if (Integer.valueOf(s) == 0) {
isLeftLandscape = true;
}
if (Integer.valueOf(s) == 180) {
isRightLandscape = true;
}
}
}
for (int i = 0; i < paths.size(); i++) {
Movie tm = MovieCreator.build(paths.get(i));
Log.d(TAG, files.size() + "files");
files.add(tm);
}
if (isLeftLandscape && isRightLandscape) {
for (int i = 0; i < paths.size(); i++) {
m.setDataSource(paths.get(i));
if (Build.VERSION.SDK_INT >= 17) {
String s = m.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
if (Integer.valueOf(s) == 0)
{
Log.d(TAG, "the rotation is " + s);
rotate(paths.get(i), i);
}}
}
}
List<Track> videoTracks = new ArrayList<Track>();
List<Track> audioTracks = new ArrayList<Track>();
for (Movie movie : files) {
for (Track t : movie.getTracks()) {
if (t.getHandler().equals("soun")) {
audioTracks.add(t);
}
if (t.getHandler().equals("vide")) {
videoTracks.add(t);
}
}
}
Movie result = new Movie();
if (audioTracks.size() > 0) {
Log.d(TAG, String.valueOf(audioTracks.size()) + "audi");
result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
}
if (videoTracks.size() > 0) {
Log.d(TAG, String.valueOf(audioTracks.size()) + "vide");
result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
}
Container out = new DefaultMp4Builder().build(result);
String folder_main = "VidApp";
File f = new File(Environment.getExternalStorageDirectory(), folder_main);
if (!f.exists()) {
f.mkdirs();
}
FileChannel fc = new RandomAccessFile(String.format(System.getenv("EXTERNAL_STORAGE") + "/VidApp" + "/output.mp4"), "rw").getChannel();
out.writeContainer(fc);
fc.close();
while (!audioTracks.isEmpty()) {
audioTracks.remove(0);
}
while (!videoTracks.isEmpty()) {
videoTracks.remove(0);
}
while (!files.isEmpty()) {
files.remove(0);
}
}
public void rotate(String path, int position) throws IOException {
IsoFile isoFile = new IsoFile(path);
Movie m = new Movie();
List<TrackBox> trackBoxes = isoFile.getMovieBox().getBoxes(
TrackBox.class);
for (TrackBox trackBox : trackBoxes) {
trackBox.getTrackHeaderBox().setMatrix(Matrix.ROTATE_180);
m.addTrack(new Mp4TrackImpl(trackBox));
}
Log.d(TAG, position + " of " + files.size());
files.set(position, m);
}
I am using this code. I need to merge two videos. It saved all videos in temp folder but not in merged condition. Append and DoAppend are my functions which I want for merging the videos.
public String append(ArrayList<String> trimVideos) {
for (int i = 0; i < trimVideos.size() - 1; i++) {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
if (i == 0) {
String OutPutFileName = Constants.STORAGE_VIDEO_TEMP_PATH +
File.separator + "APPEND" + "_" + timeStamp + ".mp4";
doAppend(trimVideos.get(0), trimVideos.get(i + 1),OutPutFileName);
Log.e(Constants.TAG, "In First: " + i + " " + OutPutFileName);
} else {
String OutPutFileName = Constants.STORAGE_VIDEO_TEMP_PATH
+ File.separator + "APPEND" + i + "_" + timeStamp + ".mp4";
doAppend(lastAppendOut, trimVideos.get(i + 1), OutPutFileName);
Log.e(Constants.TAG, "In Second: " + i + " " + OutPutFileName);
}
}
Log.e(Constants.TAG, "In End: " + " " + lastAppendOut);
return lastAppendOut;
}
This Method Crashed my application on add track.
private String doAppend(String _firstVideo, String _secondVideo,String _newName) {
try {
Log.e("test", "Stage1");
FileInputStream fis1 = new FileInputStream(_firstVideo);
FileInputStream fis2 = new FileInputStream(_secondVideo);
Movie[] inMovies = new Movie[] {
MovieCreator.build(fis1.getChannel()),MovieCreator.build(fis2.getChannel()) };
List<Track> videoTracks = new LinkedList<Track>();
List<Track> audioTracks = new LinkedList<Track>();
//It returns one item of video and 2 item of video.
for (Movie m : inMovies) {
for (Track t : m.getTracks()) {
if (t.getHandler().equals("soun")) {
audioTracks.add(t);
}
if (t.getHandler().equals("vide")) {
videoTracks.add(t);
}
}
}
Log.e("test", "Stage2");
Movie result = new Movie();
if (audioTracks.size() > 0) {
result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
}
if (videoTracks.size() > 0) {
result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
}
IsoFile out = new DefaultMp4Builder().build(result);
Log.e("test", "Stage3");
String filename = _newName;
lastAppendOut = filename;
Log.e(Constants.TAG, "In Append: " + " " + lastAppendOut);
FileOutputStream fos = new FileOutputStream(filename);
FileChannel fco = fos.getChannel();
fco.position(0);
out.getBox(fco);
fco.close();
fos.close();
fis1.close();
fis2.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
Log.e("check", e.getMessage());
}
return _newName;
}
Code For Merging Multiple Video
Gradle Dependency
implementation 'com.googlecode.mp4parser:isoparser:1.1.9'
Code
private String appendTwoVideos(String firstVideoPath, String secondVideoPath)
{
try {
Movie[] inMovies = new Movie[2];
inMovies[0] = MovieCreator.build(firstVideoPath);
inMovies[1] = MovieCreator.build(secondVideoPath);
List<Track> videoTracks = new LinkedList<>();
List<Track> audioTracks = new LinkedList<>();
for (Movie m : inMovies) {
for (Track t : m.getTracks()) {
if (t.getHandler().equals("soun")) {
audioTracks.add(t);
}
if (t.getHandler().equals("vide")) {
videoTracks.add(t);
}
}
}
Movie result = new Movie();
if (audioTracks.size() > 0) {
result.addTrack(new AppendTrack(audioTracks
.toArray(new Track[audioTracks.size()])));
}
if (videoTracks.size() > 0) {
result.addTrack(new AppendTrack(videoTracks
.toArray(new Track[videoTracks.size()])));
}
BasicContainer out = (BasicContainer) new DefaultMp4Builder().build(result);
#SuppressWarnings("resource")
FileChannel fc = new RandomAccessFile(Environment.getExternalStorageDirectory() + "/wishbyvideo.mp4", "rw").getChannel();
out.writeContainer(fc);
fc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/wishbyvideo.mp4";
return mFileName;
}
You might wanna call this function from a background thread.
The above answer is perfectly right but It will only work if your media encoder is H264.....
mediaRecorder.setVideoEncoder(VideoEncoder.H264);
happy Coding :) :D
The above answer will only work when your codec, framerate, and bitrate are the same.
Gradle Dependency(This library will work in nearly every case)
implementation 'com.github.yangjie10930:EpMedia:v0.9.5'
CODE
private void mergeVideos() {
ArrayList<EpVideo> epVideos = new ArrayList<>();
epVideos.add(new EpVideo (file2)); // Video 1
epVideos.add(new EpVideo (file1)); // Video 2
EpEditor. OutputOption outputOption =new EpEditor.OutputOption(fileOutput);
outputOption.setWidth(720);
outputOption.setHeight(1280);
outputOption.frameRate = 25 ;
outputOption.bitRate = 10 ; //Default
EpEditor.merge(epVideos, outputOption, new OnEditorListener() {
#Override
public void onSuccess () {
Log.d("Status","Success");
}
#Override
public void onFailure () {
}
#Override
public void onProgress ( float progress ) {
// Get processing progress here
Log.d("Progress",""+progress);
}
});
}
Gradle Dependency
implementation "com.writingminds:FFmpegAndroid:0.3.2"
Code
Command to concate two videos side by side into one
val cmd : arrayOf("-y", "-i", videoFile!!.path, "-i", videoFileTwo!!.path, "-filter_complex", "hstack", outputFile.path)
Command to append two videos (one after another) into one
val cmd : arrayOf("-y", "-i", videoFile!!.path, "-i", videoFileTwo!!.path, "-strict", "experimental", "-filter_complex",
"[0:v]scale=iw*min(1920/iw\\,1080/ih):ih*min(1920/iw\\,1080/ih), pad=1920:1080:(1920-iw*min(1920/iw\\,1080/ih))/2:(1080-ih*min(1920/iw\\,1080/ih))/2,setsar=1:1[v0];[1:v] scale=iw*min(1920/iw\\,1080/ih):ih*min(1920/iw\\,1080/ih), pad=1920:1080:(1920-iw*min(1920/iw\\,1080/ih))/2:(1080-ih*min(1920/iw\\,1080/ih))/2,setsar=1:1[v1];[v0][0:a][v1][1:a] concat=n=2:v=1:a=1",
"-ab", "48000", "-ac", "2", "-ar", "22050", "-s", "1920x1080", "-vcodec", "libx264", "-crf", "27",
"-q", "4", "-preset", "ultrafast", outputFile.path)
Note :
"videoFile" is your first video path.
"videoFileTwo" is your second video path.
"outputFile" is your combined video path which is our final output path
To create output path of video
fun createVideoPath(context: Context): File {
val timeStamp: String = SimpleDateFormat(Constant.DATE_FORMAT, Locale.getDefault()).format(Date())
val imageFileName: String = "APP_NAME_"+ timeStamp + "_"
val storageDir: File? = context.getExternalFilesDir(Environment.DIRECTORY_MOVIES)
if (storageDir != null) {
if (!storageDir.exists()) storageDir.mkdirs()
}
return File.createTempFile(imageFileName, Constant.VIDEO_FORMAT, storageDir)
}
Code to execute command
try {
FFmpeg.getInstance(context).execute(cmd, object : ExecuteBinaryResponseHandler() {
override fun onStart() {
}
override fun onProgress(message: String?) {
callback!!.onProgress(message!!)
}
override fun onSuccess(message: String?) {
callback!!.onSuccess(outputFile)
}
override fun onFailure(message: String?) {
if (outputFile.exists()) {
outputFile.delete()
}
callback!!.onFailure(IOException(message))
}
override fun onFinish() {
callback!!.onFinish()
}
})
} catch (e: Exception) {
} catch (e2: FFmpegCommandAlreadyRunningException) {
}
I need merge a few mp4 files on android, so I am tried using mp4parser merge a few mp4.
private void mergeMp4(File output, String[] files) throws IOException {
Movie[] inMovies = new Movie[files.length];
for (int i=0, len = files.length; i<len; ++i) {
inMovies[i] = MovieCreator.build("/tmp/tmp/" + files[i]);
}
List<Track> videoTracks = new LinkedList<Track>();
List<Track> audioTracks = new LinkedList<Track>();
for (Movie m : inMovies) {
for (Track t : m.getTracks()) {
if (t.getHandler().equals("soun")) {
audioTracks.add(t);
}
if (t.getHandler().equals("vide")) {
videoTracks.add(t);
}
}
}
Movie result = new Movie();
if (audioTracks.size() > 0) {
result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
}
if (videoTracks.size() > 0) {
result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
}
Container out = new DefaultMp4Builder().build(result);
FileChannel fc = new RandomAccessFile(output, "rw").getChannel();
out.writeContainer(fc);
fc.close();
}
// work correct for non-merged mp4 file
private long getDuration(String filename) throws IOException {
IsoFile isoFile = new IsoFile(filename);
long lengthInSeconds = isoFile.getMovieBox().getMovieHeaderBox().getDuration() / isoFile.getMovieBox().getMovieHeaderBox().getTimescale();
return lengthInSeconds * 1000;
}
somewhere in code
File tmp = new File("/tmp/tmp");
String[] tmpFiles = tmp.list();
File recordedFile = new File("/mnt/sdcard/app/output.mp4")
mergeMp4(recordedFile, tmpFiles);
// both print value near 0 (about 20 millis)
Log.d("TAG", String.valueOf(getDuration(recordedFile)));
System.out.println(getDuration(recordedFile));
If I trying play merged file with android MediaPlayer or any other player:
player = new MediaPlayer();
try {
player.setDataSource(getFilename());
player.prepare();
player.start();
int duration = player.getDuaration();
// both print value near 0 (about 20 millis)
Log.d("TAG", String.valueOf(duration));
System.out.println(duration);
} catch (IOException e) {
Log.e(TAG, "prepare() failed", e);
}
It is merges files but all mediaplayers and even method of mp4parser author of getting duration returns that duration always about 0 (zero) seconds.
I am using Sebastian Annies example for the mp4parser where I append 3 videos. The result should be one video that plays all the three videos simultaneously. However, I get one video that plays the last video three times. Here is my code...
// int i = number of videos....
try {
String[] f = new String[i];
for (int count = 0; count < i; count++) {
f[count] = "/sdcard/vid" + i + ".mp4";
}
Movie[] inMovies = new Movie[i];
for (int count = 0; count < i; count++) {
inMovies[count] = MovieCreator.build(f[count]);
}
List<Track> videoTracks = new LinkedList<Track>();
List<Track> audioTracks = new LinkedList<Track>();
for (Movie m : inMovies) {
for (Track t : m.getTracks()) {
if (t.getHandler().equals("soun")) {
audioTracks.add(t);
}
if (t.getHandler().equals("vide")) {
videoTracks.add(t);
}
}
}
Movie result = new Movie();
if (audioTracks.size() > 0) {
result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
}
if (videoTracks.size() > 0) {
result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
}
Container out = new DefaultMp4Builder().build(result);
FileChannel fc = new RandomAccessFile(String.format
("/sdcard/output.mp4"),
"rw").getChannel();
out.writeContainer(fc);
fc.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
VideoView v = (VideoView) findViewById(R.id.videoView1);
// v.setVideoPath("/sdcard/aaa" + i + ".mp4");
v.setVideoPath("/sdcard/output.mp4");
v.setMediaController(new MediaController(this));
v.start();
I dont know why it isn't doing what it's supposed to do. Please help me. Thanks
Your problem is that you're filling the input file-names with the same file:
for (int count = 0; count < i; count++) {
f[count] = "/sdcard/vid" + i + ".mp4";
}
Should be
for (int count = 0; count < i; count++) {
f[count] = "/sdcard/vid" + count + ".mp4";
}
You might be better off, for readability, to make i the loop variable, and have count be the number of files.