Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate mp4 files in Android using halfninja ffmpeg

I've manage to compile halfninja ffmpeg scripts for Android NDK using NDK version r5c. (Unfortunately any attempt to compile with earlier NDK generated some error), also I'm not very knowledgeable on the whole NDK process, so it's a bit hit-n-miss for me.

His scripts are compiling ffmpeg version N-30996-gf925b24 (the specific commit he did the scripts for)

Moving forward to my actual app. I manage to trim videos without problems, now I need to join/concatenate them but any attemp at using any and several combinations of the commands found on those 3 links (link1, link2, link3) generate errors such as cat is not valid, > is undefinined, unknown option filter_complex or trying to override some of the input files.

Does anyone know if it's possible and (how to do it), to join/concatenate mp4 videos (all same codec, size, quality, etc) using half-ninja compile of ffmpeg on Android, or how to compile/get a ffmpeg for Android using latest source codes?

I've also gave a quick try on the mp4Parser without much success.

ultimately I was trying to get this pseudo-method to work:

public static File concatenate(String[] inputPaths, String outputPath){

    // ... do stuff do generate ffmpeg commands....
    VideoKit v = new VideoKit();
    v.run(cmds);

    File f = new File(outputPath);
    return f;
}
like image 999
Budius Avatar asked Apr 19 '13 15:04

Budius


People also ask

How do I combine two videos in android programmatically Ffmpeg?

ffmpeg -f concat -i mylist.txt -c copy output.mp4 This process is a way of concatenating the files and not re-encoding. Hence, it should be done speedily & conveniently. If there is no error message, you are sure to receive a final merged video named by 'output. mp4' in the MP4 folder.

Can you concatenate mp4 files?

You can use FFmpeg to concatenate mp4 files very easily! There are many ways to do this including variations such as (1) concatenating only the audio (2) concatenating only video (3) concatenating all the files in a directory (4) concatenating files that do not have the same height, width, etc.

How do I combine two videos using ffmpeg?

Use FFmpeg concat to merge videos For videos that have the same codec, you can concatenate them using concat demuxer or concat protocol.


2 Answers

The answer provided by LordNeckbeard is really the way to go.

How to concatenate flv file into one?

Working with your restrictions

  • no -f concat
  • no -c
  • no -bsf
ffmpeg -i q.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb q.ts
ffmpeg -i r.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb r.ts
ffmpeg -i 'concat:q.ts|r.ts' -vcodec copy -acodec copy -absf aac_adtstoasc qr.mp4

Joining H264 *without* re-encoding

like image 146
Zombo Avatar answered Oct 20 '22 13:10

Zombo


this sequence will cat mp4's on the CLI. its from the ffmpeg faq page on concatenation...

$FFMPEG_HOME/ffmpeg  -i gpsclip_seg1.mp4  -vn -f u16le -acodec pcm_s16le -ac 1 -ar 44100 - > temp1.a < /dev/null
$FFMPEG_HOME/ffmpeg  -i gpsclip_seg2.mp4  -vn -f u16le -acodec pcm_s16le -ac 1 -ar 44100 - > temp2.a < /dev/null
$FFMPEG_HOME/ffmpeg  -i gpsclip_seg3.mp4  -vn -f u16le -acodec pcm_s16le -ac 1 -ar 44100 - > temp3.a < /dev/null
cat temp1.a temp2.a temp3.a > all.a

$FFMPEG_HOME/ffmpeg -i gpsclip_seg1.mp4 -an -f yuv4mpegpipe - > temp1.v < /dev/null &
$FFMPEG_HOME/ffmpeg -i gpsclip_seg2.mp4 -an -f yuv4mpegpipe - < /dev/null | tail -n +2 > temp2.v
$FFMPEG_HOME/ffmpeg -i gpsclip_seg3.mp4 -an -f yuv4mpegpipe - < /dev/null | tail -n +2 > temp3.v
cat temp1.v temp2.v temp3.v > all.v

$FFMPEG_HOME/ffmpeg -f u16le -acodec pcm_s16le -ac 1 -ar 44100 -i all.a -f yuv4mpegpipe -i all.v -same_quant -y output.mp4

i looked at halfninja's 'Android.mk' ... and for testing, you should be able to use adb to push 'ffmpeg' executable from halfninja build to /data/local/... on the phone. On building the project, I think the executable will be in ../output folder , above the JNI folder in his project.

Assuming you can get root on the device, you can then do testing on the CLI interface on the phone by getting a shell, then getting root using 'su', then copying cli expressions from ffmpeg/MP4/concat threads such as this one and running them on the phone with outputs to a folder where you have access.

In test mode, if you can get the desired result using step at a time CLI invocations as shown in the link's accepted answer, you can then move back to your JNI interfaces , calling into halfninja's 'videokit' package, implementing the same sequence of commands that you used in test.

Added Note on multiple calls...

Since you will be calling in to ffmpeg lib in the JNI multiple times, you should be aware of this issue that can impact multiple calls into ffmpeg thru JNI. If halfninja has not already done mediated this issue, you may have to change the Android.mk structure to implement the wrapper library talked about in the thread so that you can load/unload the required shared libs in between each call to ffmpeg via the JNI.

android and 'cat'

you should have a symlink in /system/bin on the phone

lrwxr-xr-x root     shell             2012-07-09 13:02 cat -> toolbox

if not , try 'busybox' install on the phone so you can simulate scripting on the cli on the phone.

like image 29
Robert Rowntree Avatar answered Oct 20 '22 11:10

Robert Rowntree