GithubHelp home page GithubHelp logo

kenjitakahashi / mpdecimate_trim Goto Github PK

View Code? Open in Web Editor NEW
19.0 3.0 3.0 16 KB

trim video clips based on mpdecimate output, keep audio synced

License: MIT License

Python 100.00%
trim video-clips ffmpeg-wrapper vaapi va-api videotoolbox

mpdecimate_trim's Introduction

Script to trim similar/duplicate fragments from video clips. While keeping audio in sync.

Note that it does not do what is often called "dropping frames" (i.e. removing them from container by replacing with PTS of a "similar enough" one from another part of the clip). It actually gets rid of them completely, making the resulting clip shorter in time.

Note also that some variables, such as mpdecimate thresholds or output codec settings, are currently hardcoded in the script. Mostly due to laziness ;-).

Usage

Needs Python3.7+ and ffmpeg.

$ mpdecimate_trim.py [--keep] [--skip SKIP] [--vaapi <render_device_filepath>] [--vaapi-decimate [render_device_filepath]] [--videotoolbox] [--videotoolbox-decimate] [--debug] <filepath>

This will take file at <filepath>, detect frames with certain similarity, re-encode it with them removed (using libx265/hevc_vaapi) and delete the original file.

The --keep switch makes it keep the original.

By default, re-encode happens even if no fragments to trim are found. This can be adjusted by setting --skip to minimum amount of remaining clip parts (e.g. <=1 is equivalent to default, 2 means 1 trimmed fragment, and so on).

The --vaapi option enables VA-API based hardware accelerated transcoding. Note that the script does not check whether supplied input and/or available GPU are capable of performing the transcode, if they are not the process will fail.

The --vaapi-decimate option enables VA-API based hardware accelerated decimate filter. If the optional device path is supplied, this device will be used. Otherwise, it will attempt to use device specified with --vaapi option. If neither device is specified, the script will fail. Note that on some older versions of ffmpeg this might fail even if VA-API transcoding works, not sure why. I have only tested this with ffmpeg>=4.4.1.

The --videotoolbox option enables Apple Video Toolbox based hardware accelerated transcoding. Note that this is super fast, but usually produces much bigger files than the CPU encoder. Only works on Apple Silicon machines and requires ffmpeg>=4.4.

The --videotoolbox-decimate option enables Apple Video Toolbox based hardware accelerated decimate filter. Note that it is often much slower than the CPU version, use only if extensive CPU use is undesirable. Only works on Apple Silicon machines and requires ffmpeg>=4.4.

The --debug flag prevents anything (both temporary and input) from getting removed, no matter if the script succeeded or not. Also enables debug loglevel for ffmpeg runs.

The ffmpeg run turned interactive!

This can happen for example when the output file already exists. It may seems like the script is stuck, but really it is just waiting for user input.

Even though the output is not visible, because it is redirected to a log file, you can still provide the input as usual. So, to confirm overwriting existing file, just type y<Enter> like you normally would.

vs_decimate?

Was a different experiment, using vapoursynth. Abandoned, because its' decimation algorithm does not fit my needs, and the whole process is also noticeably slower.

In case you want to use it for something, it needs Python3.6+ and vapoursynth with ffms2 and damb.

mpdecimate_trim's People

Contributors

kenjitakahashi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mpdecimate_trim's Issues

filter_complex_script invalid argument

Error opening file C:\Users\EZOGAM~1\AppData\Local\Temp\mpdecimate_trim.tkcnog8i. Failed to set value 'C:\Users\EZOGAM~1\AppData\Local\Temp\mpdecimate_trim.tkcnog8i' for option 'filter_complex_script': Invalid argument Error parsing global options: Invalid argument

About "mpdecimate_trim.py: error: the following arguments are required: filepath" message

Hi!
It's not an issue, but I'd like to ask, what does the script "understand" as a ? I've tried almost every type of path (with Windows python.exe), or the video filename itself, but the script demands "mpdecimate_trim.py: error: the following arguments are required: filepath".
Is Python fundamentally different with paths and filenames even from Linux? I realize it's a noob-like question from someone, who never touched Python before... :)

