Created
April 10, 2025 01:58
-
-
Save yunginnanet/84751bde7f4457dc472c121dbd5dfeea to your computer and use it in GitHub Desktop.
for reolink camera data extracted from SD card, pipe mp4s from sd card in order to remote host over ssh that runs ffmpeg to concatenate and encode them with nvidia cuda (nvenc)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# example filename: RecS03_DST20250409_182755_183258_8B28808_EAA218.mp4 | |
# example usage from folder: ./ffmpeg_ssh.sh 192.168.1.5 RecS03 | |
set -euo pipefail | |
IFS=$'\n\t' | |
# Configuration | |
SRC_DIR="$PWD" | |
DEST_HOST="$1" | |
DATE="$(basename "$(pwd -P)")" | |
TARG="$2" | |
DEST_FILE="/media/data/${TARG}_${DATE}.mkv" | |
TMP_LIST="$(mktemp)" | |
# Colors | |
GREEN='\033[0;32m' | |
CYAN='\033[0;36m' | |
RED='\033[0;31m' | |
YELLOW='\033[1;33m' | |
NC='\033[0m' | |
cleanup() { | |
rm -f "$TMP_LIST" | |
} | |
trap cleanup EXIT | |
echo -e "${CYAN}[*] Searching for ${TARG} videos in ${SRC_DIR}...${NC}" | |
if [[ ! -d "$SRC_DIR" ]]; then | |
echo -e "${RED}[!] Source directory $SRC_DIR not found.${NC}" | |
exit 1 | |
fi | |
mapfile -t FILES < <( | |
find "$SRC_DIR" -type f -iname "${TARG}*.mp4" \ | |
-newermt "$DATE" ! -newermt "$(date -I -d "$DATE + 1 day")" | | |
sort | |
) | |
if [[ "${#FILES[@]}" -eq 0 ]]; then | |
echo -e "${RED}[!] No files found for ${DATE}. Nothing to do.${NC}" | |
exit 1 | |
fi | |
for f in "${FILES[@]}"; do | |
printf "file '%s'\n" "$f" | |
done >"$TMP_LIST" | |
echo -e "${GREEN}[+] Found ${#FILES[@]} files. Preparing stream...${NC}" | |
# Start FFmpeg pipeline | |
{ | |
ffmpeg -hide_banner -loglevel error \ | |
-f concat -safe 0 -protocol_whitelist "file,pipe" -i "$TMP_LIST" \ | |
-c:v copy -c:a copy -f matroska - | |
} | ssh "$DEST_HOST" bash -c "' | |
set -e | |
echo -e \"${YELLOW}[*] Receiving stream... Encoding to ${DEST_FILE}${NC}\" | |
ffmpeg -hide_banner -loglevel error \ | |
-f matroska -i - \ | |
-c:v hevc_nvenc -preset fast -c:a copy -y \"$DEST_FILE\" | |
echo -e \"${GREEN}[✓] Video saved to $DEST_FILE${NC}\" | |
'" | |
echo -e "${GREEN}[✓] Operation complete.${NC}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment