Android - merge videos taken with front and back cameras

534 Views Asked by At

I am having an orientation problem when merging videos taken with front and back cameras in portrait, recorded using Android CameraX.

This issue is explained here, but I am yet to find a working solution. I have tried multiple solutions, such as using mp4parser as suggested in the issue explanation, but the result is a that every alternate camera video is upside down. I also tried using FFmpeg for android - mobile-ffmpeg. Using ffmpeg Concat demuxer resulted in the same result as mp4parser did.

Concat demuxer command I used: ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4

I also tried changing the rotation metadata of the videos to match before concatenating, similar to how it's done here, but that did not seem to have any effect on the end result.

Lastly, I used ffmpeg Concat filter. That solution took longer obviously because of the re-encoding of the video but it did fix the problem of the orientation of the videos, but the output video quality was poor.

Concat filter command:

ffmpeg -i front1.mp4 -i rear1.mp4 -i front2.mp4 \
-filter_complex "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] \
concat=n=3:v=1:a=1 [v] [a]" \
-map "[v]" -map "[a]" output.mp4

Has anyone found a solution on how to fix the orientation problem in android? or maybe how can I keep the videos quality as much as possible when using the FFmpeg Concat filter to merge the videos?

2

There are 2 best solutions below

0
On

For any one who encounters this issue like I did in android, I have finally solved it by using FFmpeg Concat demuxer(mp4parser can be used as well for this), the key was providing video files with 0 for the rotation metadata. Those videos were recorded using CameraView takeVideoSnapshot().

0
On

for reduce the processing time, you can use the libx264 coding. In this link you can see the different preset available.

example :

ffmpeg -i front1.mp4 -i rear1.mp4 -i front2.mp4 \
-filter_complex "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] \
concat=n=3:v=1:a=1 [vv] [aa]" \
-map "[vv]" -map "[aa]" -vcodec libx264 -preset ultrafast output.mp4