6/21/2017 c#HowcaniembediconandvideostreamStackOverflow
xDismiss
JointheStackOverflowCommunity
Stack Overflow is a community of 7.3 million
programmers, just like you, helping each other.
Join them it only takes a minute:
Signup
Howcaniembediconandvideostream
Ihavesomemp4videofileandiwanttosetinthisvideofilesomesmallicon(canbesimplebitmap).
Iwanttowritesomeapplicationthattheinputismp4/mpegfileandsomeiconorbitmapandtheoutputisthesamemp4withthisiconembed
intheleftcorner.
Howtodoit?Istheresomecodeexampleforthis?
c# c++ video videostreaming videocapture
editedMar13'14at21:13 askedMar13'14at13:16
129followers,1.7kquestions rss
Yanshof
Questionsrelatedtocomputerorientedvideocaptureandthe 3,745 10 50 116
programmingtoolsandsolutionsfordealingwithcapturedframes
fromavideo.
Well,almostalliconsarebasedonfiletype.TheonlyexceptionIcanrememberareexecutables,onwhich
frequent info topusers
theiconisjustaresourceallocatedinsidethepackage.msdn.microsoft.com/en
us/library/ms648004%28v=VS.85%29.aspx orezvaniMar25'14at5:56
Ifyoudon'thavetodothisinaprogramofyourownthereisaninstructableshowinghowtoembedalogo
intoyourvideousingwindowsmoviemaker:instructables.com/id/andypMar26'14at19:44
butineedtodoitinmyownprogram. Yanshof Mar27'14at5:17
1Answer
Youcanusealotoflibrariestoaccomplishsuchathing,IwillshowyouawaytodoitusingC++
andOpenCV.
Youbasicallyneedtodothreethings:
1.Youneedtodecodetheinputvideofile
2.Youneedtoprocesseachframeinordertoaddyouricon/imageinitscorner
3.Youneedtoreencodethemodifiedframe,andsaveitintoanewvideofile
OpenCVisalibrarythatspecializesinimageprocessing.Itcanhelpyouaccomplishtask#2.It
alsohassomefunctionalitythatallowsyoutoreadandwritevideofiles,butit'snotasadvanced
asotherlibrariesthatspecializeinencoding/decodingvideofiles(likeffmpeg).
IhavecreatedasamplethatusesOpenCVtoreadavideofile(wmv),andaddtoeachframean
imagefile(jpg).Theresultwillbeanothervideofile(avi).OpenCVcanalsoreadothertypesof
videofiles(mp4),butIcouldn'tgetittosavean.mp4file,Icouldonlymakeitsavean.avifile.
InordertousemysampleyouneedtosetupOpenCVinyourplatformandIDEofchoice.You
canfindinformationabouthowtodothisontheOpenCVwebsite.
InmysampleIusetheMJPEGencodertosavethevideofile.Ifyoureplace
CV_FOURCC('M','J','P','G') with 1 awindowwillpopupthatwillaskyouwhatencoderyou
wanttouse.Ifyouwanttouseanotherencoder,justput 1 inthere.
HereisthecodethatIused:
#include<opencv2/opencv.hpp>
usingnamespacestd;
usingnamespacecv;
https://stackoverflow.com/questions/22379852/howcaniembediconandvideostream 1/3
6/21/2017 c#HowcaniembediconandvideostreamStackOverflow
voidaddImageInCorner(MatvideoFrame,MatresizedImage)
{
//Thedestinationlocationoftheimageintheframe(upperleftcorner)
RectdestinationRect(0,0,resizedImage.cols,resizedImage.rows);
//Copytheimagetothespecifiedlocationintheframe
resizedImage.copyTo(videoFrame(destinationRect));
}
voidmain()
{
stringinputVideoFile="C:\\Users\\Public\\Videos\\SampleVideos\\Wildlife.wmv";
stringinputImageFile="C:\\Users\\Public\\Pictures\\SamplePictures\\Koala.jpg";
stringoutputVideoFile="C:\\Users\\Public\\Videos\\SampleVideos\\output.avi";
//Readimagefile
Matimage=imread(inputImageFile);
MatresizedImage;
resize(image,resizedImage,Size(400,300));
//Openvideocapture
VideoCapturecapture;
capture.open(inputVideoFile);
if(!capture.isOpened())
{
return;
}
//Getthecaptureproperties
doublefps=capture.get(CV_CAP_PROP_FPS);
intvideoWidth=capture.get(CV_CAP_PROP_FRAME_WIDTH);
intvideoHeight=capture.get(CV_CAP_PROP_FRAME_HEIGHT);
//Initializethevideowriter
VideoWriterwriter;
writer.open(outputVideoFile,CV_FOURCC('M','J','P','G'),fps,Size(videoWidth,
videoHeight));
MatcurrentFrame;
namedWindow("video");
//Whilethewindowisstillopen
while(cvGetWindowHandle("video")!=NULL)
{
//Readthenextframe
capture>>currentFrame;
if(currentFrame.empty())
{
//Iftheframecouldnotberead,exittheloop
break;
}
//Addtheimagetothecornerofthevideoframe
addImageInCorner(currentFrame,resizedImage);
writer<<currentFrame;
//Showtheimageinthevideowindow
imshow("video",currentFrame);
waitKey(10);
}
writer.release();
}
Hereistheresultingimage:
Ihopethishelpsyou.
editedMar27'14at13:49 answeredMar27'14at13:42
Ove
4,227 1 24 54
https://stackoverflow.com/questions/22379852/howcaniembediconandvideostream 2/3
6/21/2017 c#HowcaniembediconandvideostreamStackOverflow
https://stackoverflow.com/questions/22379852/howcaniembediconandvideostream 3/3