2

I have split a video file into two files 'part1' and 'part2' via Git bash. Both these files are individually unreadable. Now I need to concatenate these two files and then play the video. This works just fine via git bash but since I am very new to android, I can't seem to do it programmatically. I did come across an answer here which said to do something like this :

String[] command = {"ls","-al"}; ProcessBuilder builder = new ProcessBuilder(command); builder.directory(new File(/ngs/app/abc)); p = builder.start();

However, I don't know how to write the command 'cat part1 part2 > new.mp4' using this technique. Any help would be great! Thanks!

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • see this it helps you http://stackoverflow.com/questions/10277709/how-can-run-linux-command-on-android – Vishal Thakkar Sep 22 '16 at 11:51
  • @VishalThakkar Yes, but the point is, how do I include the two file names(to be read from the sd card) in the String? How is that going to work? – Sakshi Tyagi Sep 22 '16 at 11:58

1 Answers1

2

You have to invoke a shell and pass it the command line as script parameter. For example with the bash shell you would run the following

bash -c 'cat part1 part2 > new.mp4'

Given your template this would work out as following

String[] command = {"bash", "-c", "cat part1 part2 > new.mp4"};
ProcessBuilder builder = new ProcessBuilder(command);
builder.directory(new File(/ngs/app/abc));
p = builder.start();

Of course cat simply concatenates byte streams, hence the name. It's trivial to program smething like that yourself. Pseudocode (actually valid Python)

filnew = open("new.mp4", "wb")
fil1 = open("part1", "rb")
fil2 = open("part1", "rb")
filnew.write( fil1.read() )
filnew.write( fil2.read() )
filnew.close()
fil1.close()
fil2.close()
datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Except it's not quite *that* trivial. The pseudocode here reads the entirety of part1 into memory before writing it out, which for a large video on a mobile device, could cause the app to crash due to lack of memory. `cat` uses streaming, and so should an app that concatenates large files. – LarsH Apr 02 '18 at 06:18