xha1e
Posts: 20
Joined: Fri Apr 22, 2022 4:49 pm

cant run vlc from php

Fri Feb 23, 2024 9:39 pm

I am working on a simple video player, where a user can upload and play or stop a video. From command line it works just fine. However, from the page it does not appear to start the video.

Device is CM4, and its running pi os lite 32
Screenshot 2024-02-23 at 4.31.28 PM.png
Screenshot 2024-02-23 at 4.31.28 PM.png (128.43 KiB) Viewed 216 times
This is the php that calls the command to run the videoPlayer.sh script.
start-player-form.php:

Code: Select all

<?php

include "../includes/console-log.php";

console_log("start-player-form.php connected");


console_log($_POST['videoToPlay']);
console_log($_POST['loopVideo']);
$videoToPlay =  substr($_POST['videoToPlay'], strrpos($_POST['videoToPlay'], "\\"));
console_log($videoToPlay);
if ($_POST['videoToPlay'] == "Choose...") {
    echo '<div class="alert alert-warning alert-dismissible fade show" role="alert">
            No video file is selected.
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>';
} else {
    console_log("start-player-form.php function executed");
    $startPlayer = escapeshellcmd("sh ../commands/videoPlayer.sh start '" .  $videoToPlay . "' '" . $_POST['loopVideo'] . "'");

    $startPlayerOutput = shell_exec($startPlayer);
 
    echo '<div class="alert alert-success alert-dismissible fade show" role="alert">
            ' . $startPlayerOutput . '
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>';
}
videoPlayer.sh I can run from command line with

Code: Select all

sh /var/www/html/commands/videoPlayer.sh start big* false
The video plays just fine:

videoPlayer.sh

Code: Select all

#!/bin/bash

echo "Digital Signage Video Player Starting..."

#find vlc
var=$(ps -ef | grep vlc | grep -v grep | awk '{print $2}')

action=$1
fileName=$2
videoLoop=$3

#default path to video upload folder
path="/var/www/html/videos/"

#play video to HDMI, looped
playAndLoop="vlc --loop --fullscreen ${path}${fileName}"

#play video to HDMI, not looped
playAndExit="vlc --play-and-exit --fullscreen ${path}${fileName}"

if [ "$action" = 'start' ]; then
    if [ "$videoLoop" = 'true' ]; then
        echo "Playing Video, Loop = true"
        $playAndLoop
    fi

    if [ "$videoLoop" = 'false' ]; then
        echo "Playing Video, Loop = false"
        $playAndExit
    fi
fi

if [ "$action" = 'stop' ]; then
    echo "Stopping Video"
    sudo kill $var
fi
VLC media player 3.0.20 Vetinari
Command Line Interface initialized. Type `help' for help.
> Shutting down.
Permissions:
-rwxrwxrwx 1 pi www-data 758 Feb 23 21:35 videoPlayer.sh
-rwxrwxrwx 1 pi www-data 1076 Feb 23 21:35 start-player-form.php
drwxrwxrwx 2 pi www-data 4096 Feb 23 21:07 videos

Some similar unresolved posts i found:
https://ubuntuforums.org/showthread.php?t=2170458
https://raspberrypi.stackexchange.com/q ... m-php-file

Henrik Gill
Posts: 111
Joined: Sat Feb 24, 2024 3:23 pm

Re: cant run vlc from php

Sat Feb 24, 2024 7:52 pm

I don't know what you mean by 'can''t run vlc from php' but I do know that if you want to play a file on vlc via php then you could try this:

Code: Select all

$vlc = "C:\Program Files (x86)\VideoLan\VLC\vlc.exe"; // or whatever the path to vlc is;
$path_file = "C:\your\path\file.mp4"; // you can also retrieve that with a relative path like using realpath(relative_path);

// launch vlc
pclose(popen("start \"$vlc\" \"$path_file\"", "r"));
Let me know if this is helpful

xha1e
Posts: 20
Joined: Fri Apr 22, 2022 4:49 pm

Re: cant run vlc from php

Sat Feb 24, 2024 10:52 pm

Curious to know what part of cannot run vlc from php do you not understand?

Also, I am not using windows.

Henrik Gill
Posts: 111
Joined: Sat Feb 24, 2024 3:23 pm

Re: cant run vlc from php

Sat Feb 24, 2024 11:55 pm

Ok I think I understand now after re-reading you post. And since you aren't running windows, I got some new code:

Code: Select all

<embed 
    type="application/x-vlc-plugin" 
    pluginspage="http://www.videolan.org" 
    version="VideoLAN.VLCPlugin.2"
    width="640"
    height="480"
    target="file:///x:/vid.avi"
    id="vlc">
</embed>
I hope this is helpful to you.

Henrik Gill
Posts: 111
Joined: Sat Feb 24, 2024 3:23 pm

Re: cant run vlc from php

Sun Feb 25, 2024 1:12 pm

Now that I think about it, it might not be possible to do what you want to do. You see, PHP is a server side language. If you want to do what you want, use HTML & JavaScript. The <video> element would be perfect for what you want to do. Use this code for a video:

Code: Select all

<video width="350" height="270" controls>
<source src="whatever_file_or_use_script_so_you_upload_the_files_to_the_video_box.mp4>
</video>

Return to “Graphics, sound and multimedia”