foltia で動画のキャプチャを

foltia は、再生開始の頭出しのサムネイル用に自動キャプチャ取得が行われます。ですが画像サイズは小さく、また10秒間隔なので目的のフレームでないことも多々あります。

そこで、再生時にボタンを押せばキャプチャ画像が取得されるスクリプトを書いてみました。


取得したキャプチャは NAS 領域 ( /home/foltia/php/tv/nas, \\foltia\NAS ) に作るようにしています。また、画像の大きさは動画の大きさと同じです(動画が SD サイズなら SD )。



/home/foltia/php/recorded/html5player.html/home/foltia/php/recorded/mp4player.html のお好みの方、あるいは両方の末尾( </body> の直前)に以下の3行を追加します。

<button id="capture_image" type="button" data-recfolder="<?php echo $recfolderpath; ?>" data-tid="<?php echo $tid; ?>" data-mp4filename="<?php echo $filename; ?>">キャプチャ保存</button>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="capture_image.js"></script>


そして、下記の2つのファイル /home/foltia/php/recorded にアップロードします。

// capture_image.js

$(function (){
    $("#capture_image")
    .css("margin", "1ex 0 1ex 0")
    .after("<div id=\"captured_images\"></div>")
    .click(function (){
        $.ajax({
            url: "capture_image.php",
            type: "POST",
            dataType: "JSON",
            data: {
                recfolder: $(this).data("recfolder"),
                tid: $(this).data("tid"),
                mp4filename: $(this).data("mp4filename"),
                currenttime: $("video")[0].currentTime
            }
        })
        .done(function (resp){
            if(resp['status'] == 200){
                $("#captured_images").append("<img " +
                        "src=\"" + resp['image_url'] + "\" " +
                        "alt=\"" + resp['image_file'] + "\" " +
                        "title=\"" + resp['image_file'] + "\" " +
                        "height=\"200\" " +
                        "style=\"margin: 0 1ex 1ex 0;\">");
            } else{
                alert(resp['message']);
            }
        })
        .fail(function (){
        });
    });
});
<?php
// capture_image.php

$recfolder   = filter_input(INPUT_POST, "recfolder");
$tid         = filter_input(INPUT_POST, "tid");
$mp4filename = filter_input(INPUT_POST, "mp4filename");
$currenttime = filter_input(INPUT_POST, "currenttime");

$pos     = sprintf("%.3f", (float)$currenttime);
$mp4file = "{$recfolder}/{$tid}.localized/mp4/{$mp4filename}";
$mp4path = pathinfo($mp4file);
$imgfile = "{$recfolder}/nas/{$mp4path['filename']}_{$pos}.jpg";
$imgurl  = "/tv/nas/{$mp4path['filename']}_{$pos}.jpg";

$result = array(
    'status' => 200,
    'message' => "ok",
    'image_file' => $imgfile,
    'image_url' => $imgurl
);


if(file_exists($mp4file)){
    $cmd = "ffmpeg -ss {$pos} -i {$mp4file} -f image2 -vframes 1 {$imgfile}";
    exec($cmd, $out, $ret);
    if($ret == 0){
    } else{
        $result['status'] = 400;
        $result['message'] = "conversion error - {$cmd}";
    }
} else{
    $result['status'] = 404;
    $result['message'] = "mp4file is not found";
}


header("Content-Type: applicateion/json");
echo json_encode($result);
?>