I'm new to PHP. Everything else works on my site okay but I seem to have an issue with this particular piece of code. The $efn is my file name, and I need to retrieve it from a MySQL database but this code doesn't seem to work. It works in a normal video container but not when using the if statement.
Any ideas how to fix this?
<?php
$video_container = substr($efn, -3);
if ($video_container == 'mp4') {
echo '<video preload="auto" width="640" height="264" poster="../images/<?php echo $thumb ?>" controls="controls" autoPlay="false">
<source src="<?php echo $efn ?>" type="video/mp4">
</video>';
}
else {
echo '<video height="620" width="480" controls>
<source src="<?php echo $efn ?>" type ="video/avi" />
</video>';
}
?>
You have syntax problem when printing the string. Replace the <?php echo $fn ?> section by ' . $efn . '
Like this:
$video_container = substr($efn, -3);
if ($video_container == 'mp4') {
echo '<video preload="auto" width="640" height="264" poster="../images/<?php echo $thumb ?>" controls="controls" autoPlay="false">
<source src="' . urlencode($efn) . '" type="video/mp4">
</video>';
}
else {
echo '<video height="620" width="480" controls>
<source src="' . urlencode($efn) . '" type ="video/avi" />
</video>';
}
Related
I have setup a video website but i need help to make $target_file change to the file url but it won't because it is in '' brackets and also at $stringdata
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$myFile = "/home/darlpsou/video.darlingserver.com/$target_file.html"; // or .php
$fh = fopen($myFile, 'w'); // or die("error");
$stringData = '<h1>Title</h1>
<p> </p>
<p><video tabindex="0" poster="thumb.png" autoplay="autoplay" loop="loop" controls="controls" width="842" height="417">
<source src="http://video.darlingserver.com/$target_file" type="video/mp4" />
<iframe title="YouTube video player" width="842" height="417" src="http://www.youtube.com/embed/iYuG55vOIVc" frameborder="0" allowfullscreen="allowfullscreen">
</iframe></video></p>';
fwrite($fh, $stringData);
fclose($fh);
There are various ways to do this. One would be to use sprintf:
$stringData = sprintf('<h1>Title</h1>
<p> </p>
<p><video tabindex="0" poster="thumb.png" autoplay="autoplay" loop="loop" controls="controls" width="842" height="417">
<source src="http://video.darlingserver.com/%s" type="video/mp4" />
<iframe title="YouTube video player" width="842" height="417" src="http://www.youtube.com/embed/iYuG55vOIVc" frameborder="0" allowfullscreen="allowfullscreen">
</iframe></video></p>',
urlencode($target_file)
);
Another would be to use HEREDOC syntax:
$stringData = <<< HTML
<h1>Title</h1>
<p> </p>
<p><video tabindex="0" poster="thumb.png" autoplay="autoplay" loop="loop" controls="controls" width="842" height="417">
<source src="http://video.darlingserver.com/$target_file" type="video/mp4" />
<iframe title="YouTube video player" width="842" height="417" src="http://www.youtube.com/embed/iYuG55vOIVc" frameborder="0" allowfullscreen="allowfullscreen">
</iframe></video></p>
HTML;
Yet another would be to concatenate the variable:
$stringData = '<h1>Title</h1>
<p> </p>
<p><video tabindex="0" poster="thumb.png" autoplay="autoplay" loop="loop" controls="controls" width="842" height="417">
<source src="http://video.darlingserver.com/' . urlencode($target_file) .'" type="video/mp4" />
<iframe title="YouTube video player" width="842" height="417" src="http://www.youtube.com/embed/iYuG55vOIVc" frameborder="0" allowfullscreen="allowfullscreen">
</iframe></video></p>';
This is really strings 101 so please refer to http://php.net/strings for additional clues.
I need help. I have a file "video.php"
Where it contains an html5 player.
I want instead
www.mysite.com/myvideo.mp4
Be it
Www.mysite.com/video.php?file=myvideo.mp4
I want the src of the video to change according to what is typed in the url.
video.php
<video width="480" height="320" controls>
<source src=" **Name typed in the url** " type="video/mp4">
</video>
This will work:
<video width="480" height="320" controls>
<source src="<?php echo $_GET['file']?>" type="video/mp4">
</video>
There are similar questions on StackOverflow but I'm still having trouble fixing my problem. This is what I have
<video class="video-js vjs-default-skin" width="100%" poster="sqqsdqd.jpg" data-setup='{"controls":true, "autoplay": true, "preload": "auto"}'><source src="$media['data']['videos']['standard_resolution']['url']" type="video/mp4" /></video>
I need to echo it using php but whenever I try I'm getting a syntax error. This code:
echo '<div class="pic"><img src=" ' . $media['data']['images']['standard_resolution']['url'] . '"></div>';
is working fine but I can't figure out how to do it for the video one, help is appreciated.
Thank you.
Edit:
Sorry my actual code is like this
<?php
if ($media['data']['type'] == 'image') {
echo '<div class="pic"><img src=" ' . $media['data']['images']['standard_resolution']['url'] . '"></div>';
} else {
echo '<video class="video-js vjs-default-skin" width="100%" poster="httjpg" data-setup='{"controls":true, "autoplay": true, "preload": "auto"}'> <source src=" '.$media['data']['videos']['standard_resolution']['url'].'" type="video/mp4" /></video>';
}
?>
Just try this,
<?php
echo 'yourstuff';
?>
<video class="video-js vjs-default-skin" width="100%" poster="sqqsdqd.jpg" data-setup='{"controls":true, "autoplay": true, "preload": "auto"}'><source src="<?php echo $media['data']['videos']['standard_resolution']['url'] ?>" type="video/mp4" /></video>
<?php
echo 'yourstuff';
?>
update:
<?php
if ($media['data']['type'] == 'image') {
echo '<div class="pic"><img src=" ' . $media['data']['images']['standard_resolution']['url'] . '"></div>';
} else {
?>
<video class="video-js vjs-default-skin" width="100%" poster="sqqsdqd.jpg" data-setup='{"controls":true, "autoplay": true, "preload": "auto"}'><source src="<?php echo $media['data']['videos']['standard_resolution']['url'] ?>" type="video/mp4" /></video>
<?php
}
?>
I hope this will help to acheive
This issue comes because you have bad quoting style on this line
'<video class="video-js vjs-default-skin" width="100%" poster="httjpg" data-setup='
See the apos ( single quotes ), there are several ways around this. The previous answer is one of them. Here is another
<?php
if ($media['data']['type'] == 'image') {
echo '<div class="pic"><img src=" ' . $media['data']['images']['standard_resolution']['url'] . '"></div>';
} else {
echo <<<HTML
<video class="video-js vjs-default-skin" width="100%" poster="httjpg" data-setup='{"controls":true, "autoplay": true, "preload": "auto"}'> <source src="{$media['data']['videos']['standard_resolution']['url']}" type="video/mp4" /></video>
HTML;
}
?>
Note that the closing HTML; must be on it's own line with no space before or after it. It's called a HEREDOC
I used this plugin as a starting point to create a custom meta box that allows users to select a featured video. The meta box is working great, and now I am trying to figure out how to display the video in the post. The following code displays the video:
<video controls="controls" preload="auto" width="100%" height="100%">
<source src="<?php
// Retrieves the stored value from the database
$meta_value = get_post_meta( get_the_ID(), 'meta-image', true );
// Checks and displays the retrieved value
if( !empty( $meta_value ) ) {
echo $meta_value;
} ?>" type="video/mp4" />
</video>
That's great. But I want to write a statement that says "if the post has a featured video, display it, if not display the featured thumbnail." Anyone know how to do this?
EDIT: I am getting closer. The following code almost works, but for the posts that have featured images (not videos), it displays an empty video player instead of the featured image. How can I modify the below code so that the featured images work?
<?php
$slam_featured_video = get_post_meta( get_the_ID(), 'meta-image', true );
if (isset($meta_value)) {
echo '<video controls="controls" preload="auto" width="100%" height="100%">
<source src="'. $slam_featured_video. '" type="video/mp4" />
</video>';
} elseif (empty($meta_value)) {
echo the_post_thumbnail('full');
}
?>
You almost got it!
if there is no featured video, you will get back an empty string (""). isset("") = true, so you'll still end up in the featured video block.
Just an empty string by itself will evaluate to false, so just do:
if ($meta_value) {
echo '<video controls="controls" preload="auto" width="100%" height="100%">
<source src="'. $slam_featured_video. '" type="video/mp4" />
</video>';
} elseif (empty($meta_value)) {
echo the_post_thumbnail('full');
}
After some more research and experimentation, I was able to find a solution. The following code works for me. Thanks to #manishie for setting me on the right track.
<?php
$slam_featured_video = get_post_meta( get_the_ID(), 'meta-image', true );
if (!empty($slam_featured_video)) {
echo '<video controls="controls" preload="auto" width="100%" height="100%">
<source src="'. $slam_featured_video. '" type="video/mp4" />
</video>';
} elseif (empty($slam_featured_video)) {
echo the_post_thumbnail('full');
}
?>
Right now, I've got a chunk of code that will load one of three videos -- The random play order is in the video itself.
In the code below, I'm dealing with only three videos, given all possibilities, that makes for around six possibilities when the page loads. I'll be dealing with around 12 videos on the final site and this method won't cut it given the time it would take to build each possibility (114 videos)...
Anyways, here's my code:
<div id="video_container">
<div id="video">
<video width="1060" height="596" autoplay="autoplay" loop="loop" muted="muted">
<? $videolink = get_template_directory_uri() . "/videos/";
$videos = array(
' <source src="' . $videolink . 'test-123.mp4" type="video/mp4">
<source src="' . $videolink . 'test-123.webm" type="video/webm">
<source src="' . $videolink . 'test-123.ogv" type="video/ogg">
',
' <source src="' . $videolink . 'test-132.mp4" type="video/mp4">
<source src="' . $videolink . 'test-132.webm" type="video/webm">
<source src="' . $videolink . 'test-132.ogv" type="video/ogg">
',
' <source src="' . $videolink . 'test-213.mp4" type="video/mp4">
<source src="' . $videolink . 'test-213.webm" type="video/webm">
<source src="' . $videolink . 'test-213.ogv" type="video/ogg">
',
' <source src="' . $videolink . 'test-231.mp4" type="video/mp4">
<source src="' . $videolink . 'test-231.webm" type="video/webm">
<source src="' . $videolink . 'test-231.ogv" type="video/ogg">
',
' <source src="' . $videolink . 'test-312.mp4" type="video/mp4">
<source src="' . $videolink . 'test-312.webm" type="video/webm">
<source src="' . $videolink . 'test-312.ogv" type="video/ogg">
',
' <source src="' . $videolink . 'test-321.mp4" type="video/mp4">
<source src="' . $videolink . 'test-321.webm" type="video/webm">
<source src="' . $videolink . 'test-321.ogv" type="video/ogg">
',
);
echo $videos[array_rand($videos)]; } ?>
</video>
</div>
</div>
It's pretty simple; a php array to build the list of options, then an echo to list one of the urls at random.
What I need to figure out is a way to autoplay one video, then randomly play another from a set directory as soon as it ends.
I thought that I might be able to put all of the videos into a slide show, but I'm not sure how the slider would be able to trigger each one to play when it shows...
Try shuffle() (see PHP manual: shuffle()).
You can create your array containing all 12 of your videos, call shuffle() on it, and then it will output all 12 randomly every time.
What you are trying to do cant be done in just PHP. PHP being a server-side language can only effect how the page originally loads. From there to get a series of videos to automatically play you will need to use client side code like jQuery or Javascript.
I would suggest using XML to list your video urls like this:
<Videos>
<Video>
<Name>Video Name</Name>
<URL>link to video</URL>
</Video>
<Video>
<Name>Video Name</Name>
<URL>link to video</URL>
</Video>
<Video>
<Name>Video Name</Name>
<URL>link to video</URL>
</Video>
</Videos>
Then use jQuery to load the XML file and populate the videos randomly onLoad and then set a new random video when the page loads.
Here is a link to how to load an XML file using jQuery: Jquery.Get()
In JavaScript or TypeScript create an array of source names and then create a random function.
function random(i){
return Math.rand()*100%12;
}
then use returned value to set source src
then you have to load your player if any other video was previously playing.
$("video").load();
then play.
$("video").play();
To clarify from what I understand you have 12 videos and you want to play random 3 out of them, one after another. I would use below approach if I were you,
on page load I would select 3 videos, you can use array of video names and then use "shuffle" function.
using javascript listener after each video finishes load the other video.
php code to generate javascript video array
$videos = [
"video1.mp4",
"video2.mp4",
"video3.mp4",
"video4.mp4",
"video5.mp4"
];
shuffle($videos);
echo "var videos = " . json_encode( array_slice($videos, 0, 3) );
JS block for playback
var videos = [
"http://clips.vorwaerts-gmbh.de/VfE_html5.mp4",
"http://clips.vorwaerts-gmbh.de/VfE_html5.mp4",
];
var videoId =0;
var elemVideo = document.getElementById('video');
var elemSource = document.createElement('source');
elemVideo.appendChild(elemSource);
elemVideo.addEventListener('ended',play_next,false);
function play_next(){
if(videoId==2){
elemVideo.stop();
}else{
video.pause();
elemSource.setAttribute('src', videos[videoId]);
videoId++;
elemVideo.load();
elemVideo.play();
}
}
play_next();
<video id="video" width="400" autoplay="autoplay" loop="loop" muted="muted" controls>
video not supported
</video>