<?php
include("config.php");
/////////mySQL Connect////////////////
mysql_connect($dbloc, $dbuser, $dbpass);
mysql_select_db($db);
if ($_COOKIE['username']){
}else{
header("Location:index.php");
}
if(@$_POST["description"]){

$userid=$_COOKIE['username'];
$description=mysql_real_escape_string(@$_POST["description"]);
$vname=mysql_real_escape_string(@$_POST["vname"]);

$date=date("F j, Y, g:i a");
$vdate=date("zHis");
$fname=$vdate. @$_FILES['name']. '.mp4';
$fname2=$vdate. @$_FILES['name'];
$genre=@$_POST["genre"];
$privacy='Public';
$picture=$vdate. '.jpeg';

////////////////////The Actual Uploading///////////////////////////////////////
$uploaddir = '';  
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);    
echo "<p>";  
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) 
{ 
 $status= "File is valid, and was successfully uploaded."; 

$file=$_FILES['userfile']['name'];

        $handle = fopen($file, "r");
        $contents = fread($handle, filesize($file));
        fclose($handle);
        $make_hexa = hexdec(bin2hex(substr($contents,strlen($contents)-3)));
        if (strlen($contents) > $make_hexa){
        $pre_duration = hexdec(bin2hex(substr($contents,strlen($contents)-$make_hexa,3))) ;
        $post_duration = $pre_duration/1000;
        $timehours = $post_duration/3600;
        $timeminutes =($post_duration % 3600)/60;
        $timeseconds = ($post_duration % 3600) % 60;
        $timehours = explode(".", $timehours);
        $timeminutes = explode(".", $timeminutes);
        $timeseconds = explode(".", $timeseconds);
        $time = $timehours[0]. ":" . $timeminutes[0]. ":" . $timeseconds[0];}

//Get allgenres id
$result = mysql_query("SHOW TABLE STATUS LIKE 'allgenres'");
$row = mysql_fetch_array($result);
$nextid = $row['Auto_increment']; 

mysql_query("INSERT INTO $genre(userid, vname, description, date, privacy, fname, downloads, likes, image, duration, nextid) VALUES('$userid', '$vname', '$description', '$date', '$privacy', '$fname', '0', '0', '$picture', '$time', '$nextid')"); 
mysql_query("INSERT INTO allgenres(userid, vname, description, date, privacy, fname, downloads, likes, image, duration, category) VALUES('$userid', '$vname', '$description', '$date', '$privacy', '$fname', '0', '0', '$picture', '$time', '$genre')"); 

//Check if already MP4
$rest = substr($_FILES['userfile']['name'], -3);
echo $rest;   

rename($_FILES['userfile']['name'], $fname);
if($rest=='mp4'){
$getvid=$_FILES['userfile']['name'];
echo exec('ffmpeg ffmpeg -version');
echo exec('ffmpeg -i '. $fname. ' -ss 00:00:14.435  -vframes 1 thumbs/'. $fname2. '.jpeg');

//Windows Version
echo exec('ffmpeg.exe ffmpeg -version');
echo exec('ffmpeg.exe -i '. $fname. ' -ss 00:00:14.435  -vframes 1 thumbs/'. $fname2. '.jpeg');



rename($fname, "songs/". $fname);
}else{
$thev=$_FILES['userfile']['name'];
echo exec('ffmpeg ffmpeg -version');
//ffmpeg -i input.flv -c:v libx264 -crf 23 -c:a libfaac -q:a 100 output.mp4
echo exec('ffmpeg -i '. $thev. ' -vcodec copy -acodec copy songs/'. $fname);
echo exec('ffmpeg -i '. $fname. ' -ss 00:00:14.435  -vframes 1 thumbs/'. $fname2. '.jpeg');

//Windows Version
echo exec('ffmpeg.exe -i '. $thev. ' -c:v libx264 -preset veryslow -qp 0 songs/'. $fname);
echo exec('ffmpeg.exe -i '. $fname. ' -ss 00:00:14.435  -vframes 1 thumbs/'. $fname2. '.jpeg');


rename($fname, "songs/". $fname);
}
}
 else {  
echo "File was not uploaded. Please try another file format.";
}  
}
//////////////////////////////////////////////////////
include('headertop.php');
?><P>
<table width=96% bgcolor=white border=0 align=center>
<td width=45% bgcolor=white width=45% align=right>
<td bgcolor=white><?php echo @$status; echo @$genre; ?>
</td></table>

<table width=96% bgcolor=white border=0 align=center><td width=45% bgcolor=white width=45% align=right>
<font size=8>Select Video:</td><td bgcolor=white><form enctype="multipart/form-data" action="upload.php" method="POST"> 

Send this file: <input name="userfile" type="file" /> <p></td>
<tr><td bgcolor=white width=45% align=right><font size=8>Video Name:</td><td bgcolor=white><input type=text name= "vname"><p></td>
<tr><td bgcolor=white width=45% align=right><font size=8>Description:</td><td bgcolor=white><textarea name = "description"></textarea><p></td>
<tr><td bgcolor=white width=45% align=right><font size=8>Category:</td><td bgcolor=white><select name=genre>
<?php
mysql_connect($dbloc, $dbuser, $dbpass);
mysql_select_db($db);
$result = mysql_query("SELECT * FROM categories"); 
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
////////////////
echo '<option value="'. $row['ctn']. '">'. $row['name']. '</option>';
}
?>
</select></td>
<tr><td bgcolor=white width=45% align=right><font size=8>Privacy:</td><td bgcolor=white><select name=privacy>
<option value="public">Public</option>
</select></td>

<tr><td bgcolor=white width=45% align=right></td><td bgcolor=white>
<input type = "submit" value="Upload" name = "submit" tabindex = "10"></form>
</td></table>
<P><br>
<?php include("headerbottom.php"); ?>