mixer.cpp 4.25 KB
#include "mixer.h"

Mixer::Mixer(string mVideo, string sVideo, int ssVideo, int psVideo, int tsVideo,
 		string id, string pathTemp, string pathContents) {
	this->mainVideo = mVideo;
	this->secondaryVideo = sVideo;
	this->sizeSVideo = ssVideo;
	this->positionSVideo = psVideo;
	this->transpSVideo = tsVideo;
	this->transparency = ""; //Por enquanto, a transparência esta desativada.
	this->pathTempFiles = pathTemp;
	this->pathMixedVideo = pathContents;
	this->userID = id;
	PRINTL(util::_DEBUG, "Mixer Done!\n");
}

Mixer::~Mixer() {
	PRINTL(util::_DEBUG, "Mixer finalized!\n");
}

void Mixer::setPathOfFiles() {
	this->pathTempFiles.append("/").append(this->userID);
	this->pathMixedVideo.append("/").append(this->userID).append(".mp4");

	cout << "PATH TEMP FILES: " << this->pathTempFiles << endl;
	cout << "PATH MIXED VIDEOS: " << this->pathMixedVideo << endl;
}

/* O valor da largura e altura está na unidade de Pixels.
 * Se o valor da variável for -1, deverá manter a proporção da mesma em relação
 * à outra.
 * Ex.: 600:-1 ==> largura é 600 e a altura será calculada em relação à mesma para
 * manter proporção.
 * Ex.: -1:600 ==> altura é 600 e a largura será calculada em relação à mesma para
 * manter proporção.
 */
void Mixer::setSVSize() {
	string tempTextFile = this->pathTempFiles;
	tempTextFile.append("/").append("tamanho.txt");

	cout << "TEMPTXT: " << tempTextFile << endl;

	string ffprobeSentence = "ffprobe -show_streams "+this->mainVideo+" 2> /dev/null | grep \"height=\" | cut -d'=' -f2 > "+tempTextFile;
    system(ffprobeSentence.c_str());

    string strHeight = "324"; //Se não conseguir ler do arquivo a altura será essa...
    ifstream arq(tempTextFile.c_str()); //Abrindo arquivo com info. dos vídeos
    if (arq.is_open()) {        
        getline(arq,strHeight); //Lendo o tamanho(altura) do vídeo
        arq.close();
    }
    int height = atoi(strHeight.c_str());

    stringstream alt;
    stringstream larg;

    switch(this->sizeSVideo){
    	case SMALL:
    		this->widthSVideo = -1;
			this->heigthSVideo = (0.3*height);

			larg << this->widthSVideo;
			larg >> this->widthStr;

			alt << this->heigthSVideo;
			alt >> this->heigthStr;
			break;

		case MEDIUM:
			this->widthSVideo = -1;
			this->heigthSVideo = (0.4*height);

			larg << this->widthSVideo;
			larg >> this->widthStr;

			alt << this->heigthSVideo;
			alt >> this->heigthStr;
			break;

		case LARGE:
			this->widthSVideo = -1;
			this->heigthSVideo = (0.5*height);

			larg << this->widthSVideo;
			larg >> this->widthStr;

			alt << this->heigthSVideo;
			alt >> this->heigthStr;
			break;

		default: //Se não escolheu nenhum inteiro válido, consideramos MEDIUM como o tamanho padrão.
			this->widthSVideo = -1;
			this->heigthSVideo = (0.4*height);

			larg << this->widthSVideo;
			larg >> this->widthStr;

			alt << this->heigthSVideo;
			alt >> this->heigthStr;

			PRINTL(util::_WARNING, "Tamanho do vídeo de libras é inválido! Tamanho padrão selecionado.\n");
    }
}

void Mixer::setSVPosition() {
	switch(this->positionSVideo){
		case TOP_LEFT:
			this->positionStr = "10:10";
			break;

		case TOP_RIGHT:
			this->positionStr = "main_w-overlay_w-10:10";
			break;

		case BOTTOM_RIGHT:
			this->positionStr = "main_w-overlay_w-10:main_h-overlay_h-10";
			break;

		case BOTTOM_LEFT:
			this->positionStr = "10:main_h-overlay_h-10";
			break;

		default: //Se não escolheu nenhum inteiro válido, consideramos BOTTOM_RIGHT como a posição padrão.
			this->positionStr = "main_w-overlay_w-10:main_h-overlay_h-10";
			PRINTL(util::_WARNING, "Posição do vídeo de libras é inválido! Posição padrão selecionada.\n");
	}
}

void Mixer::initialize() {
	setPathOfFiles(); //Tem que aparecer antes de setSVSize();
	setSVSize();
	setSVPosition();
	mixVideos();
}

void Mixer::mixVideos() {
	PRINTL(util::_INFO, "Mixando...\n");
	
	string ffmpegSentence = "ffmpeg -i "+this->mainVideo+" -v quiet -y -vf \"movie="+this->secondaryVideo+", "+
        "scale="+ this->widthStr +":"+this->heigthStr+", setpts=PTS-STARTPTS, [movie] overlay"+this->transparency+"="+this->positionStr+
        " [out]\" -qscale 0 -strict experimental -vcodec libx264 -preset fast -r 30 -threads "+NUMTHREADS+" "+this->pathMixedVideo;

    cout << "FFMPEG: " << ffmpegSentence << endl;

    system(ffmpegSentence.c_str());
}