-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Implement video-length filter using ffprobe #6246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Skaronator
wants to merge
19
commits into
mikf:master
Choose a base branch
from
Skaronator:ffprobe
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
1e7457b
Implement videolength filter with ffprobe
Skaronator 4121bfa
Fix line length error
Skaronator 8856d66
Fix more lint errors
Skaronator 3c7d897
Reduce unnessecary diff
Skaronator 1a8937c
Update Readme
Skaronator 3aabb09
Redurce diff
Skaronator 56054bd
Add Headers and Retry Mechanism to ffprobe Utility
Skaronator 7e7c92e
Remove f string since they don't exist in python 3.5
Skaronator 4c848ce
Ignore .venv
Skaronator 5732ffe
Merge remote-tracking branch 'upstream/master' into ffprobe
Skaronator c64b800
Fix issue when hitting errors
Skaronator d05a284
Merge remote-tracking branch 'upstream/master' into ffprobe
Skaronator f053953
Merge remote-tracking branch 'upstream/master' into ffprobe
Skaronator 8e27342
Merge remote-tracking branch 'upstream/master' into ffprobe
Skaronator 40dff3c
fix: rebasing code correctly
Skaronator 6ebcac0
fix: format code correctly
Skaronator bd1133d
Merge remote-tracking branch 'upstream/master' into ffprobe
Skaronator a926352
Merge remote-tracking branch 'upstream/master' into ffprobe
Skaronator bddbdbb
Merge remote-tracking branch 'upstream/master' into ffprobe
Skaronator File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| archive/ | ||
| .venv | ||
|
|
||
| # Byte-compiled / optimized / DLL files | ||
| __pycache__/ | ||
|
|
||
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| # Copyright 2014-2019 Mike Fährmann | ||
| # | ||
| # This program is free software; you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License version 2 as | ||
| # published by the Free Software Foundation. | ||
|
|
||
| """Fetch Video Length before actually downloading a whole file""" | ||
|
|
||
| import subprocess | ||
| import json | ||
| import time | ||
| from datetime import timedelta | ||
| from . import util | ||
|
|
||
|
|
||
| def get_video_length(obj, url): | ||
| minimum_frames = 10 | ||
| data = None | ||
| tries = 0 | ||
| msg = "" | ||
|
|
||
| ffprobe = util.expand_path(obj.config("ffprobe-location", "ffprobe")) | ||
|
|
||
| command = [ | ||
| ffprobe, | ||
| "-v", | ||
| "quiet", | ||
| "-print_format", | ||
| "json", | ||
| "-show_format", | ||
| "-show_streams", | ||
| ] | ||
|
|
||
| if obj.headers: | ||
| for key, value in obj.headers.items(): | ||
| command.extend(["-headers", key + ": " + value]) | ||
|
|
||
| command.append(url) | ||
|
|
||
| while True: | ||
| if tries: | ||
| obj.log.warning("%s (%s/%s)", msg, tries, obj.retries+1) | ||
| if tries > obj.retries: | ||
| return False | ||
| time.sleep(tries) | ||
| tries += 1 | ||
|
|
||
| try: | ||
| result = subprocess.run( | ||
| command, | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE, | ||
| text=True, | ||
| check=True, | ||
| ) | ||
| data = json.loads(result.stdout) | ||
| except subprocess.CalledProcessError as e: | ||
| msg = "ffprobe failed: " + str(e) | ||
| continue | ||
| except json.JSONDecodeError: | ||
| msg = "Failed to decode ffprobe output as JSON" | ||
| continue | ||
|
|
||
| # A file typically contains multiple streams (video, audio, subtitle). | ||
| # Here we filter out everything that is not considered a video | ||
| video_streams = [ | ||
| float(stream["duration"]) | ||
| for stream in data["streams"] | ||
| if stream["codec_type"] == "video" and | ||
| "duration" in stream and | ||
| "avg_frame_rate" in stream and | ||
| frame_count(stream) >= minimum_frames | ||
| ] | ||
|
|
||
| if not video_streams: | ||
| obj.log.info( | ||
| "No video streams found or none with a valid duration " | ||
| "and minimum frames." | ||
| ) | ||
| return None | ||
|
|
||
| duration = timedelta(seconds=min(video_streams)) | ||
| return duration | ||
|
|
||
|
|
||
| def frame_count(stream): | ||
| """Calculates the number of frames in the video stream.""" | ||
| try: | ||
| duration = float(stream["duration"]) | ||
| avg_frame_rate = eval(stream["avg_frame_rate"]) | ||
| return int(duration * avg_frame_rate) | ||
| except (ValueError, ZeroDivisionError): | ||
| return 0 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.