Thank you in advance for any response. :)

obraz

Can mpdecimate_trim create smaller sized videos that are the same length of time as the original video in order to retain all of the original audio?

I presume the following is far beyond the scope of this project, but I figured I'd ask just in case I am wrong.

Script to trim similar/duplicate fragments from video clips. While keeping audio in sync.

Does that mean only the audio associated with portions of the video that are not trimmed will be included in the video clips which mpdecimate_trim creates?

I intend to use OBS on Linux to create screencasts of my coding sessions which I will narrate. In other words, I intend to talk while I code. As a result, when I review my coding sessions I will be able to quickly be reminded that, for example, "Oh, yeah, that's what I was thinking when I wrote that line of code."

I presume that sometimes I will stop coding, yet continue narrating. Normally, in such cases, nothing would change on my monitor because I would neither be using my keyboard nor my mouse. Would mpdecimate_trim delete my narration (the audio) associated with the frames it had “mpdecimated”?

I don't want to lose any of my narration.

Is there a way to decrease the file size of my screencasts—say, by using mpdecimate_trim—yet not actually decrease the length of time of my screencasts so that all of my narration (the audio) would be synched up with the video, which would be precisely the same length as it was before mpdecimate_trim was used?

I know next to nothing about video files, but I assume my feature request would require the video player to be able to pause the video portion of a file—yet continue the audio portion of a file—at those points where mpdecimate_trim had removed duplicate video frames.

In other words, if a sequence of frames were frame A, frame B, frame C, and frame D and if all four frames were identical, and if mpdecimate_trim had removed frame B, frame C, and frame D, then I assume the video player would need to pause frame A so that sequence of frames consisting of frame A, frame B, frame C, and frame D would be replaced by sequence of frames consisting of frame A played for the first frame and then paused for the second, third, and fourth frames, yet play the audio that had been associated with frame B, frame C, and frame D.

My hunch is that this is not a feature which normal video players support. I use MPlayer which, as you might know, has a remarkable number of features. But I kind of doubt even “Mighty MPlayer” has that sort of ability.

Script calling for a nonexistent file in my temp folder?

Basically, when attempting to use this script I get this error:
Error opening file C:\Users\DownT\AppData\Local\Temp\mpdecimate_trim.qevny2ob.
Failed to set value 'C:\Users\DownT\AppData\Local\Temp\mpdecimate_trim.qevny2ob' for option 'filter_complex_script': Invalid argument
Error parsing global options: Invalid argument

Any idea how to fix this? Thank you.

A small issue with writing mpdecimate_filter into TEMP folder (I think)

That's the error I'm getting after calling python .\mpdecimate_trim.py --debug .\2021-04.mp4 :)

2023-11-18 20:56:23[I] Standard output capture: w:\Temp\mpdecimate_trim.tfljivxj\decimate.stdout.log
2023-11-18 20:56:23[I] Standard error capture: w:\Temp\mpdecimate_trim.tfljivxj\decimate.stderr.log
2023-11-18 20:56:24[I] The decimate phase took 00:00:01
2023-11-18 20:56:24[I] The filter creation phase is starting
2023-11-18 20:56:24[I] Filter definition: w:\Temp\mpdecimate_trim.tfljivxj\mpdecimate_filter
Traceback (most recent call last):
  File "X:\Videos\decim\mpdecimate_trim.py", line 199, in <module>
    write_filter()
  File "X:\Videos\decim\mpdecimate_trim.py", line 47, in a
    r = f(*args, **kwargs)
  File "X:\Videos\decim\mpdecimate_trim.py", line 181, in write_filter
    frames_to_keep, has_audio = get_frames_to_keep(mpdecimate_fn)
  File "X:\Videos\decim\mpdecimate_trim.py", line 151, in get_frames_to_keep
    for line in mpdecimate:
  File "C:\Users\Mik\AppData\Local\Programs\Python\Python310\lib\encodings\cp1250.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x88 in position 4249: character maps to <undefined>

Of course, I'm not launching Python with admin right (Windows 10 22H2).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.