Repository navigation
hitvid v1.0.2
hitvid Script Modification Report
Overview
The new version of the hitvid script introduces significant functional enhancements and user experience (UX) optimizations over its predecessor. Key changes include the introduction of interactive playback controls (pause, seek forward/backward, speed adjustment), optimized frame processing performance (e.g., attempting to use the RAM disk /dev/shm for temporary file storage, FFmpeg pre-scaling), considerably improved user feedback mechanisms (such as progress bars for time-consuming operations), along with several code refactorings and enhancements to error handling.
Detailed Change Points
-
New Interactive Playback Controls
- Scope of Impact: Core logic of
play_chafa_framesfunction,show_helpmessage, new global variables. - Specific Changes:
- In
play_chafa_frames, user input is read non-blockingly usingread -t. - Supported Controls:
Spacebar: Pause/Resume playback.Right Arrow Key: Seek forward (bySEEK_SECONDS, default 5 seconds).Left Arrow Key: Seek backward (bySEEK_SECONDS).Up Arrow Key: Increase playback speed.Down Arrow Key: Decrease playback speed.
- New related global variables:
PAUSED: Flags the paused state.ORIGINAL_FPS: Stores the user-defined or default original extraction frame rate.CURRENT_FPS_MULTIPLIER_INDEX: Index to control the playback speed multiplier.PLAYBACK_SPEED_MULTIPLIERS: Defines an array of available playback speed multipliers (e.g., 0.25x, 0.5x, 1.0x, 2.0x).SEEK_SECONDS: Defines the seek duration in seconds.
show_help: Added an "Interactive Controls" section to explain these new features.
- In
- Purpose: Greatly enhances user flexibility and control while watching videos.
- Scope of Impact: Core logic of
-
Performance Optimizations
- Temporary File Storage Optimization (
setup_temp_dir):- Prioritizes creating the temporary directory in
/dev/shm(tmpfs, a memory-based file system). If successful, this can significantly improve the read/write speed of frame files, especially for many small files. - If
/dev/shmis unavailable or creation fails, it falls back to the traditional/tmpdirectory.
- Prioritizes creating the temporary directory in
- FFmpeg Pre-scaling (
extract_frames):- New constants
CHAR_PIXEL_WIDTH_APPROXandCHAR_PIXEL_HEIGHT_APPROXare introduced to estimate pixel dimensions corresponding to character cells. - Based on the target display character width (
WIDTH) and height (HEIGHT), calculates target pixel dimensions for FFmpeg pre-scaling. - Adds a
scalefilter (and potentiallycropdepending onSCALE_MODE) to theffmpegcommand's-vf(video filter) option. This allows FFmpeg to scale frames to a size close to what Chafa requires during extraction. - Purpose: Reduces Chafa's scaling workload, potentially improving Chafa's rendering speed and the visual consistency of the final output.
- New constants
- Stream Mode First-Frame Pre-rendering (Main Execution Block for
streammode):- In
streammode, the script now attempts to synchronously render the first frame of the video before launching the background process to render the remaining frames. - Purpose: Reduces the waiting time for the user to see the first frame in stream mode, improving the startup experience.
- In
- Temporary File Storage Optimization (
-
User Experience (UX) Enhancements
- Progress Bar Display:
- New helper function
display_progress_baradded to generate and display text-based progress bars. extract_frames: In non-quiet mode, uses theffmpeg -progressoption to output progress information to a file. The script reads this file to display a real-time progress bar for FFmpeg frame extraction.render_all_chafa_frames_parallel: Inpreloadmode and non-quiet mode, displays a rendering progress bar by polling the number of generated Chafa text frames.play_chafa_frames: The information line during playback now includes a more intuitive progress bar showing the current playback position.
- New helper function
- Clarification of FPS Parameter Semantics:
- The help description for the
--fpscommand-line option is updated to "Set extraction frames per second." - The
ORIGINAL_FPSvariable is introduced to store this extraction frame rate. The actual frame delay during playback is calculated based on this original FPS and the current playback speed multiplier.
- The help description for the
- More Detailed Output Information:
get_video_info: Output now includes the total number of input frames in the video (if provided by ffprobe).extract_frames: Outputs the video filter options used by FFmpeg.- Richer informational messages for various stages (e.g., temporary directory location, renderer PID).
- Terminal State Restoration (
cleanup):- Added the
stty sanecommand to ensure terminal settings are restored to a "sane" known-good state upon script exit (including abnormal exits).
- Added the
- Help Message (
show_help):- Updated descriptions for
-f, --fpsand-s, --scaleoptions for better accuracy. - Added a note about how
-q, --quietmode affects interactive feedback.
- Updated descriptions for
- Progress Bar Display:
-
Functional Improvements & Refactoring
- Dependency Check (
check_dependencies):- Added a dependency check for
awk, as it's now used for floating-point arithmetic (e.g., calculating frame delay, comparing FPS values).
- Added a dependency check for
- Argument Parsing and Validation:
- The file existence check for
VIDEO_PATHnow correctly handles network stream URLs (e.g.,http*,ftp*,rtmp*) and standard input (-), for which local file existence should not be checked. - Parameter validations involving floating-point numbers (e.g.,
FPS > 0,FPS > MAX_FPS) are now implemented usingawk, which is more suitable for floating-point operations than Bash built-ins. - If an unknown option is provided, the script now shows the help message instead of directly exiting with an error.
- The file existence check for
- Chafa Options and Environment:
- The definition of the
CHAFA_OPTS_RENDERvariable is moved to the main execution flow, afterextract_framesand before callingrender_all_chafa_frames_parallelorrender_single_frame_for_xargs. - In the
render_all_chafa_frames_parallelfunction, whenxargsis used to executerender_single_frame_for_xargs, variables likeCHAFA_OPTS_RENDER,JPG_FRAMES_DIR,CHAFA_FRAMES_DIR, andQUIETare explicitlyexported to ensure sub-shells correctly inherit these environment variables.
- The definition of the
- FFmpeg Invocation (
extract_frames):- The
ffmpegcommand now includes the-nostdinoption to prevent it from accidentally consuming standard input, which is crucial for the subsequent interactive key reading.
- The
- Playback Timing (
play_chafa_frames):- The calculation of the
sleepduration after each frame now considers the actual processing time for that frame (reading file,catoutput, etc.), making the frame interval closer to the target frame rate. frame_delaycalculation is now based onORIGINAL_FPS * PLAYBACK_SPEED_MULTIPLIER.
- The calculation of the
- Error Handling:
- In stream mode, if the background renderer process (
RENDER_PID) terminates unexpectedly and the required frame file is missing, a more specific error message is displayed before exiting.
- In stream mode, if the background renderer process (
- Dependency Check (
-
Code Cleanup & Minor Adjustments
- Removed the cleanup logic for
XARGS_FIFOfrom thecleanupfunction, as it was mentioned in the old version but not actually used. - In
render_single_frame_for_xargs, when the source JPG file doesn't exist, the check forQUIETis removed, and it directly returns an error code (as warnings go to stderr, and the progress bar is the primary user feedback). - Other minor updates to comments and code formatting for improved readability.
- Removed the cleanup logic for
Conclusion
The new version of hitvid, by introducing interactive controls, optimizing performance, and improving the user interface, significantly enhances its usability and user-friendliness as a terminal video player. These modifications make the script more powerful, easier to operate, and provide a smoother experience.