Jill
Jill

Reputation: 533

FFmpeg - Concatenating videos with different resolutions

I currently have an android app where the videos get combined using ffmpeg's concat. That works fine. However, when I add text (using drawtext) to one of the videos, after combining the videos again, the combined video gets distorted. The video with the drawtext shows up fine, but then the videos without text switches orientation.

I think the problem roots from the fact that a vertical video on my android phone has the resolution of 1920x1080, however if I use that for the scale when executing drawtext, it becomes horizontal.

I think an example might help explain this better. Let's say I have 2 vertically recorded videos, and I combine them with concat. The combined video shows up fine. Then if I add text to the 1st video using drawtext, the combined video will then show the first video (with text) at normal orientation (vertical). However, when it transitions to the next video, it first stalls for a couple seconds and then the 2nd video is shown at a horizontal orientation.

Also, I tried this also with having the aspect ratio as 1:1 so it wouldn't matter, but the second video's orientation still changed counter clockwise.

Thanks for the help, and happy to add more clarification if needed.

This is for adding the text:

ffmpeg -i input.mp4 -vf drawbox=y=ih-200:[email protected]:width=iw:height=200:t=max
    ,drawtext=fontfile=RobotoRegular.ttf:text='Text':x=(w-text_w)/2:y=(h-text_h)-100:fontsize=70:fontcolor=white
    -c:v libx264 -b:v 17000k -r 30 -preset ultrafast -y output.mp4

Output:

ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
                                                       built with gcc 4.8 (GCC)
                                                       configuration: --target-os=linux --cross-prefix=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/vagrant/SourceCode/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/vagrant/SourceCode/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
                                                       libavutil      55. 17.103 / 55. 17.103
                                                       libavcodec     57. 24.102 / 57. 24.102
                                                       libavformat    57. 25.100 / 57. 25.100
                                                       libavdevice    57.  0.101 / 57.  0.101
                                                       libavfilter     6. 31.100 /  6. 31.100
                                                       libswscale      4.  0.100 /  4.  0.100
                                                       libswresample   2.  0.101 /  2.  0.101
                                                       libpostproc    54.  0.100 / 54.  0.100
                                                     Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/emulated/0/Android/data/com.me.app/files/65/VID_20170802_111518.mp4':
                                                       Metadata:
                                                         major_brand     : mp42
                                                         minor_version   : 0
                                                         compatible_brands: isommp42
                                                         creation_time   : 2017-08-02 18:15:20
                                                         com.android.version: 7.0
                                                       Duration: 00:00:00.93, start: 0.000000, bitrate: 16415 kb/s
                                                         Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080, 16408 kb/s, SAR 1:1 DAR 16:9, 28.94 fps, 30 tbr, 90k tbn, 180k tbc (default)
                                                         Metadata:
                                                           rotate          : 90
                                                           creation_time   : 2017-08-02 18:15:20
                                                           handler_name    : VideoHandle
                                                         Side data:
                                                           displaymatrix: rotation of -90.00 degrees
                                                     [libx264 @ 0xeab7ec00] using SAR=1/1
                                                     [libx264 @ 0xeab7ec00] using cpu capabilities: none!
                                                     [libx264 @ 0xeab7ec00] profile Constrained Baseline, level 4.1
                                                     [libx264 @ 0xeab7ec00] 264 - core 148 - H.264/MPEG-4 AVC codec - Copyleft 2003-2015 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=25 scenecut=0 intra_refresh=0 rc=abr mbtree=0 bitrate=17000 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
                                                     Output #0, mp4, to '/storage/emulated/0/Android/data/com.me.app/files/65/temp.mp4':
                                                       Metadata:
                                                         major_brand     : mp42
                                                         minor_version   : 0
                                                         compatible_brands: isommp42
                                                         com.android.version: 7.0
                                                         encoder         : Lavf57.25.100
                                                         Stream #0:0(eng): Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1080x1920 [SAR 1:1 DAR 9:16], q=-1--1, 17000 kb/s, 30 fps, 15360 tbn, 30 tbc (default)
                                                         Metadata:
                                                           handler_name    : VideoHandle
                                                           creation_time   : 2017-08-02 18:15:20
                                                           encoder         : Lavc57.24.102 libx264
                                                         Side data:
                                                           unknown side data type 10 (24 bytes)
                                                     Stream mapping:
                                                       Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
                                                     Press [q] to stop, [?] for help
                                                     frame=    6 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=1 drop=0 speed=   0x    
                                                     frame=   10 fps=8.8 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=1 drop=0 speed=   0x    
                                                     frame=   14 fps=8.4 q=15.0 size=     155kB time=00:00:00.03 bitrate=38067.5kbits/s dup=1 drop=0 speed=0.0201x    
                                                     frame=   18 fps=8.0 q=22.0 size=     744kB time=00:00:00.16 bitrate=36592.5kb

