Need help with code i am trying to acheive upload of videos but when they are uploaded the file names
need to be changed to have hyphens in replace of white space and still keep the exstensions.
its proving difficult because once uploaded ffmpeg needs to read the output as the new file name so it can be
placed into the database and echo'ed out later :)

Ive searched about and tried for hours now i just need a little guidance as my brain is turning to mush at the min.

<?php

if ($_POST) {
$folder = "../videos/";
$redirect = "http://www.url.com/admin/imitv.php?success"; 
$title = $_POST['title']; 
$description = $_POST['description']; 
$video_cat_id = $_POST['video_cat_id']; 
move_uploaded_file($_FILES["imagefile"]["tmp_name"], "$folder" . $_FILES["imagefile"]["name"]);
$imagefile = $_FILES['imagefile']['name'];
//VIDEO
move_uploaded_file($_FILES["file"]["tmp_name"], "$folder" . $_FILES["file"]["name"]);
$input = ("$folder" . $_FILES['file']['name']);
$new_input = pathinfo("$folder" . $_FILES['file']['name']);
$new_input=preg_replace('/[^A-Za-z0-9-]+/', '-', $new_input);
$new_video = trim($new_input); 
$output = ("$folder" .$new_video."NEW.mp4");
//rename('../videos/'.$_FILES['file']['name'], '../videos/'.$new_video.'NEW.mp4'); 

echo "Converting $input to $output<br />";
//$command = "ffmpeg -i $video_name -ab 106kb -b 2109kb -s hd720 -ar 44100 -r 29.96 $output";
$command = "ffmpeg -i $input $output";

echo "$command<br />";
echo shell_exec( $command );
echo "Converted<br />";


$con = mysql_connect("localhost","user","pass");
if (!$con) {die('Could not connect: ' . mysql_error());}
mysql_select_db("database", $con);
$sql="INSERT INTO table VALUES ('null', '$title', '$description', '$output', '$imagefile', '$video_cat_id')";
if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}
mysql_close($con);
header('Location: '.$redirect); die;
}

?>

Recommended Answers

All 2 Replies

The snippet below demonstrates the PHP 'preg replace' function that is useful for this sort of text manipulation
http://php.net/manual/en/function.preg-replace.php

$string = 'The quick brown fox jumped over the lazy dog.';
$patterns = array();              // find these
$patterns[0] = '/ /';             //// single blank space
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';

$replacements = array();          /// swap in these
$replacements[0] = '-';
$replacements[1] = 'black';
$replacements[2] = 'slow';
echo preg_replace($patterns, $replacements, $string);

Thanks Solo7 managed to get it working with this

$input = ($_FILES['file']['name']);
$patterns = array(); 
$patterns[0] = '/ /';
$patterns[1] = '/.flv/';
$patterns[2] = '/.mp4/';
$patterns[3] = '/.mov/';
$patterns[4] = '/.avi/';
$replacements = array(); 
$replacements[0] = '-';
$replacements[1] = '';
$replacements[2] = '';
$replacements[3] = '';
$replacements[4] = '';
$new_input = preg_replace($patterns, $replacements, $input);

$video_out = "" . $new_input . "-NEW.mp4";

Now I just got the small problem of getting ffmpeg to run but I should be able to get that sorted :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.