Merge Videos Side-by-Side

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Merge Videos Side-by-Side

Post by Rye »

I'm not entirely sure if Imagemagick is actually capable of video editing/merging (or ffmpeg would be the better choice), however I figure I'll ask either way.

I have two videos, both the same resolution (with one being 2 seconds shorter than the other.

I want to merge them in one video that will show them side by side.

Like this:

Image + Image = ImageImage

Thanks in advance for a reply.
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Merge Videos Side-by-Side

Post by snibgo »

IM can't directly edit videos. But I do it all the time:

1. Use ffmpeg to disassemble shots into frames.

2. Use IM to process frames (and Sox and cSound to process sound).

3. Use ffmeg to reassemble movie.

The main command you need is something like:

Code: Select all

convert frame_A_000034.png frame_B_000034.png out_000034.png
You need to decide how (if at all) you want to crop the input frames, whether you want a gutter between them, what to do about the extra 2 seconds, etc.

For a simple one-off job, one of the free video editors might be easier and quicker.
snibgo's IM pages: im.snibgo.com
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Merge Videos Side-by-Side

Post by Rye »

Sound is not important.

The Resolutions should add up.

For example if both videos have a resolution of 640x480 the end result will be 1280x480.

So if I had two videos, called video1.avi and video2.avi.

What would "your" full commandlineset fro both ffmpeg and imagemagick look like ?
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
rich2005
Posts: 32
Joined: 2012-04-09T11:07:36-07:00
Authentication code: 8675308

Re: Merge Videos Side-by-Side

Post by rich2005 »

I can get you (the ffmpeg) part of the way there

individual frames
ffmpeg -i inputfile.avi -f image2 image-%07d.png

make a video from a sequence of stills
ffmpeg -r 15 -b 1000 -i image-%07d.png test.avi

where -r is the framerate - keep it the same as the original(s).
and -b is the required bitrate

That should work but recently the syntax has changed a little. It is try and see.

If using linux ffmpeg will be in the repo.
If using windoze then Dirk Paehl has a good up-to-date compilation
go to www.paehl.de/cms/ then 'Open Source' then 'Video Convert Tools'.

I can not think of a free video editor which will 'easily' join the 2 videos side by side.
If using Linux then Openshot is about the easiest I have come across for a picture-in-a-picture. Presumably you could run that twice on a blank frame.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Merge Videos Side-by-Side

Post by snibgo »

You can probably do the job entirely within a single ffmpeg command. But I don't know how; I just use ffmpeg as rich2005 says.

I gave an IM command in my first reply. You'd need to repeat it for every frame: put it in a script loop.
snibgo's IM pages: im.snibgo.com
rich2005
Posts: 32
Joined: 2012-04-09T11:07:36-07:00
Authentication code: 8675308

Re: Merge Videos Side-by-Side

Post by rich2005 »

Had a quick look at how I might do it in linux. I would need to dig out my M$DOS book for a windows batch file.

Using PClinuxOS here and ImageMagick 6.8.0
Need to +append in the command (I thought it was assumed?) ie.
convert frame1a.png frame1b.png +append newframe.png

A quick-n-dirty bash file, easier to rename the input to suit than add some variables.
So start with a.avi and b.avi (or whatever ffmpeg will support)

Best to use a new empty directory for the files Remember @ 25 fps (PAL) 1 minute of video = 1500 images.

Code: Select all

#!/bin/bash
#
# reduce the video to frames - change the names/format if required
# resist the temptation to add leading zeros to the filenumber
#
ffmpeg -i a.avi -f image2 a%d.png
ffmpeg -i b.avi -f image2 b%d.png
#
# join pairs of frames together
# ***set the ending frame in the next line otherwise***
# ********this will close with an error********
for n in $(seq 1 1 500)		
	do
	echo "frame_$n" # this line only to show something is happening
        convert "a$n".png "b$n".png +append "c$n".png
done
#
# join all the new frames back together
# change the frame rate -r bitrate -b and outfile name/format as required
#
ffmpeg -r 25 -b 2000 -i c%d.png combined.avi
#
# clean up
rm *.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Merge Videos Side-by-Side

Post by snibgo »

Eek, yes, I forgot to put "+append" in my command above. Sorry. It should read:

Code: Select all

convert frame_A_000034.png frame_B_000034.png +append out_000034.png
snibgo's IM pages: im.snibgo.com
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Merge Videos Side-by-Side

Post by Rye »

The only command that would have to be rewritten for windows is this , right ?

Code: Select all

for n in $(seq 1 1 500)      
   do
   echo "frame_$n" # this line only to show something is happening
        convert "a$n".png "b$n".png +append "c$n".png
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Merge Videos Side-by-Side

Post by snibgo »

Also remove the "done", as that is the closure of "do".

Also change "rm *.png" to "del *.png".
snibgo's IM pages: im.snibgo.com
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Merge Videos Side-by-Side

Post by Rye »

How do I have to modify this line:

Code: Select all

for n in $(seq 1 1 500)
in order for this to automatically process all frames of the video ?


Also:
If I have two videos with different framecount - for example they differ by 18 frames.
Is there a way to compensate for this.


This script actually works. I tried it.
Thanks so far !
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Merge Videos Side-by-Side

Post by snibgo »

If you have 500 frames, a Windows command in a bat file could be:

Code: Select all

for /L %%i in (1,1,500) do convert a%%i.png b%%i.png +append c%%i.png
You need to decide what you want to happen for the 18 missing frames. One solution would be to create dummy frames of the required size, perhaps black.

Code: Select all

for /L %%i in (483,1,500) do convert -size 640x480 xc:Black b%%i.png
snibgo's IM pages: im.snibgo.com
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Merge Videos Side-by-Side

Post by Rye »

However if you don't know the frame number ?

How about reading it from the video and saving it to a variable ?
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Merge Videos Side-by-Side

Post by snibgo »

You could use ffprobe to find the frames in the videos (see viewtopic.php?f=1&t=23911) or simply count the frames that come out of ffmpeg.
snibgo's IM pages: im.snibgo.com
Rye
Posts: 158
Joined: 2013-02-25T10:43:05-07:00
Authentication code: 6789

Re: Merge Videos Side-by-Side

Post by Rye »

So far so good.

However the end result looks really blurry and pixelated while the end frames "c" look much more crisp ?

Is there anyhting that can be done to help that ?
Version: ImageMagick-7.0.7-28-Q16-x64-static http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Merge Videos Side-by-Side

Post by snibgo »

What bitrate are the original videos, and what is your new video? Are the codecs the same?
snibgo's IM pages: im.snibgo.com
Post Reply