Skip to content

Instantly share code, notes, and snippets.

@KennyStier
Created January 20, 2023 07:17
Show Gist options
  • Save KennyStier/df149aefc7a6084acd926eca3e84acd9 to your computer and use it in GitHub Desktop.
Save KennyStier/df149aefc7a6084acd926eca3e84acd9 to your computer and use it in GitHub Desktop.
Frigate Export Clip
#!/bin/bash
# Frigate Export Script
# By Kenny Stier (kstr.us)
# Based on https://github.com/blakeblackshear/frigate/issues/2369#issuecomment-1268359613
# Menu taken from https://stackoverflow.com/a/28326129
URL='http://192.168.0.14:5000'
OUTDIR='./archive'
mkdir -p $OUTDIR
# cams list from frigate api:
mycam=$(curl -s ${URL}/api/config |jshon -e cameras -k)
declare -a camarray=($mycam)
createmenu ()
{
select option; do # in "$@" is the default
if [ "$REPLY" -eq "$#" ];
then
echo "Exiting..."
break;
elif [ 1 -le "$REPLY" ] && [ "$REPLY" -le $(($#-1)) ];
then
echo "You selected $option"
break;
else
echo "Incorrect Input: Select a number 1-$#"
fi
done
}
echo "Choose your camera:"
createmenu "${camarray[@]}"
read -p "Enter start time (Format: Jan 1, 1980 16:20:00):" startstring
read -p "Enter end time (Format: Jan 1, 1980 18:30:00):" endstring
start=$(date +%s -d"${startstring}")
end=$(date +%s -d"${endstring}")
startname=$(date +%Y-%m-%d_%H-%M-%S -d"${startstring}")
ffmpeg -y -i ${URL}/vod/$option/start/$start/end/$end/index.m3u8 -c copy ${OUTDIR}/${option}-${startname}.mp4
@artlov
Copy link

artlov commented Jan 20, 2023

Nice expantion of my basic script :)

@KennyStier
Copy link
Author

@artlov Thanks for giving me something to start with!
Have tried to export large clips in the past to make timelapses with, this will make it much easier

@DrSpaldo
Copy link

DrSpaldo commented May 17, 2023

Let me start this with two caveats; 1) I stole/used @KennyStier's script above, who based it on @artlov's . 2) I am not a designer and this doesn't look fancy at all. But, so far it is working and is easier than using a script for those who prefer the visual aspect.

Below are the two main files;

index.html: (camera names & descriptions need to be updated)

<!DOCTYPE html>
<html>
<head>
	<title>Frigate Export</title>
	<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
	<style>
		.ui-datepicker {
			font-size: 0.8em;
		}
	</style>
</head>
<body>
	<form method="post" action="frigate-export.php">
		<label for="camera">Choose your camera:</label>
		<select name="camera" id="camera">
        <option value="camera1">Camera 1</option>
        <option value="camera2">Camera 2</option>
        <option value="camera3">Camera 3</option>
        <option value="camera4">Camera 4</option>
        <option value="camera5">Camera 5</option>
        <option value="camera6">Camera 6</option>
        <option value="camera7">Camera 7</option>
		</select>
		<br>
		<label for="start">Start date and time:</label>
		<input type="text" name="start" id="start" placeholder="Jan 1, 2023 16:20:00">
		<br>
		<label for="end">End date and time:</label>
		<input type="text" name="end" id="end" placeholder="Jan 1, 2023 18:30:00">
		<br>
		<button type="submit">Export</button>
	</form>
	<?php if (!empty($filename)): ?>
		<p>Generated file: <?php echo $filename; ?></p>
	<?php endif; ?>

	<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
	<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-ui-timepicker-addon/1.6.3/jquery-ui-timepicker-addon.min.js"></script>
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-ui-timepicker-addon/1.6.3/jquery-ui-timepicker-addon.min.css">
	<script>
		$(document).ready(function() {
			$("#start, #end").datetimepicker({
				dateFormat: "M d, yy",
				timeFormat: "HH:mm:ss",
				showAnim: "slideDown",
				changeYear: true,
				yearRange: "-100:+0",
			});
		});
	</script>
</body>
</html>

frigate-export.php: (server IP/port need to be updated)

<?php
$url = 'http://192.168.1.10:5000';
$outdir = './archive';
$filename = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $camera = $_POST['camera'];
    $startstring = $_POST['start'];
    $endstring = $_POST['end'];

    if (!empty($camera) && !empty($startstring) && !empty($endstring)) {
        $start = strtotime($startstring);
        $end = strtotime($endstring);
        $startname = date('Y-m-d_H-i-s', $start);

        $command = "ffmpeg -y -i {$url}/vod/{$camera}/start/{$start}/end/{$end}/index.m3u8 -c copy {$outdir}/{$camera}-{$startname}.mp4";

        exec($command, $output, $return_var);

        if ($return_var === 0) {
            $filename = "{$camera}-{$startname}.mp4";
            $filepath = $outdir . '/' . $filename;
            echo 'Export completed successfully. File: <a href="' . $filepath . '" target="_blank" rel="noopener noreferrer">' . $filename . '</a>';
        } else {
            echo 'Export failed.';
        }
    }
}
?>

Notes; I am running nginx in docker. The index.html & frigate-export.php files are in one of the web hosting files, I created a sub-folder called archive. This folder is where all the exported clips go into. You need to have ffmpeg installed and I also have the two attached files daterangepicker.css & daterangepicker.js due to the visual date selection.

Hopefully this makes sense and may be helpful for someone else...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment