GitHub - skeskinen/smartcut: Cut video files with minimal recoding

6 min read Original article ↗

Logo

Video Smartcut

PyPI - Version Discord

This tool is an open-source CLI companion to Smart Media Cutter.

smartcut can cut video files in many different formats and codecs with only minimal recoding around the cutpoints (a.k.a. "smart cut").

This means that you can cut even long video files in seconds!

In contrast, Smart Media Cutter is fully-featured linear video editor with a lot more features like: Easy to use GUI, Transcript based editing (edit videos like text documents), Automatic silence cutting, Export timeline to editors like Resolve & Premiere and a generous free tier.

Demo

Logo

Open Sourced Features

  • Efficient Cutting: smartcut only recodes around the cutpoints, preserving the majority of the original video quality.
  • Flexible Input: Supports a wide range of video/audio formats and codecs.
  • Flexible cutting: Allows for both "keep" and "cut" operations based on specified input times.
  • Audio Export: Includes all audio tracks by default with nearly lossless passthrough.

Installation

Get built binaries for Windows and Linux from releases.

Install from PyPI

# Install directly with pip
pip install smartcut

# Or use pipx for isolated installation
pipx install smartcut

# Then run from anywhere
smartcut input.mp4 output.mp4 --keep 10,20,40,50

Install from source

# Clone this repository:
git clone https://github.com/skeskinen/smartcut.git
cd smartcut
# Create python virtual environment:
python -m venv venv
source venv/bin/activate
# Install the project to venv along with required dependencies:
pip install .
# Run:
python ./smartcut input.mp4 output.mp4 --keep 10,20,40,50

Usage

The CLI requires the input and output file paths as positional arguments. You can specify the segments to keep or cut using the --keep or --cut options.

Basic Commands

  • Keep specific segments:

    smartcut.exe input.mp4 output.mp4 --keep 10,20,40,50

    This keeps the segments from 10s to 20s and from 40s to 50s.

  • Cut specific segments:

    smartcut.exe input.mp4 output.mp4 --cut 30,40,01:00,01:10

    This cuts out the segments from 30s to 40s and from 1m to 1m10s, keeping the rest.

  • Various shorthands:

  smartcut input.mp4 output.mp4 -k 10,20,40,50 # -k for --keep
  smartcut input.mp4 output.mp4 -c 30,35 # -c for --cut

  # Video start end keywords: s/start and e/end
  smartcut input.mp4 output.mp4 -k start,30,01:00,end
  smartcut input.mp4 output.mp4 -k s,30,60,e

  # Negative timestamps for counting from end of file
  smartcut input.mp4 output.mp4 -c "-5,end"          # Cut last 5 seconds
  smartcut input.mp4 output.mp4 -k 10,-10          # Keep 10s to 10s from end
  smartcut input.mp4 output.mp4 -k 0,-1:30         # Keep all but last 1m30s
  • Specify log level:

    smartcut.exe input.mp4 output.mp4 --keep 10,20 --log-level info

Audio Export

By default, all audio tracks are included with passthrough codec settings. This can be adjusted by modifying the AudioExportInfo in the script if needed.

Contributing

Contributions are welcome! All the code will be licensed under MIT license.

Any changes have to work with the closed-source GUI app as well, so please coordinate with me if you want to make significant changes. You can find me on discord most of the time.

Testing

We have ok test coverage for various video and audio formats. Video tests check that pixel values are ~unchanged. In audio testing it's harder to check if the output is the same as input, but we try our best by checking the correlation of input&output as well as absolute diff.

Some of the tests depend on components in the GUI app that are not open-source. These tests are disabled.

Some commands to run different subsets of tests:

  # Test synthetic H.264 tests only
  python smartcut_tests.py --category h264

  # Test only real-world H.264 videos
  python smartcut_tests.py --category real_world_h264

  # Test all real-world videos (all codecs)
  python smartcut_tests.py --category real_world

  # See all available categories
  python smartcut_tests.py --list-categories

