Skip to content

Instantly share code, notes, and snippets.

@jelloleaf
Last active August 30, 2024 16:10
Show Gist options
  • Save jelloleaf/6b06f77ae424ab07d9f6b95fd633701f to your computer and use it in GitHub Desktop.
Save jelloleaf/6b06f77ae424ab07d9f6b95fd633701f to your computer and use it in GitHub Desktop.

Revisions

  1. jelloleaf revised this gist Aug 30, 2024. 1 changed file with 38 additions and 10 deletions.
    48 changes: 38 additions & 10 deletions clipper.php
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,17 @@
    <?php
    /*
    Name: ffmpeg web gui
    Author: Greg Colley
    Created: 03/12/2011
    Update: 17/10/2014
    Version: 0.2 beta
    Description: This is a modified script to take a video URL, start time, end time, and file name, and run the ffmpeg command accordingly.
    Name: remote video clipper (save video slices remotely using ffmpeg and a php webpage)
    Author: BtwAlds
    Created: 08/30/2024
    Update: 08/30/2024
    Version: 0.1 beta
    Description: Takes a video url, start, and end time, and creates a slice of the video.
    */

    function logFile($logPath = '', $logName = null, $startingLogName = null) {
    if (file_exists($logPath.$logName)) {
    $fileSize = filesize($logPath.$logName);
    // If file size is 3MB or bigger
    // If file size is 5MB or bigger
    if ($fileSize >= 3145728) {
    $newLogName = 'overflow.log';
    $log = $logPath.$newLogName;
    @@ -39,6 +39,30 @@ function logText($file, $text) {
    fclose($log);
    }

    // Function to normalize time format to HH:MM:SS
    function normalizeTime($timeInput) {
    $parts = explode(':', $timeInput);

    if (count($parts) === 1) {
    // Input is in seconds (e.g., "5" or "05")
    $seconds = intval($parts[0]);
    return sprintf('00:00:%02d', $seconds);
    } elseif (count($parts) === 2) {
    // Input is in minutes:seconds (e.g., "00:05")
    $minutes = intval($parts[0]);
    $seconds = intval($parts[1]);
    return sprintf('00:%02d:%02d', $minutes, $seconds);
    } elseif (count($parts) === 3) {
    // Input is in HH:MM:SS (e.g., "00:00:05")
    $hours = intval($parts[0]);
    $minutes = intval($parts[1]);
    $seconds = intval($parts[2]);
    return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
    }

    return '00:00:00'; // Default if something goes wrong
    }

    set_time_limit(0);
    ini_set('display_errors', 'On');

    @@ -56,6 +80,10 @@ function logText($file, $text) {
    // Validate input
    if (!empty($videoUrl) && !empty($startTime) && !empty($endTime)) {

    // Normalize start and end times
    $startTimeFormatted = normalizeTime($startTime);
    $endTimeFormatted = normalizeTime($endTime);

    // Remove all backslashes from the URL
    $scrubbedUrl = str_replace('\\', '', $videoUrl);

    @@ -64,7 +92,7 @@ function logText($file, $text) {
    $safe_outputfile = escapeshellarg($outputFile);

    // Construct the ffmpeg command
    $command = "/usr/bin/ffmpeg -i $safe_videourl -ss $startTime -to $endTime -c copy $safe_outputfile 2>&1";
    $command = "/usr/bin/ffmpeg -i $safe_videourl -ss $startTimeFormatted -to $endTimeFormatted -c copy $safe_outputfile 2>&1";

    // Execute the command
    exec($command, $output, $status);
    @@ -114,11 +142,11 @@ function listMp4Files() {
    <input type="text" name="video_url" id="video_url" required>
    </fieldset>
    <fieldset class="fieldset">
    <label for="start_time">Start Time (e.g., 00:01:00):</label>
    <label for="start_time">Start Time (e.g., 00:01:00 or 5 for 5 seconds):</label>
    <input type="text" name="start_time" id="start_time" required>
    </fieldset>
    <fieldset class="fieldset">
    <label for="end_time">End Time (e.g., 00:02:00):</label>
    <label for="end_time">End Time (e.g., 00:02:00 or 10 for 10 seconds):</label>
    <input type="text" name="end_time" id="end_time" required>
    </fieldset>
    <input type="submit" value="Trim Video">
  2. jelloleaf created this gist Aug 30, 2024.
    145 changes: 145 additions & 0 deletions clipper.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,145 @@
    <?php
    /*
    Name: ffmpeg web gui
    Author: Greg Colley
    Created: 03/12/2011
    Update: 17/10/2014
    Version: 0.2 beta
    Description: This is a modified script to take a video URL, start time, end time, and file name, and run the ffmpeg command accordingly.
    */

    function logFile($logPath = '', $logName = null, $startingLogName = null) {
    if (file_exists($logPath.$logName)) {
    $fileSize = filesize($logPath.$logName);
    // If file size is 3MB or bigger
    if ($fileSize >= 3145728) {
    $newLogName = 'overflow.log';
    $log = $logPath.$newLogName;
    $logName = $newLogName;
    $fh = fopen($log, "a") or die("can't open file");
    clearstatcache();
    chmod($log, 0777);
    clearstatcache();
    } else {
    $log = $logPath.$logName;
    }
    } else {
    $log = $logPath.$logName;
    fopen($log, 'a') or die("can't open file");
    clearstatcache();
    chmod($log, 0777);
    clearstatcache();
    }
    return array($log, $logPath, $logName);
    }

    function logText($file, $text) {
    $log = fopen($file, 'a') or die("can't open file");
    fwrite($log, $text);
    fclose($log);
    }

    set_time_limit(0);
    ini_set('display_errors', 'On');

    // Get user input
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $videoUrl = isset($_POST['video_url']) ? $_POST['video_url'] : '';
    $startTime = isset($_POST['start_time']) ? $_POST['start_time'] : '';
    $endTime = isset($_POST['end_time']) ? $_POST['end_time'] : '';

    // Automatically set output file name based on the video URL
    $parsedUrl = parse_url($videoUrl);
    $path = $parsedUrl['path'];
    $outputFile = basename($path);

    // Validate input
    if (!empty($videoUrl) && !empty($startTime) && !empty($endTime)) {

    // Remove all backslashes from the URL
    $scrubbedUrl = str_replace('\\', '', $videoUrl);

    // Safely escape the scrubbed URL for the shell command
    $safe_videourl = escapeshellarg($scrubbedUrl);
    $safe_outputfile = escapeshellarg($outputFile);

    // Construct the ffmpeg command
    $command = "/usr/bin/ffmpeg -i $safe_videourl -ss $startTime -to $endTime -c copy $safe_outputfile 2>&1";

    // Execute the command
    exec($command, $output, $status);

    // Log command and output
    list($log, $logPath, $logName) = logFile('./logs/', 'video_trim_' . date('d-m-Y') . '.log');
    logText($logPath.$logName, "Command: $command\nOutput: " . implode("\n", $output) . "\n");

    // Check if command was successful
    if ($status === 0) {
    echo "Video successfully trimmed and saved as $outputFile";
    } else {
    echo "Error trimming video. Please check the log for details.";
    }
    } else {
    echo "All fields are required.";
    }
    }

    // Function to list all mp4 files in the current directory
    function listMp4Files() {
    $files = glob('*.mp4'); // Get all mp4 files in the current directory
    return $files;
    }

    $mp4Files = listMp4Files();
    ?>

    <html>
    <head>
    <title>FFmpeg GUI</title>
    <style type="text/css">
    .clear { clear: both; }
    .error { color: #f00; font-weight: bold; padding: 10px; }
    .success { color: #45862d; font-weight: bold; padding: 10px; }
    .fieldset { clear: both; padding: 5px; }
    table { width: 100%; border-collapse: collapse; }
    th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
    th { background-color: #f2f2f2; }
    </style>
    </head>
    <body>
    <h1>FFmpeg Video Trimmer</h1>
    <form method="POST">
    <fieldset class="fieldset">
    <label for="video_url">Video URL:</label>
    <input type="text" name="video_url" id="video_url" required>
    </fieldset>
    <fieldset class="fieldset">
    <label for="start_time">Start Time (e.g., 00:01:00):</label>
    <input type="text" name="start_time" id="start_time" required>
    </fieldset>
    <fieldset class="fieldset">
    <label for="end_time">End Time (e.g., 00:02:00):</label>
    <input type="text" name="end_time" id="end_time" required>
    </fieldset>
    <input type="submit" value="Trim Video">
    </form>

    <h2>Available MP4 Files</h2>
    <table>
    <thead>
    <tr>
    <th>File Name</th>
    <th>Download Link</th>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($mp4Files as $file): ?>
    <tr>
    <td><?php echo htmlspecialchars($file); ?></td>
    <td><a href="<?php echo htmlspecialchars($file); ?>" download><?php echo htmlspecialchars($file); ?></a></td>
    </tr>
    <?php endforeach; ?>
    </tbody>
    </table>
    </body>
    </html>