Emgu CV throws a warning when combining two videos - c#

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

Related

UWP C# How to use ChromaKeyEffect on a video stream from MediaCapture and overlay the result on another MediaCapture webcam video stream?

I'm trying to get one video stream (like webcam) with MediaCapture and apply a Chromakey effect on it and overlay the result on another video stream (like another webcam) with MediaCapture too and record the result in a mp4 file during "live".
I've tried with custom video effect. The ChromaKeyEffect works but I've failed having the secondary input stream for put is as background in my effect class.
I've also try with MediaComposition but it doesn't work with MediaCapture. It use only MediaClips witch are not "live".
I don't know how should I do this.
Edit:
In my page
VideoEffectDefinition definition = new VideoEffectDefinition(typeof(AlphaOnMediaCaptureEffect).FullName);
await mediaCapture.AddVideoEffectAsync(definition, MediaStreamType.VideoRecord);
In a AlphaOnMediaCaptureEffect: IBasicVideoEffect
public async void ProcessFrame(ProcessVideoFrameContext context){
using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(canvasDevice, context.InputFrame.Direct3DSurface))
using (CanvasRenderTarget renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(canvasDevice, context.OutputFrame.Direct3DSurface, 96))
using (CanvasDrawingSession ds = renderTarget.CreateDrawingSession())
{
ChromaKeyEffect chromaKeyEffect = new ChromaKeyEffect
{
Source = inputBitmap,
Color = Color.FromArgb(255, 0, 255, 0),
Feather = true
};
chromaKeyEffect.Tolerance = 0.3f;
CompositeEffect compositeEffect = new CompositeEffect
{
Sources = { otherInputBitmap, chromaKeyEffect }
};
compositeEffect.Mode = CanvasComposite.SourceIn;
ds.DrawImage(compositeEffect);
}
I need another CanvasBitmap like inputBitmap (otherInputBitmap) which contains frames from a second mediacapture. I said "I've failed" because I didn't find a way to code this. I think I wrong in the "process" to obtain this live video overlay on a live video with an ChromaKeyEffect. I'm not sure that IBasicVideoEffect is the best way to do this.

PDF to bmp Images (12 pages = 12 images)

I have to deconstruct/extract a pdf page by page into bitmap images. This will be done on a server via a web service which I've setup. How do I get this right? It has to be page by page (1 page per image).
I am really stuck and I know one of you geniuses have the answer that I've been looking for.
I have tried: http://www.pdfsharp.net/wiki/ExportImages-sample.ashx Which didn't work correctly.
I am using C#;
The PDF is not password protected;
If this solution could take a Uri as a parameter for the location of the PDF it would be excellent!
The solution should not be reliant on Acrobat PDF Reader at all
I have been struggling for a very long time trying to use MigraDoc and PDFSharp and their alternatives to achieve the aforementioned problem.
ANY help/advice/code would be greatly appreciated!!
Thanks in advance!
LibPdf
This library converts converts PDF file to an image. Supported image formats are PNG and BMP, but you can easily add more.
Usage example:
using (FileStream file = File.OpenRead(#"..\path\to\pdf\file.pdf")) // in file
{
var bytes = new byte[file.Length];
file.Read(bytes, 0, bytes.Length);
using (var pdf = new LibPdf(bytes))
{
byte[] pngBytes = pdf.GetImage(0,ImageType.BMP); // image type
using (var outFile = File.Create(#"..\path\to\pdf\file.bmp")) // out file
{
outFile.Write(pngBytes, 0, pngBytes.Length);
}
}
}
Or Bytescout PDF Renderer SDK
using System;
using Bytescout.PDFRenderer;
namespace PDF2BMP
{
class Program
{
static void Main(string[] args)
{
// Create an instance of Bytescout.PDFRenderer.RasterRenderer object and register it.
RasterRenderer renderer = new RasterRenderer();
renderer.RegistrationName = "demo";
renderer.RegistrationKey = "demo";
// Load PDF document.
renderer.LoadDocumentFromFile("multipage.pdf");
for (int i = 0; i < renderer.GetPageCount(); i++)
{
// Render first page of the document to BMP image file.
renderer.RenderPageToFile(i, RasterOutputFormat.BMP, "image" + i + ".bmp");
}
// Open the first output file in default image viewer.
System.Diagnostics.Process.Start("image0.bmp");
}
}
}

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.

How to merge 2 video files together in 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.

Generating a multipage TIFF is not working

I'm trying to generate a multipage TIFF file from an existing picture using code by Bob Powell:
picture.SelectActiveFrame(FrameDimension.Page, 0);
var image = new Bitmap(picture);
using (var stream = new MemoryStream())
{
ImageCodecInfo codecInfo = null;
foreach (var imageEncoder in ImageCodecInfo.GetImageEncoders())
{
if (imageEncoder.MimeType != "image/tiff") continue;
codecInfo = imageEncoder;
break;
}
var parameters = new EncoderParameters
{
Param = new []
{
new EncoderParameter(Encoder.SaveFlag, (long) EncoderValue.MultiFrame)
}
};
image.Save(stream, codecInfo, parameters);
parameters = new EncoderParameters
{
Param = new[]
{
new EncoderParameter(Encoder.SaveFlag, (long) EncoderValue.FrameDimensionPage)
}
};
for (var i = 1; i < picture.GetFrameCount(FrameDimension.Page); i++)
{
picture.SelectActiveFrame(FrameDimension.Page, i);
var img = new Bitmap(picture);
image.SaveAdd(img, parameters);
}
parameters = new EncoderParameters
{
Param = new[]
{
new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.Flush)
}
};
image.SaveAdd(parameters);
stream.Flush();
}
But it's not working (only the first frame is included in the image) and I don't know why.
What I want to do is to change a particular frame of a TIFF file (add annotations to it).
I don't know if there's a simpler way to do it but what I have in mind is to create a multipage TIFF from the original picture and add my own picture instead of that frame.
[deleted first part after comment]
I'm working with multi-page TIFFs using LibTIFF.NET; I found many quicks in handling of TIFF using the standard libraries (memory related and also consistent crashes on 16-bit gray scale images).
What is your test image? Have you tried a many-frame tiff (preferably with a large '1' on the first frame, a '2 on the next etc; this could help you to be certain on the frame included in the file.
Another useful diagnosis may be tiffdump utility, as included in LibTiff binaries (also for windows). This will tell you exactly what frames you have.
See Using LibTiff from c# to access tiled tiff images
[Edit] If you want to understand the .NET stuff: I've found a new resource on multi-page tiffs using the standard .NET functionality (although I'll stick with LibTIFF.NET): TheCodeProject : Save images into a multi-page TIFF file... If you download it, the code snippet in Form1.cs function saveMultipage(..) is similar (but still slightly different) than your code. Especially the flushing at the end is done in a differnt way, and the file is deleted before the first frame...
[/Edit]
It seems that this process doesn't change image object but it changes the stream so I should get the memory stream buffer and build another image object:
var buffer=stream.GetBuffer();
using(var newStream=new MemoryStream(buffer))
{
var result=Image.FromStream(newStream);
}
Now result will include all frames.

Categories