phpffmpegpreg-replacedirname

Changing/Replace 1st dot in every generated jpg filename from video to remain single dot


my site is using ffmpeg to generate a thumbnail jpg for every video being uploaded. The code as below:

// get the videos uploaded
foreach ($videos as $video) {
    if ($profile_author_id == $userid || current_user_can('level_10')) {
        $imagebuttons = '<span class="edit-buttons"><span class="icon button-delete icon-cancel rad3"></span></span>';
    }

    echo '<div class="profile-video-thumb-wrapper"><div class="profile-img-thumb profile-video-thumb rad3"  id="'.$video->ID.'" style="background: url('.$video->guid.'.jpg) center no-repeat; background-size: cover;">';
    echo    $imagebuttons;

    if(get_post_meta($video->ID, 'processing', true) && !is_video_processing_running(get_post_meta($video->ID, 'processing', true))) {
        delete_post_meta($video->ID, 'processing');
        unlink(get_post_meta($video->ID, "original_file", true));
        delete_post_meta($video->ID, 'original_file');

    }
    $file_path = get_attached_file($video->ID);
    $file_path_thumb = $file_path.".jpg";
    if(!file_exists($file_path_thumb)) {
        $output = shell_exec("/usr/local/bin/ffmpeg -i $file_path");
        $videoresizeheight = get_option("videoresizeheight") ? get_option("videoresizeheight") : '400';
        $comd = "/usr/local/bin/ffmpeg -y -i \"$file_path\" -f mjpeg -vframes 1 -ss 00:00:03.000 -vf scale=".$videoresizeheight.":-1 \"$file_path_thumb\" 2>&1";
        shell_exec($comd);
    }

    if(get_post_meta($video->ID, 'processing', true)) {
        if ($profile_author_id == $userid || current_user_can('level_10')) {
            echo '<span class="video-processing rad3">'._d('this video is still processing',1269).'</span>';
            echo '<img data-original-url="'.get_template_directory_uri().'/i/video-placeholder.svg" class="mobile-ready-img rad3" alt="'.get_the_title().'" data-responsive-img-url="'.get_template_directory_uri().'/i/video-placeholder-mobile.svg" />';
        }
    } else {
        echo '<div id="'.preg_replace("/([^a-zA-Z0-9])/", "", $video->post_title).'" class="video-player-lightbox text-center hide" itemprop="video" itemscope itemtype="http://schema.org/VideoObject">';
        echo    '<meta itemprop="thumbnailUrl" content="'.$video->guid.'.jpg" />';
        echo    '<meta itemprop="contentURL" content="'.$video->guid.'" />';
        echo    '<video height="100%" width="100%" controls>';
        echo        '<source src="'.$video->guid.'" type="video/mp4">';
        echo        _d("Your browser does not support the video tag.",1270);
        echo    '</video> ';
        echo '</div>';

        echo '<a href="#'.preg_replace("/([^a-zA-Z0-9])/", "", $video->post_title).'" rel="profile-video">';
        echo    '<img src="'.$video->guid.'.jpg" class="hide" />';
        echo    '<img src="'.get_template_directory_uri().'/i/video-placeholder.svg" class="video-image-play" />';
        echo '</a>';
    }

    echo '<div class="clear"></div></div></div>'."\n";
}
if(count($videos) > 0) {
    echo '<div class="clear10"></div>';
}

This as a result generates a thumbnail jpg file for every video uploaded; but the problem occurs when the filename of jpg includes a dot brought forward from the original video file, example I upload a video with filename video.mp4, this will generate a jpg file call video.mp4.jpg. And this, my website will treat this file as none existence.

Now I need to find a solution that can generate the correct thumbnail filename while still sticking to use wordpress tags, which is changing the .mp4 to -mp4 when adding an additional .jpg extention.


Solution

  • [after 2 days of working on solution, finally I've found a solution to my usage, working perfectly for me]

    At the $file_path_thumb, I've replace it with

    dirname($file_path).'/'.preg_replace("/\./","-",basename($file_path)).".jpg";
    

    so it becomes

    $file_path_thumb = dirname($file_path).'/'.preg_replace("/\./","-",basename($file_path)).".jpg";
    

    this will enable only replacing the extra dot brought forward from the original video file in varies different video extension.

    following by changing the rest of the thumbnail display link where

    "'.$video->guid.'.jpg"
    

    to

    "'.dirname($video->guid).'/'.preg_replace("/\./","-",basename($video->guid)).'.jpg"
    

    Example: i'm uploading a video with filename somevideo.mp4 and this will not mess with the original file using ffmpeg, while generating a thumbnail photo to somevideo-mp4.jpg, and then having the display thumbnail pointing to the correct generated jpg file.