From 966d58a644a5f681af7e717861095aad180a0d61 Mon Sep 17 00:00:00 2001 From: S4NKALP Date: Sun, 20 Jul 2025 23:15:59 +0545 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=20=20refactor(screen-capture):=20enha?= =?UTF-8?q?nce=20screen-capture=20script=20targeting=20latest=20video=20by?= =?UTF-8?q?=20default?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/launcher/plugins/screencapture.py | 187 ++++++++-- scripts/screen-capture.sh | 425 ++++++++++++---------- 2 files changed, 381 insertions(+), 231 deletions(-) diff --git a/modules/launcher/plugins/screencapture.py b/modules/launcher/plugins/screencapture.py index 7608f94..3cdbb2c 100644 --- a/modules/launcher/plugins/screencapture.py +++ b/modules/launcher/plugins/screencapture.py @@ -69,14 +69,23 @@ class ScreencapturePlugin(PluginBase): # Control commands "stop": "Stop current recording", # Conversion commands - "convert-webm": "Convert recordings to WebM format", - "conv-webm": "Convert recordings to WebM format", - "convert-iphone": "Convert recordings for iPhone compatibility", - "conv-iphone": "Convert recordings for iPhone compatibility", - "convert-youtube": "Convert recordings for YouTube upload", - "conv-youtube": "Convert recordings for YouTube upload", - "convert-gif": "Convert recordings to GIF format", - "conv-gif": "Convert recordings to GIF format", + "convert-webm": "Convert latest MKV recording to WebM format", + "conv-webm": "Convert latest MKV recording to WebM format", + "convert-iphone": "Convert latest MKV recording for iPhone compatibility", + "conv-iphone": "Convert latest MKV recording for iPhone compatibility", + "convert-youtube": "Convert latest recording for YouTube upload", + "conv-youtube": "Convert latest recording for YouTube upload", + "convert-gif": "Convert latest recording to GIF format", + "conv-gif": "Convert latest recording to GIF format", + # Conversion commands with file input + "convert-webm-file": "Convert specific MKV file to WebM format", + "conv-webm-file": "Convert specific MKV file to WebM format", + "convert-iphone-file": "Convert specific MKV file for iPhone compatibility", + "conv-iphone-file": "Convert specific MKV file for iPhone compatibility", + "convert-youtube-file": "Convert specific video file for YouTube upload", + "conv-youtube-file": "Convert specific video file for YouTube upload", + "convert-gif-file": "Convert specific video file to GIF format", + "conv-gif-file": "Convert specific video file to GIF format", } def _run_script(self, *args): @@ -86,6 +95,13 @@ class ScreencapturePlugin(PluginBase): except Exception as e: print(f"Error running screen-capture script: {e}") + def _run_script_with_file(self, format_type: str, file_path: str): + """Execute the screen-capture script with file parameter.""" + try: + subprocess.Popen([self.script_path, "convert", format_type, file_path]) + except Exception as e: + print(f"Error running screen-capture script with file: {e}") + def _is_recording(self): """Check if recording is currently active.""" try: @@ -296,75 +312,166 @@ class ScreencapturePlugin(PluginBase): ), # Conversion commands "convert-webm": ( - "Convert to WebM", - "Convert MKV recordings to WebM format", + "Convert Latest to WebM", + "Convert latest MKV recording to WebM format", screenrecord, lambda: self._run_script("convert", "webm"), ), "conv-webm": ( - "Convert to WebM", - "Convert MKV recordings to WebM format", + "Convert Latest to WebM", + "Convert latest MKV recording to WebM format", screenrecord, lambda: self._run_script("convert", "webm"), ), "convert-iphone": ( - "Convert for iPhone", - "Convert recordings for iPhone compatibility", + "Convert Latest for iPhone", + "Convert latest MKV recording for iPhone compatibility", screenrecord, lambda: self._run_script("convert", "iphone"), ), "conv-iphone": ( - "Convert for iPhone", - "Convert recordings for iPhone compatibility", + "Convert Latest for iPhone", + "Convert latest MKV recording for iPhone compatibility", screenrecord, lambda: self._run_script("convert", "iphone"), ), "convert-youtube": ( - "Convert for YouTube", - "Convert recordings for YouTube upload", + "Convert Latest for YouTube", + "Convert latest recording for YouTube upload", screenrecord, lambda: self._run_script("convert", "youtube"), ), "conv-youtube": ( - "Convert for YouTube", - "Convert recordings for YouTube upload", + "Convert Latest for YouTube", + "Convert latest recording for YouTube upload", screenrecord, lambda: self._run_script("convert", "youtube"), ), "convert-gif": ( - "Convert to GIF", - "Convert video recordings to GIF", + "Convert Latest to GIF", + "Convert latest recording to GIF format", screenrecord, lambda: self._run_script("convert", "gif"), ), "conv-gif": ( - "Convert to GIF", - "Convert video recordings to GIF", + "Convert Latest to GIF", + "Convert latest recording to GIF format", screenrecord, lambda: self._run_script("convert", "gif"), ), + # File-based conversion commands (these will be handled specially) + "convert-webm-file": ( + "Convert File to WebM", + "Type filename after command (e.g., convert-webm-file video.mkv)", + screenrecord, + None, # Will be handled in query method + ), + "conv-webm-file": ( + "Convert File to WebM", + "Type filename after command (e.g., conv-webm-file video.mkv)", + screenrecord, + None, # Will be handled in query method + ), + "convert-iphone-file": ( + "Convert File for iPhone", + "Type filename after command (e.g., convert-iphone-file video.mkv)", + screenrecord, + None, # Will be handled in query method + ), + "conv-iphone-file": ( + "Convert File for iPhone", + "Type filename after command (e.g., conv-iphone-file video.mkv)", + screenrecord, + None, # Will be handled in query method + ), + "convert-youtube-file": ( + "Convert File for YouTube", + "Type filename after command (e.g., convert-youtube-file video.mkv)", + screenrecord, + None, # Will be handled in query method + ), + "conv-youtube-file": ( + "Convert File for YouTube", + "Type filename after command (e.g., conv-youtube-file video.mkv)", + screenrecord, + None, # Will be handled in query method + ), + "convert-gif-file": ( + "Convert File to GIF", + "Type filename after command (e.g., convert-gif-file video.mkv)", + screenrecord, + None, # Will be handled in query method + ), + "conv-gif-file": ( + "Convert File to GIF", + "Type filename after command (e.g., conv-gif-file video.mkv)", + screenrecord, + None, # Will be handled in query method + ), } if command in command_info: title, subtitle, icon, action = command_info[command] - return Result( - title=title, - subtitle=subtitle, - icon_markup=icon, - action=action, - relevance=1.0, - plugin_name=self.display_name, - ) + if action is not None: # Regular command + return Result( + title=title, + subtitle=subtitle, + icon_markup=icon, + action=action, + relevance=1.0, + plugin_name=self.display_name, + ) + else: # File-based command, show instruction + return Result( + title=title, + subtitle=subtitle, + icon_markup=icon, + action=lambda: None, # No action for instruction + relevance=1.0, + plugin_name=self.display_name, + ) return None def query(self, query_string: str) -> List[Result]: """Search for screencapture actions based on query.""" + # Import here to avoid circular imports + from utils.icons import screenrecord, screenshots, ssfull, ssregion, stop + # Clean the query string query = query_string.strip().lower() results = [] + # Check for file-based conversion commands with parameters + file_conversion_commands = { + "convert-webm-file": "webm", + "conv-webm-file": "webm", + "convert-iphone-file": "iphone", + "conv-iphone-file": "iphone", + "convert-youtube-file": "youtube", + "conv-youtube-file": "youtube", + "convert-gif-file": "gif", + "conv-gif-file": "gif", + } + + # Parse query for file-based commands + query_parts = query.split() + if len(query_parts) >= 2: + command = query_parts[0] + file_param = " ".join(query_parts[1:]) + + if command in file_conversion_commands: + format_type = file_conversion_commands[command] + return [Result( + title=f"Convert {file_param} to {format_type.upper()}", + subtitle=f"Convert specified file to {format_type} format", + icon_markup=screenrecord, + action=lambda fp=file_param, ft=format_type: self._run_script_with_file(ft, fp), + relevance=1.0, + plugin_name=self.display_name, + )] + # Check if query matches a command and return it as a result command_result = self._get_command_result(query) if command_result: @@ -540,32 +647,32 @@ class ScreencapturePlugin(PluginBase): results.extend( [ Result( - title="Convert to WebM", - subtitle="Convert MKV recordings to WebM format", + title="Convert Latest to WebM", + subtitle="Convert latest MKV recording to WebM format", icon_markup=screenrecord, action=lambda: self._run_script("convert", "webm"), relevance=0.01, plugin_name=self.display_name, ), Result( - title="Convert for iPhone", - subtitle="Convert recordings for iPhone compatibility", + title="Convert Latest for iPhone", + subtitle="Convert latest MKV recording for iPhone compatibility", icon_markup=screenrecord, action=lambda: self._run_script("convert", "iphone"), relevance=0.01, plugin_name=self.display_name, ), Result( - title="Convert for YouTube", - subtitle="Convert recordings for YouTube upload", + title="Convert Latest for YouTube", + subtitle="Convert latest recording for YouTube upload", icon_markup=screenrecord, action=lambda: self._run_script("convert", "youtube"), relevance=0.01, plugin_name=self.display_name, ), Result( - title="Convert to GIF", - subtitle="Convert video recordings to GIF", + title="Convert Latest to GIF", + subtitle="Convert latest recording to GIF format", icon_markup=screenrecord, action=lambda: self._run_script("convert", "gif"), relevance=0.01, diff --git a/scripts/screen-capture.sh b/scripts/screen-capture.sh index 4d3958a..a1fd899 100755 --- a/scripts/screen-capture.sh +++ b/scripts/screen-capture.sh @@ -46,12 +46,16 @@ Commands: status Check if recording is active (exit 0 if recording, 1 if not) - convert Convert recordings + convert [file] Convert recordings Formats: - webm - Convert MKV files to WebM - iphone - Convert MKV files for iPhone - youtube - Convert MKV files for YouTube (high quality) - gif - Convert MKV/MP4 files to GIF + webm - Convert latest MKV to WebM (or specify file) + iphone - Convert latest MKV to iPhone format (or specify file) + youtube - Convert latest video to YouTube format (or specify file) + gif - Convert latest video to GIF (or specify file) + + Optional [file] parameter: + - If not provided, converts the latest recorded video + - If provided, converts the specified file (full path or filename in Recordings folder) Examples: $SCRIPT_NAME screenshot selection @@ -60,7 +64,9 @@ Examples: $SCRIPT_NAME record-hq eDP-1 $SCRIPT_NAME record-gif selection $SCRIPT_NAME record stop - $SCRIPT_NAME convert gif + $SCRIPT_NAME convert gif # Convert latest video to GIF + $SCRIPT_NAME convert youtube # Convert latest video for YouTube + $SCRIPT_NAME convert webm /path/to/video.mkv # Convert specific file to WebM EOF exit 1 @@ -220,9 +226,53 @@ record_gif() { fi } +# Function to find the latest video file for conversion +find_latest_video() { + local format="$1" + local recording_dir="${HOME}/Videos/Recordings" + + case "$format" in + "webm"|"iphone") + # For webm and iphone, only look for MKV files + find "$recording_dir" -name "*.mkv" -type f -printf '%T@ %p\n' 2>/dev/null | sort -n | tail -1 | cut -d' ' -f2- + ;; + "youtube"|"gif") + # For youtube and gif, look for both MKV and MP4 files + find "$recording_dir" \( -name "*.mkv" -o -name "*.mp4" \) -type f -printf '%T@ %p\n' 2>/dev/null | sort -n | tail -1 | cut -d' ' -f2- + ;; + *) + echo "" + ;; + esac +} + +# Function to resolve video file path +resolve_video_file() { + local format="$1" + local file_param="$2" + local recording_dir="${HOME}/Videos/Recordings" + + if [ -n "$file_param" ]; then + # File parameter provided + if [ -f "$file_param" ]; then + # Full path provided and exists + echo "$file_param" + elif [ -f "$recording_dir/$file_param" ]; then + # Filename provided, exists in recordings folder + echo "$recording_dir/$file_param" + else + echo "" + fi + else + # No file parameter, find latest + find_latest_video "$format" + fi +} + # Parse command COMMAND="$1" TARGET="$2" +FILE_PARAM="$3" # Optional file parameter for convert command # Set up file paths IMG="${HOME}/Pictures/Screenshots/$(date +%Y-%m-%d_%H-%m-%s).png" @@ -413,35 +463,43 @@ case "$COMMAND" in exit 1 fi - RECORDING_DIR="${HOME}/Videos/Recordings" - CONVERTED=0 - TOTAL=0 + # Resolve the video file to convert + video_file=$(resolve_video_file "webm" "$FILE_PARAM") - for mkv_file in "${RECORDING_DIR}"/*.mkv; do - if [ -f "$mkv_file" ]; then - TOTAL=$((TOTAL + 1)) - webm_file="${mkv_file%.mkv}.webm" - - # Check if webm version doesn't already exist - if [ ! -f "$webm_file" ]; then - # Simpler ffmpeg command with basic settings - ffmpeg -y -i "$mkv_file" -c:v libvpx -b:v 1M -c:a libvorbis "$webm_file" 2>/tmp/ffmpeg_error.log - - if [ $? -eq 0 ]; then - CONVERTED=$((CONVERTED + 1)) - notify-send "Converted to WebM" "$(basename "$mkv_file")" - else - error=$(cat /tmp/ffmpeg_error.log | tail -n 5) - notify-send "Conversion Failed" "Error: $error" - fi - fi + if [ -z "$video_file" ] || [ ! -f "$video_file" ]; then + if [ -n "$FILE_PARAM" ]; then + notify-send "WebM Conversion Error" "File not found: $FILE_PARAM" + else + notify-send "WebM Conversion Error" "No MKV files found in Recordings folder" fi - done + exit 1 + fi - if [ $TOTAL -eq 0 ]; then - notify-send "WebM Conversion" "No MKV files found in Recordings folder" + # Ensure it's an MKV file + if [[ "$video_file" != *.mkv ]]; then + notify-send "WebM Conversion Error" "Only MKV files can be converted to WebM. Found: $(basename "$video_file")" + exit 1 + fi + + webm_file="${video_file%.mkv}.webm" + + # Check if webm version doesn't already exist + if [ -f "$webm_file" ]; then + notify-send "WebM Conversion Skipped" "WebM version already exists: $(basename "$webm_file")" + exit 0 + fi + + notify-send "Converting to WebM" "Processing: $(basename "$video_file")" + + # Convert the file + ffmpeg -y -i "$video_file" -c:v libvpx -b:v 1M -c:a libvorbis "$webm_file" 2>/tmp/ffmpeg_error.log + + if [ $? -eq 0 ]; then + file_size=$(du -h "$webm_file" | cut -f1) + notify-send "WebM Conversion Success" "$(basename "$video_file") → $(basename "$webm_file") ($file_size)" else - notify-send "WebM Conversion Complete" "Converted $CONVERTED out of $TOTAL MKV files" + error=$(cat /tmp/ffmpeg_error.log | tail -n 5) + notify-send "WebM Conversion Failed" "Error: $error" fi ;; @@ -452,50 +510,51 @@ case "$COMMAND" in exit 1 fi - RECORDING_DIR="${HOME}/Videos/Recordings" - CONVERTED=0 - SKIPPED_IPHONE=0 - SKIPPED_EXISTING=0 - TOTAL_FILES=0 + # Resolve the video file to convert + video_file=$(resolve_video_file "iphone" "$FILE_PARAM") - for mkv_file in "${RECORDING_DIR}"/*.mkv; do - if [ -f "$mkv_file" ]; then - TOTAL_FILES=$((TOTAL_FILES + 1)) - base_filename=$(basename "$mkv_file") - - # Skip files with "iphone" in the filename - if [[ $base_filename == *"iphone"* ]]; then - SKIPPED_IPHONE=$((SKIPPED_IPHONE + 1)) - continue - fi - - iphone_file="${mkv_file%.mkv}-iphone.mp4" - - # Check if iPhone version doesn't already exist - if [ ! -f "$iphone_file" ]; then - # Simpler ffmpeg command for iPhone compatibility - ffmpeg -y -i "$mkv_file" -vcodec h264 -acodec aac "$iphone_file" 2>/tmp/ffmpeg_error.log - - if [ $? -eq 0 ]; then - CONVERTED=$((CONVERTED + 1)) - notify-send "Converted for iPhone" "$(basename "$mkv_file")" - else - error=$(cat /tmp/ffmpeg_error.log | tail -n 5) - notify-send "Conversion Failed" "Error: $error" - fi - else - SKIPPED_EXISTING=$((SKIPPED_EXISTING + 1)) - fi + if [ -z "$video_file" ] || [ ! -f "$video_file" ]; then + if [ -n "$FILE_PARAM" ]; then + notify-send "iPhone Conversion Error" "File not found: $FILE_PARAM" + else + notify-send "iPhone Conversion Error" "No MKV files found in Recordings folder" fi - done + exit 1 + fi - if [ $TOTAL_FILES -eq 0 ]; then - notify-send "iPhone Conversion" "No MKV files found in Recordings folder" + # Ensure it's an MKV file + if [[ "$video_file" != *.mkv ]]; then + notify-send "iPhone Conversion Error" "Only MKV files can be converted for iPhone. Found: $(basename "$video_file")" + exit 1 + fi + + base_filename=$(basename "$video_file") + + # Skip files with "iphone" in the filename + if [[ $base_filename == *"iphone"* ]]; then + notify-send "iPhone Conversion Skipped" "File already appears to be iPhone format: $(basename "$video_file")" + exit 0 + fi + + iphone_file="${video_file%.mkv}-iphone.mp4" + + # Check if iPhone version doesn't already exist + if [ -f "$iphone_file" ]; then + notify-send "iPhone Conversion Skipped" "iPhone version already exists: $(basename "$iphone_file")" + exit 0 + fi + + notify-send "Converting for iPhone" "Processing: $(basename "$video_file")" + + # Convert the file + ffmpeg -y -i "$video_file" -vcodec h264 -acodec aac "$iphone_file" 2>/tmp/ffmpeg_error.log + + if [ $? -eq 0 ]; then + file_size=$(du -h "$iphone_file" | cut -f1) + notify-send "iPhone Conversion Success" "$(basename "$video_file") → $(basename "$iphone_file") ($file_size)" else - notify-send "iPhone Conversion Complete" "Converted: $CONVERTED files -Skipped (already iPhone): $SKIPPED_IPHONE files -Skipped (has iPhone version): $SKIPPED_EXISTING files -Total files checked: $TOTAL_FILES" + error=$(cat /tmp/ffmpeg_error.log | tail -n 5) + notify-send "iPhone Conversion Failed" "Error: $error" fi ;; @@ -506,73 +565,64 @@ Total files checked: $TOTAL_FILES" exit 1 fi - RECORDING_DIR="${HOME}/Videos/Recordings" - CONVERTED=0 - SKIPPED_YOUTUBE=0 - SKIPPED_EXISTING=0 - TOTAL_FILES=0 + # Resolve the video file to convert + video_file=$(resolve_video_file "youtube" "$FILE_PARAM") - # Process both MKV and MP4 files - for video_file in "${RECORDING_DIR}"/*.{mkv,mp4}; do - if [ -f "$video_file" ]; then - TOTAL_FILES=$((TOTAL_FILES + 1)) - base_filename=$(basename "$video_file") - - # Skip files already marked as YouTube uploads - if [[ $base_filename == *"youtube"* ]]; then - SKIPPED_YOUTUBE=$((SKIPPED_YOUTUBE + 1)) - continue - fi - - # Create YouTube optimized filename - youtube_file="${video_file%.*}-youtube.mp4" - - # Check if YouTube version doesn't already exist - if [ ! -f "$youtube_file" ]; then - notify-send "Converting for YouTube" "Processing: $(basename "$video_file")" - - # YouTube recommended settings: - # - H.264 codec with High profile - # - 1080p or source resolution - # - 60fps or source framerate - # - High bitrate for quality (8-12 Mbps for 1080p60) - # - AAC audio at 384kbps - # - yuv420p pixel format for compatibility - # - Keyframe interval of 2 seconds (GOP) - # - No filters to preserve original colors - - ffmpeg -y -i "$video_file" \ - -c:v libx264 \ - -profile:v high \ - -preset slow \ - -crf 18 \ - -pix_fmt yuv420p \ - -c:a aac \ - -b:a 384k \ - -movflags +faststart \ - "$youtube_file" 2>/tmp/ffmpeg_error.log - - if [ $? -eq 0 ]; then - CONVERTED=$((CONVERTED + 1)) - file_size=$(du -h "$youtube_file" | cut -f1) - notify-send "YouTube Conversion Success" "$(basename "$video_file") → $(basename "$youtube_file") ($file_size)" - else - error=$(cat /tmp/ffmpeg_error.log | tail -n 5) - notify-send "YouTube Conversion Failed" "Error converting $(basename "$video_file"): $error" - fi - else - SKIPPED_EXISTING=$((SKIPPED_EXISTING + 1)) - fi + if [ -z "$video_file" ] || [ ! -f "$video_file" ]; then + if [ -n "$FILE_PARAM" ]; then + notify-send "YouTube Conversion Error" "File not found: $FILE_PARAM" + else + notify-send "YouTube Conversion Error" "No video files found in Recordings folder" fi - done + exit 1 + fi - if [ $TOTAL_FILES -eq 0 ]; then - notify-send "YouTube Conversion" "No video files found in Recordings folder" + base_filename=$(basename "$video_file") + + # Skip files already marked as YouTube uploads + if [[ $base_filename == *"youtube"* ]]; then + notify-send "YouTube Conversion Skipped" "File already appears to be YouTube format: $(basename "$video_file")" + exit 0 + fi + + # Create YouTube optimized filename + youtube_file="${video_file%.*}-youtube.mp4" + + # Check if YouTube version doesn't already exist + if [ -f "$youtube_file" ]; then + notify-send "YouTube Conversion Skipped" "YouTube version already exists: $(basename "$youtube_file")" + exit 0 + fi + + notify-send "Converting for YouTube" "Processing: $(basename "$video_file")" + + # YouTube recommended settings: + # - H.264 codec with High profile + # - 1080p or source resolution + # - 60fps or source framerate + # - High bitrate for quality (8-12 Mbps for 1080p60) + # - AAC audio at 384kbps + # - yuv420p pixel format for compatibility + # - Keyframe interval of 2 seconds (GOP) + # - No filters to preserve original colors + + ffmpeg -y -i "$video_file" \ + -c:v libx264 \ + -profile:v high \ + -preset slow \ + -crf 18 \ + -pix_fmt yuv420p \ + -c:a aac \ + -b:a 384k \ + -movflags +faststart \ + "$youtube_file" 2>/tmp/ffmpeg_error.log + + if [ $? -eq 0 ]; then + file_size=$(du -h "$youtube_file" | cut -f1) + notify-send "YouTube Conversion Success" "$(basename "$video_file") → $(basename "$youtube_file") ($file_size)" else - notify-send "YouTube Conversion Complete" "Converted: $CONVERTED files -Skipped (already YouTube): $SKIPPED_YOUTUBE files -Skipped (has YouTube version): $SKIPPED_EXISTING files -Total files checked: $TOTAL_FILES" + error=$(cat /tmp/ffmpeg_error.log | tail -n 5) + notify-send "YouTube Conversion Failed" "Error converting $(basename "$video_file"): $error" fi ;; @@ -583,67 +633,60 @@ Total files checked: $TOTAL_FILES" exit 1 fi - RECORDING_DIR="${HOME}/Videos/Recordings" - CONVERTED=0 - SKIPPED_GIF=0 - SKIPPED_EXISTING=0 - TOTAL_FILES=0 + # Resolve the video file to convert + video_file=$(resolve_video_file "gif" "$FILE_PARAM") - # Process both MKV and MP4 files - for video_file in "${RECORDING_DIR}"/*.{mkv,mp4}; do - if [ -f "$video_file" ]; then - TOTAL_FILES=$((TOTAL_FILES + 1)) - base_filename=$(basename "$video_file") - - # Skip files already GIFs - if [[ $base_filename == *.gif ]]; then - SKIPPED_GIF=$((SKIPPED_GIF + 1)) - continue - fi - - # Create GIF filename - gif_file="${video_file%.*}.gif" - - # Check if GIF version doesn't already exist - if [ ! -f "$gif_file" ]; then - notify-send "Converting to GIF" "Processing: $(basename "$video_file")" - - # Get video dimensions for scaling - width=$(ffprobe -v error -select_streams v:0 -show_entries stream=width -of csv=s=x:p=0 "$video_file") - - # Scale down if wider than 800px to keep file size reasonable - if [ "$width" -gt 800 ]; then - scale_filter="scale=800:-1:flags=lanczos," - else - scale_filter="" - fi - - # Create high-quality GIF with optimized palette - ffmpeg -i "$video_file" \ - -vf "${scale_filter}fps=15,split[s0][s1];[s0]palettegen=max_colors=256:stats_mode=diff[p];[s1][p]paletteuse=dither=bayer:bayer_scale=5:diff_mode=rectangle" \ - -loop 0 \ - "$gif_file" 2>/tmp/ffmpeg_error.log - - if [ $? -eq 0 ]; then - CONVERTED=$((CONVERTED + 1)) - send_recording_notification "$gif_file" - else - error=$(cat /tmp/ffmpeg_error.log | tail -n 5) - notify-send "GIF Conversion Failed" "Error converting $(basename "$video_file"): $error" - fi - else - SKIPPED_EXISTING=$((SKIPPED_EXISTING + 1)) - fi + if [ -z "$video_file" ] || [ ! -f "$video_file" ]; then + if [ -n "$FILE_PARAM" ]; then + notify-send "GIF Conversion Error" "File not found: $FILE_PARAM" + else + notify-send "GIF Conversion Error" "No video files found in Recordings folder" fi - done + exit 1 + fi - if [ $TOTAL_FILES -eq 0 ]; then - notify-send "GIF Conversion" "No video files found in Recordings folder" + base_filename=$(basename "$video_file") + + # Skip files already GIFs + if [[ $base_filename == *.gif ]]; then + notify-send "GIF Conversion Skipped" "File is already a GIF: $(basename "$video_file")" + exit 0 + fi + + # Create GIF filename + gif_file="${video_file%.*}.gif" + + # Check if GIF version doesn't already exist + if [ -f "$gif_file" ]; then + notify-send "GIF Conversion Skipped" "GIF version already exists: $(basename "$gif_file")" + exit 0 + fi + + notify-send "Converting to GIF" "Processing: $(basename "$video_file")" + + # Get video dimensions for scaling + width=$(ffprobe -v error -select_streams v:0 -show_entries stream=width -of csv=s=x:p=0 "$video_file") + + # Scale down if wider than 800px to keep file size reasonable + if [ "$width" -gt 800 ]; then + scale_filter="scale=800:-1:flags=lanczos," else - notify-send "GIF Conversion Complete" "Converted: $CONVERTED files -Skipped (already GIF): $SKIPPED_GIF files -Skipped (has GIF version): $SKIPPED_EXISTING files -Total files checked: $TOTAL_FILES" + scale_filter="" + fi + + # Create high-quality GIF with optimized palette + ffmpeg -i "$video_file" \ + -vf "${scale_filter}fps=15,split[s0][s1];[s0]palettegen=max_colors=256:stats_mode=diff[p];[s1][p]paletteuse=dither=bayer:bayer_scale=5:diff_mode=rectangle" \ + -loop 0 \ + "$gif_file" 2>/tmp/ffmpeg_error.log + + if [ $? -eq 0 ]; then + file_size=$(du -h "$gif_file" | cut -f1) + notify-send "GIF Conversion Success" "$(basename "$video_file") → $(basename "$gif_file") ($file_size)" + send_recording_notification "$gif_file" + else + error=$(cat /tmp/ffmpeg_error.log | tail -n 5) + notify-send "GIF Conversion Failed" "Error converting $(basename "$video_file"): $error" fi ;;