How to merge 2 video files together in C#? - c#

I need to merge multiple video files (.wmv) together to get a single wmv file.
How can I do it?

You can do that easily Use Splicer, it free and open source in C#
Simplify developing applications for editing and encoding audio and video using DirectShow
Example:
using Splicer;
using Splicer.Timeline;
using Splicer.Renderer;
string firstVideoFilePath = #"C:\first.avi";
string secondVideoFilePath = #"C:\second.avi";
string outputVideoPath = #"C:\output.avi";
using (ITimeline timeline = new DefaultTimeline())
{
IGroup group = timeline.AddVideoGroup(32, 720, 576);
var firstVideoClip = group.AddTrack().AddVideo(firstVideoFilePath);
var secondVideoClip = group.AddTrack().AddVideo(secondVideoFilePath, firstVideoClip.Duration);
using (AviFileRenderer renderer = new AviFileRenderer(timeline, outputVideoPath))
{
renderer.Render();
}
}

You can split and join video files using DirectShow or the Windows Media Encoder.
DirectShowNet library has examples which you might find useful. I think its called DESCombine.

Related

Emgu CV throws a warning when combining two videos

I have a script that reads frames from two videos and combines them into one and saves the final video in the same folder with emgu cv. During the video merge process, Emgu CV prints a line in red that appears to be a warning or a flag that couldn't find video codec for mp4 raw input but then the video merge process works and any two videos i paste in that folder are merged.
//import the runtime
using Emgu.Cv;
//import other useful libraries
using System.IO;
namespace VideoMerger{
public class Video{
static void Main(string[] args){
//the name of the folder that contains the videos to be merged
string folder = "./Videos";
//get all the files in that path
string[] videos = Directory.GetFiles(folder);
if(videos.Length>0 && videos.Length==2){
//get the two videos
string video1 = videos[0];
string video2 = videos[1];
//create two emgu cv video objects
VideoCapture vidx = new VideoCapture(video1);
VideoCapture vidy = new VideoCapture(video2);
//declare the file name of the output video
string output = "combined.mp4";
//create a new video file writer object
VideoFileWriter writer = new VideoFileWriter(output, new Size(vidx.Width,vidx.Height),30, true);
//read frames from first video and write it to the third video
Mat mat = new Mat();
while(vidx.Grab()){
mat = vidx.Read(mat);
//write the frame to the output video
writer.Write(mat);
}
//add frames from video 2
Mat mymat = new Mat();
while(vidy.Grab()){
vidy.Read(mymat);
writer.Write(mymat);
}
//release the objects
vidx.Dispose();
vidy.Dispose();
}
}
}
}
why is it throwing that flag or that warning but then again manages to videos

Extract GPS meta data of a video using C#