Normal test run looks like this:

Skipping smc tests
test_h264_cut_on_keyframes: PASS
test_h264_smart_cut: PASS
test_h264_24_fps_long: PASS
test_h264_1080p: PASS
test_h264_multiple_cuts: PASS
test_h264_profile_baseline: PASS
test_h264_profile_main: PASS
test_h264_profile_high: PASS
test_h264_profile_high10: PASS
test_h264_profile_high422: PASS
test_h264_profile_high444: PASS
test_mp4_cut_on_keyframe: PASS
test_mp4_smart_cut: PASS
test_mp4_to_mkv_smart_cut: PASS
test_mkv_to_mp4_smart_cut: PASS
test_vp9_smart_cut: PASS
test_vp9_profile_1: PASS
test_av1_smart_cut: PASS
test_avi_smart_cut: PASS
test_flv_smart_cut: PASS
test_mov_smart_cut: PASS
test_wmv_smart_cut: PASS
test_mpg_cut_on_keyframes: PASS
test_mpg_smart_cut: PASS
test_m2ts_mpeg2_smart_cut: PASS
test_m2ts_h264_smart_cut: PASS
test_ts_smart_cut: PASS
test_night_sky: PASS
test_night_sky_to_mkv: PASS
test_sunset: PASS
test_h265_cut_on_keyframes: PASS
test_h265_smart_cut: PASS
test_h265_smart_cut_large: PASS
test_mp4_h265_smart_cut: PASS
test_vertical_transform: PASS
x265 [warning]: Source height < 720p; disabling lookahead-slices
x265 [warning]: Source height < 720p; disabling lookahead-slices
test_video_recode_codec_override: PASS
test_vorbis_passthru: PASS
test_mkv_with_video_and_audio_passthru: PASS
test_mp3_passthru: PASS
test_seeking: PASS
Tests ran in 153.6s

Acknowledgements

  • This project is part of Smart Media Cutter
  • We use PyAV to interface with ffmpeg internals in a pythonic way
  • avcut is one of better smartcut implementations and their code was useful in understanding some of the nuances

Other projects

Version History

1.6

  • Proper handling of HEVC CRA and RASL frames.
  • Other small compatibility changes.
  • Set encoded by program name to 'smartcut'.

1.5

  • Better handling of inputs where DTS is missing.
  • Code cleanups to get clean pyright output

1.4

  • Support converting from .ts to .mp4/.mkv.
  • Add support for .mkv attachments. They are copied to output when cutting a file with attachments.
  • Various small fixes to cutting correctness and corner cases.
  • Added --flaky to test suite to catch even more corner cases.
  • All tests pass with 10 different random seeds.
  • Update to PyAV 16.

1.3.3

  • Another h264 NAL detection fix

1.3.2

  • Fix memory usage issues for good with better handling of large GoPs
  • Fix an issue with certain types of h264 streams

1.3.1

  • Fix a critical issue in h265 smartcutting that caused a lot of memory usage when cutting specific types of h265 streams
  • Better h265 gop detection
  • Added h265 CRA to BLA conversion

1.3

  • Update to PyAV 15.0
  • Fixed some critical issues in h264 and h265 smartcutting
  • A lot more tests with real world videos
  • Improve command line time parameter handling
  • Preserve disposition data when cutting (forced subtitles, etc.) #14

1.2

  • #11 Allow frame number input instead of time input, by snelg

Starting from this version the Windows binaries are unsigned. This means that you'll probably get a security warning about running unsigned code. If this bothers you, you can either use older versions or run the software from source code.

1.1

  • Cut subtitle tracks
  • Add support for MPEG-2 cutting and more container formats (.flv, .mov, .wmv, .avi)
  • Add timecode format hh:mm:ss to the command line interface

1.0

  • Initial opensource release
  • Frame accurate smart cutting of most modern video codecs (h264, h265, vp9, av1)