ffmpeg -f concat -safe 0 -i input.mp4 -c copy -y output.mp4

Output:

ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
                                                        built with gcc 4.8 (GCC)
                                                        configuration: --target-os=linux --cross-prefix=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/vagrant/SourceCode/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/vagrant/SourceCode/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
                                                        libavutil      55. 17.103 / 55. 17.103
                                                        libavcodec     57. 24.102 / 57. 24.102
                                                        libavformat    57. 25.100 / 57. 25.100
                                                        libavdevice    57.  0.101 / 57.  0.101
                                                        libavfilter     6. 31.100 /  6. 31.100
                                                        libswscale      4.  0.100 /  4.  0.100
                                                        libswresample   2.  0.101 /  2.  0.101
                                                        libpostproc    54.  0.100 / 54.  0.100
                                                      [mov,mp4,m4a,3gp,3g2,mj2 @ 0xe762a600] Auto-inserting h264_mp4toannexb bitstream filter
                                                      Input #0, concat, from '/storage/emulated/0/Android/data/com.me.app/files/64/TXT_20170802_110325.txt':
                                                        Duration: N/A, start: 0.000000, bitrate: 23403 kb/s
                                                          Stream #0:0(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 1080x1920 [SAR 1:1 DAR 9:16], 23403 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc
                                                          Metadata:
                                                            handler_name    : VideoHandler
                                                      Output #0, mp4, to '/storage/emulated/0/Android/data/com.me.app/files/64/VID_20170802_110325concat.mp4':
                                                        Metadata:
                                                          encoder         : Lavf57.25.100
                                                          Stream #0:0(eng): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1080x1920 [SAR 1:1 DAR 9:16], q=2-31, 23403 kb/s, 30 fps, 30 tbr, 15360 tbn, 15360 tbc
                                                          Metadata:
                                                            handler_name    : VideoHandler
                                                      Stream mapping:
                                                        Stream #0:0 -> #0:0 (copy)
                                                      Press [q] to stop, [?] for help
                                                      [mov,mp4,m4a,3gp,3g2,mj2 @ 0xe762a600] Auto-inserting h264_mp4toannexb bitstream filter
                                                      frame=   64 fps=0.0 q=-1.0 Lsize=    5218kB time=00:00:12.30 bitrate=3474.5kbits/s speed= 122x    
                                                      video:5217kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.025721%

Upvotes: 1

Views: 3561

Answers (1)

OldC
OldC

Reputation: 186

From your log, you can see that your source is 1920x1080 with meta data rotation 90. After drawtext, the output is 1080x1920 with no meta data.

Then you concat 1080x1920 video without rotation and 1920x1080 video with rotation 90 together. The final output is half 1080x1920 and half 1920x1080 video without rotation.

My suggestion is to use transpose filter to rotate your video to 0 degree before processing and concat.

Update

If you want to keep the original rotation, you need to drawtext on a intermediate buffer, rotate it, then overlay with the input. You may also need to add the meta data to the output. The sample command is as follow:

ffmpeg -f lavfi -i [email protected]:s=1080x1920,format=yuva444p -noautorotate -i input.mp4 \
-filter_complex "[0:v]drawbox=y=ih-200:[email protected]:width=iw:height=200:t=max,drawtext=fontfile=RobotoRegular.ttf:text='Text':x=(w-text_w)/2:y=(h-text_h)-100:fontsize=70:fontcolor=white,transpose=dir=2,[1:v]overlay" \
-c:v libx264 -b:v 17000k -r 30 -preset ultrafast -y output1.mp4

ffmpeg -i output1.mp4 -c copy -metadata:s:v:0 rotate=-90 output2.mp4

Then you can concat the output with your another rotated source.

Upvotes: 1

Related Questions