I have tried following libraries in c# (.net core)
Media Info
Metadata Extractor
Tag Lib
Media Tool Kit
I wasn't able to retrieve GPS metadata from any of these libraries of a video.
But for an image, it worked fine. Also, I tried the Exif tool, but no luck.
Following are some code samples I used. Looking for help on this.
// using metadata extractor
var byteArray = await File.ReadAllBytesAsync("./meta_video.mp4");
Stream stream = new MemoryStream(byteArray);
var directories = QuickTimeMetadataReader.ReadMetadata(stream);
var directories1 = ImageMetadataReader.ReadMetadata("./meta_image1.jpg");
// using tag lib
var tfile = TagLib.File.Create(#"./meta_video.mp4");
// using media tool kit
var inputFile = new MediaToolkit.Model.MediaFile {Filename = #"./meta_video.mp4"};
using (var engine = new Engine())
{
engine.GetMetadata(inputFile);
}
// using media info
string fileNameAndPath = "./video2.mp4";
var myVideo = new MediaFile(fileNameAndPath);

Problem with comparing two images using imagemagick in C#

I am using ImageMagick tool in C# to identity two images are simiral or not.
var img1 = new ImageMagick.MagickImage("D:\\test\\img1.jpg");
var img2 = new ImageMagick.MagickImage("D:\\test\\img2.jpg");
var img3 = new ImageMagick.MagickImage("D:\\test\\img3.jpg");
// Return 1 Means Similar Image
if (img2.Compare(img1) == 1)
{
// Here I want to delete Duplicate Image
}
if (img3.Compare(img1) == 1)
{
// Here I don't want to delete because both image different
}
Here are images in sequence.
Please help me with this where I am wrong.
OR
Suggest any other best and quick solution to compare images visually in c#.

Merge image with a video with nreco

I have converted an image in to a vedio using following code
NReco.VideoConverter.ConvertSettings setting = new NReco.VideoConverter.ConvertSettings();
setting.SetVideoFrameSize(FrameSizeWidth, FrameSizeHeight);
setting.VideoCodec = "libx264";
setting.AudioCodec = "mp3";
setting.CustomInputArgs = " -framerate 1/"+imageDuration;
setting.CustomOutputArgs = " -profile:v high -r 30";
FFMpegConv.ConvertMedia(inputFilePath, null, outputFIlePath, null, setting);
And also I have a mp4 video with me which is splited using the follwoing code.
var ffMpegConverter = new FFMpegConverter();
ffMpegConverter.ConvertMedia(SourceFile, Format.mp4, DestinationFile, Format.mp4,
new ConvertSettings()
{
Seek = StartTime,
MaxDuration = (EndTime-StartTime),
VideoCodec = "libx264",
AudioCodec = "mp3",
});
I need to concat these two videos. I used the fllowing code for it.Here the SourceVideoList array contains the two videos(Splite and image video)
var ffMpegConverter = new FFMpegConverter();
ffMpegConverter.ConcatMedia(SourceVideoList.ToArray(),OutputFile,null,new ConcatSettings());
But when i try to do it gives and error saying "Cannot find a matching stream for unlabeled input pad 3 on filter Parsed_concat_0".
Can some one help me on this
ConcatMedia method uses ffmpeg concat filter; it expects that all input video chunks have the same frame size and frame rate. In most cases error you faced (Cannot find a matching stream for unlabeled input pad 3 on filter Parsed_concat_0) means that video chunks have incompatible set of parameters. You can get more information by handling LogReceived event (to get ffmpeg console log output).
Note that you can use another ways to merge videos supported by ffmpeg (concat demuxer, concat protocol - see https://trac.ffmpeg.org/wiki/Concatenate for more details) with ConvertMedia method overload that accepts several input files.

WP8/VS2013 MediaLibrary Songs collection is empty

I'm having a play with writing a mp3 player app for WP8, using MediaLibrary to handle the phone's own mp3 collection.
I want to test the result in the phone emulator on VS2013, but when I use the following code:
using (MediaLibrary library = new MediaLibrary())
{
SongCollection songs = library.Songs;
Song song = songs[0];
MediaPlayer.Play(song);
}
The song collection is empty, presumably because VS doesn't have any knowledge of a media library with songs in.
Is there any way to test this in the emulator using a fake medialibrary or for VS to use windows' media library? I just want to see (or hear) the code working before I proceed :)
I have managed to find a workaround!
If you add an mp3 file to the app's assets, the following code will add the mp3 to the media player library:
private void AddSong()
{
Uri file = new Uri("Assets/someSong.mp3", UriKind.Relative);
//copy file to isolated storage
var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
var fileStream = myIsolatedStorage.CreateFile("someSong.mp3");
var resource = Application.GetResourceStream(file);
int chunkSize = 4096;
byte[] bytes = new byte[chunkSize];
int byteCount;
while ((byteCount = resource.Stream.Read(bytes, 0, chunkSize)) > 0)
{
fileStream.Write(bytes, 0, byteCount);
}
fileStream.Close();
Microsoft.Xna.Framework.Media.PhoneExtensions.SongMetadata metaData = new Microsoft.Xna.Framework.Media.PhoneExtensions.SongMetadata();
metaData.AlbumName = "Some Album name";
metaData.ArtistName = "Some Artist Name";
metaData.GenreName = "test";
metaData.Name = "someSongName";
var ml = new MediaLibrary();
Uri songUri = new Uri("someSong.mp3", UriKind.RelativeOrAbsolute);
var song = Microsoft.Xna.Framework.Media.PhoneExtensions.MediaLibraryExtensions.SaveSong(ml, songUri, metaData, Microsoft.Xna.Framework.Media.PhoneExtensions.SaveSongOperation.CopyToLibrary);
}
I also needed to add:
using System.IO.IsolatedStorage;
I would love to claim credit for this, but I found the answer here:
http://social.msdn.microsoft.com/forums/wpapps/en-US/f5fa73da-176b-4aaa-8960-8f704236bda5/medialibrary-savesong-method
By default the media library on the emulator is empty. I also do not think it is possible to automagically hook up your dev machine's music folder to the emulator to test that way. It might be possible to manually configure the emulated phone with an email account! and save music onto it that way, but even if that worked you'd have to do it each and every time you restart the emulator.
Best way to test would be t deploy to a real device.

Categories