├── .github
├── FUNDING.yml
├── ISSUE_TEMPLATE
│ ├── bug_report.md
│ └── feature_request.md
├── pull_request_template.md
└── workflows
│ ├── docker-deploy-scanner.yml
│ ├── docker-deploy-skipper.yml
│ ├── docker-test-build-scanner.yml
│ ├── docker-test-build-skipper.yml
│ └── python-lint-test.yml
├── .gitignore
├── Dockerfile_Jellyfin-Intro-Scanner
├── Dockerfile_Jellyfin-Intro-Skipper
├── LICENSE
├── README.md
├── config
├── data
│ ├── fingerprints
│ │ └── placeholder
│ └── jellyfin_cache
│ │ └── placeholder
└── path_map.txt
├── decode.py
├── diff_jellyfin_cache.py
├── docs
├── _config.yml
├── docker.md
├── index.md
├── troubleshooting.md
└── usage.md
├── ffmpeg_fingerprint.py
├── jellyfin.py
├── jellyfin_api_client.py
├── jellyfin_auto_skip.py
├── jellyfin_queries.py
├── requirements.txt
├── tox.ini
└── unused
└── ytube_scrape.py
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | github: [mueslimak3r]
4 | patreon: # Replace with a single Patreon username
5 | open_collective: # Replace with a single Open Collective username
6 | ko_fi: # Replace with a single Ko-fi username
7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9 | liberapay: # Replace with a single Liberapay username
10 | issuehunt: # Replace with a single IssueHunt username
11 | otechie: # Replace with a single Otechie username
12 | lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
13 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
14 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Describe the bug**
11 | A clear and concise description of what the bug is.
12 |
13 | **To Reproduce**
14 | Steps to reproduce the behavior:
15 | 1. Go to '...'
16 | 2. Click on '....'
17 | 3. Scroll down to '....'
18 | 4. See error
19 |
20 | **Expected behavior**
21 | A clear and concise description of what you expected to happen.
22 |
23 | **Additional context**
24 | Add any other context about the problem here.
25 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Suggest an idea for this project
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Is your feature request related to a problem? Please describe.**
11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12 |
13 | **Describe the solution you'd like**
14 | A clear and concise description of what you want to happen.
15 |
16 | **Describe alternatives you've considered**
17 | A clear and concise description of any alternative solutions or features you've considered.
18 |
19 | **Additional context**
20 | Add any other context or screenshots about the feature request here.
21 |
--------------------------------------------------------------------------------
/.github/pull_request_template.md:
--------------------------------------------------------------------------------
1 |
4 |
5 | **Changes**
6 |
7 |
8 | **Issues**
9 |
11 |
--------------------------------------------------------------------------------
/.github/workflows/docker-deploy-scanner.yml:
--------------------------------------------------------------------------------
1 | name: Deploy Jellyfin Intro Scanner
2 |
3 | on:
4 | push:
5 | branches:
6 | - master
7 | paths:
8 | - '**.py'
9 | - 'Dockerfile_Jellyfin-Intro-Scanner'
10 | - '!jellyfin_auto_skip.py'
11 | - '!diff_jellyfin_cache.py'
12 | - '!Dockerfile_Jellyfin-Intro-Skipper'
13 | - '!**.md'
14 | - '!**.gitignore'
15 | - '!docs/**'
16 | - '!.github/**'
17 |
18 | env:
19 | IMAGE_NAME_SCANNER: jellyfin-intro-scanner
20 | REGISTRY: ghcr.io
21 |
22 | jobs:
23 | Scanner_Deploy:
24 | runs-on: ubuntu-latest
25 | permissions:
26 | contents: read
27 | packages: write
28 |
29 | steps:
30 | - name: Convert Names to Lowercase
31 | run: |
32 | echo LOWERCASE_REPOSITORY_OWNER_NAME=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV
33 | - name: Checkout repository
34 | uses: actions/checkout@v3
35 |
36 | - name: Log in to the Container registry
37 | uses: docker/login-action@v1
38 | with:
39 | registry: ${{ env.REGISTRY }}
40 | username: ${{ github.actor }}
41 | password: ${{ secrets.GITHUB_TOKEN }}
42 |
43 | - name: Extract metadata (tags, labels) for Docker
44 | id: meta
45 | uses: docker/metadata-action@v2
46 | with:
47 | images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_SCANNER }}
48 |
49 | - name: Build and push Docker image
50 | uses: docker/build-push-action@v2
51 | with:
52 | context: .
53 | push: true
54 | tags: ${{ env.REGISTRY }}/${{ env.LOWERCASE_REPOSITORY_OWNER_NAME }}/${{ env.IMAGE_NAME_SCANNER }}:latest
55 | labels: ${{ steps.meta.outputs.labels }}
56 | file: Dockerfile_Jellyfin-Intro-Scanner
--------------------------------------------------------------------------------
/.github/workflows/docker-deploy-skipper.yml:
--------------------------------------------------------------------------------
1 | name: Deploy Jellyfin Intro Auto Skipper
2 |
3 | on:
4 | push:
5 | branches:
6 | - master
7 | paths:
8 | - 'jellyfin_auto_skip.py'
9 | - 'Dockerfile_Jellyfin-Intro-Skipper'
10 |
11 | env:
12 | IMAGE_NAME_SKIPPER: jellyfin-intro-skipper
13 | REGISTRY: ghcr.io
14 |
15 | jobs:
16 | Skipper_Deploy:
17 | runs-on: ubuntu-latest
18 | permissions:
19 | contents: read
20 | packages: write
21 |
22 | steps:
23 | - name: Convert Names to Lowercase
24 | run: |
25 | echo LOWERCASE_REPOSITORY_OWNER_NAME=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV
26 | - name: Checkout repository
27 | uses: actions/checkout@v3
28 |
29 | - name: Log in to the Container registry
30 | uses: docker/login-action@v1
31 | with:
32 | registry: ${{ env.REGISTRY }}
33 | username: ${{ github.actor }}
34 | password: ${{ secrets.GITHUB_TOKEN }}
35 |
36 | - name: Extract metadata (tags, labels) for Docker
37 | id: meta
38 | uses: docker/metadata-action@v2
39 | with:
40 | images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_SKIPPER }}
41 |
42 | - name: Build and push Docker image
43 | uses: docker/build-push-action@v2
44 | with:
45 | context: .
46 | push: true
47 | tags: ${{ env.REGISTRY }}/${{ env.LOWERCASE_REPOSITORY_OWNER_NAME }}/${{ env.IMAGE_NAME_SKIPPER }}:latest
48 | labels: ${{ steps.meta.outputs.labels }}
49 | file: Dockerfile_Jellyfin-Intro-Skipper
--------------------------------------------------------------------------------
/.github/workflows/docker-test-build-scanner.yml:
--------------------------------------------------------------------------------
1 | name: Docker Test Build Jellyfin Intro Scanner
2 |
3 | on:
4 | pull_request:
5 | paths:
6 | - '**.py'
7 | - 'Dockerfile_Jellyfin-Intro-Scanner'
8 | - '!jellyfin_auto_skip.py'
9 | - '!diff_jellyfin_cache.py'
10 | - '!Dockerfile_Jellyfin-Intro-Skipper'
11 | - '!**.md'
12 | - '!**.gitignore'
13 | - '!docs/**'
14 | - '!.github/**'
15 |
16 | env:
17 | IMAGE_NAME_Scanner: jellyfin-intro-scanner-test
18 |
19 | jobs:
20 | Test_Build_Scanner:
21 | runs-on: ubuntu-latest
22 | steps:
23 | - uses: actions/checkout@v2
24 | - name: Build image
25 | run: docker build -f Dockerfile_Jellyfin-Intro-Scanner -t jellyfin-intro-scanner-test .
--------------------------------------------------------------------------------
/.github/workflows/docker-test-build-skipper.yml:
--------------------------------------------------------------------------------
1 | name: Docker Image Test Build Jellyfin Intro Auto Skipper
2 |
3 | on:
4 | pull_request:
5 | paths:
6 | - 'jellyfin_auto_skip.py'
7 | - 'Dockerfile_Jellyfin-Intro-Skipper'
8 |
9 | env:
10 | IMAGE_NAME_Skipper: jellyfin-intro-skipper-test
11 |
12 | jobs:
13 | Test_Build_Skipper:
14 | runs-on: ubuntu-latest
15 | steps:
16 | - uses: actions/checkout@v2
17 | - name: Build image
18 | run: docker build -f Dockerfile_Jellyfin-Intro-Skipper -t jellyfin-intro-skipper-test .
--------------------------------------------------------------------------------
/.github/workflows/python-lint-test.yml:
--------------------------------------------------------------------------------
1 | name: Python Lint Test
2 |
3 | on:
4 | push:
5 | paths:
6 | - '**.py'
7 |
8 | jobs:
9 | Python_Lint_Test:
10 | runs-on: ubuntu-latest
11 | steps:
12 | - uses: actions/checkout@v2
13 | - name: Set up Python 3.10.2
14 | uses: actions/setup-python@v2
15 | with:
16 | python-version: 3.10.2
17 | - name: Install dependencies
18 | run: |
19 | python -m pip install --upgrade pip
20 | python -m pip install wheel
21 | pip install flake8
22 | pip install pylint
23 | if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
24 | - name: Lint with flake8
25 | run: |
26 | flake8 . --count --ignore=E501,E116,W293,W605,F841 --max-complexity=30 --builtins="_" --show-source --statistics
27 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea
2 | __pycache__/
3 | *.py[cod]
4 | *.json
5 | *.txt
6 | *.DS_STORE
7 | .vscode
8 | cred.json
9 | venv
10 | config/data
--------------------------------------------------------------------------------
/Dockerfile_Jellyfin-Intro-Scanner:
--------------------------------------------------------------------------------
1 | FROM python:3.10
2 | COPY . /app
3 | RUN apt-get update
4 | RUN apt-get install -y ffmpeg libsm6 libxext6
5 | RUN pip install --no-cache-dir --upgrade pip
6 | RUN pip install --no-cache-dir wheel
7 | RUN pip install --no-cache-dir -r /app/requirements.txt
8 | WORKDIR /app
9 | ENTRYPOINT ["python"]
10 | CMD [ "-u", "jellyfin.py", "-j", "-v", "-l" ]
11 |
12 | VOLUME /app/config
13 |
--------------------------------------------------------------------------------
/Dockerfile_Jellyfin-Intro-Skipper:
--------------------------------------------------------------------------------
1 | FROM python:3.10
2 | COPY . /app
3 | RUN apt-get update
4 | RUN apt-get install -y ffmpeg libsm6 libxext6
5 | RUN pip install --no-cache-dir --upgrade pip
6 | RUN pip install --no-cache-dir wheel
7 | RUN pip install --no-cache-dir -r /app/requirements.txt
8 | WORKDIR /app
9 | ENTRYPOINT ["python"]
10 | CMD [ "-u", "jellyfin_auto_skip.py", "-a", "-c", "2" ]
11 |
12 | VOLUME /app/config
13 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # TV Intro Detection
2 |
3 | This project tries to detect intros of tv series by comparing pairs of episodes to find the largest common subset of frames.
4 |
5 | For setup details you can find the documentation here:
6 |
7 | https://mueslimak3r.github.io/tv-intro-detection/
8 |
9 | ### Notice
10 | `path_map.txt` and env `PATH_MAP` now use `::` to delimit path maps instead of `:`. This allows handling Windows paths
11 |
12 | ### How the script compares videos
13 | Each frame from the first quarter of each episode is extracted and a hash (https://pypi.org/project/ImageHash/) is made on the frame. Each frame hash is added to a long video hash.
14 | In pairs the longest identical string is searched from the two video hashes.
15 | Assumption: this is the intro
16 |
17 | ### How the script finds your videos
18 | When using jellyfin.py, the script queries the jellyfin server and is sent a list of all the tv shows in your library. With that list of shows, it then queries the jellyfin server for the seasons and episodes for each of the shows. Included in the results of those queries is the location on the filesystem for each item (show, season, episode).
19 |
20 | To account for jellyfin potentially using a different filesystem path to access the media than the system running the script would (eg: running in docker, running the script on a separate computer with the media drives mounted as network shares), path mapping is used to translate the path jellyfin sees into a path that the script can use.
21 |
22 | When running decode.py on its own, the -i parameter is used to specify a folder that contains video files. decode.py doesn't do any searching beyond that single folder, so typically the provided folder will be a "season folder" that contains all the video files for that season of a show.
23 |
24 | Individual shows or seasons can be ignored by creating an empty file named `.ignore-intros` inside its folder.
25 |
26 | ### Examples
27 | scan your jellyfin library, store the result in json, verbose logging enabled, logging debug output to file enabled
28 |
29 | `export JELLYFIN_URL="https://myurl" && export JELLYFIN_USERNAME="myusername" && export JELLYFIN_PASSWORD='mypassword'`
30 |
31 | `jellyfin.py -j -v -l`
32 |
33 | or in reverse order
34 |
35 | `jellyfin.py -j -v -l --reverse`
36 |
37 | monitor your jellyfin sessions and automatically skip intros using the stored json data
38 |
39 | `export JELLYFIN_URL="https://myurl" && export JELLYFIN_USERNAME="myusername" && export JELLYFIN_PASSWORD='mypassword'`
40 |
41 | `jellyfin_auto_skip.py`
42 |
43 | manually scan a directory containing at least 2 video files, debug logging enabled, logging debug output to file enabled, delete fingerprint data afterward
44 | `decode.py -i /path/to/tv/season -d -l -c`
45 |
46 | make the script aware of your host:container path mapping by editing `path_map.txt`
47 |
48 | ```
49 | # use this file if you run jellyfin in a container
50 | # examples:
51 | #
52 | # linux client, Jellyfin on linux
53 | #
54 | # /host/system/tv/path::/jellyfin/container/tv/path
55 | #
56 | # or
57 | # Windows client, Jellyfin on Windows
58 | #
59 | # X:\some\path\TV::Y:\some\path\TV
60 | #
61 | # or
62 | # Windows client, Jellyfin on linux
63 | #
64 | # or
65 | # X:\some\path\TV::/jellyfin/container/tv/path
66 | ```
67 |
--------------------------------------------------------------------------------
/config/data/fingerprints/placeholder:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mueslimak3r/tv-intro-detection/afc397bf12b7e072b0e3a781c9d7a42e8c5dee79/config/data/fingerprints/placeholder
--------------------------------------------------------------------------------
/config/data/jellyfin_cache/placeholder:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mueslimak3r/tv-intro-detection/afc397bf12b7e072b0e3a781c9d7a42e8c5dee79/config/data/jellyfin_cache/placeholder
--------------------------------------------------------------------------------
/config/path_map.txt:
--------------------------------------------------------------------------------
1 | # use this file if you run jellyfin in a container
2 | # examples:
3 | #
4 | # linux client, Jellyfin on linux
5 | #
6 | # /host/system/tv/path::/jellyfin/container/tv/path
7 | #
8 | # or
9 | # Windows client, Jellyfin on Windows
10 | #
11 | # X:\some\path\TV::Y:\some\path\TV
12 | #
13 | # or
14 | # Windows client, Jellyfin on linux
15 | #
16 | # or
17 | # X:\some\path\TV::/jellyfin/container/tv/path
18 |
--------------------------------------------------------------------------------
/decode.py:
--------------------------------------------------------------------------------
1 | import os
2 | import re
3 | import sys
4 | import getopt
5 | import shutil
6 | import json
7 | import cv2
8 | import hashlib
9 | import pandas
10 | import imagehash
11 |
12 | from math import floor
13 | from datetime import datetime, timedelta
14 | from pathlib import Path
15 |
16 | from ffmpeg_fingerprint import get_fingerprint_ffmpeg
17 |
18 | FIRST = 0
19 | SECOND = 1
20 | BOTH = 2
21 |
22 | config_path = Path(os.environ['CONFIG_DIR']) if 'CONFIG_DIR' in os.environ else Path(Path.cwd() / 'config')
23 | data_path = Path(os.environ['DATA_DIR']) if 'DATA_DIR' in os.environ else Path(config_path / 'data')
24 |
25 | preroll_seconds = 0 # adjust the end time to return n seconds prior to the calculated end time
26 | # jellyfin_auto_skip.py also handles pre-roll so adjust it there
27 | # adjusting it here bakes the pre-rolled value into the result
28 | max_fingerprint_mins = 10
29 | min_intro_length_sec = 10
30 | max_intro_length_sec = 180
31 | workers = 4 # number of executors to use
32 | target_image_height = 180 # scale frames to height of 180px
33 |
34 | check_frame = 1 # check_frame should be 1 if hash_fps is lower than video fps
35 | hash_fps = 2
36 |
37 | session_timestamp = datetime.now().strftime("%Y_%m_%d-%I_%M_%S_%p")
38 |
39 | revision_id = 2.0
40 |
41 |
42 | def print_debug(a=[], log=True, log_file=False):
43 | # Here a is the array holding the objects
44 | # passed as the argument of the function
45 | output = ' '.join([str(elem) for elem in a])
46 | if log:
47 | print(output, file=sys.stderr)
48 | if log_file:
49 | log_path = config_path / 'logs'
50 | log_path.mkdir(parents=True, exist_ok=True)
51 | with (log_path / ('log_%s.txt' % session_timestamp)).open('a') as logger:
52 | logger.write(output + '\n')
53 |
54 |
55 | def dict_by_value(dict, value):
56 | for name, age in dict.items():
57 | if age == value:
58 | return name
59 |
60 |
61 | def write_fingerprint(path, fingerprint):
62 | path = Path(data_path / 'fingerprints' / replace(path) / 'fingerprint.txt')
63 | with path.open('w+') as text_file:
64 | text_file.write(fingerprint)
65 |
66 |
67 | def replace(s):
68 | return re.sub('[^A-Za-z0-9]+', '', s)
69 |
70 |
71 | def print_timestamp(name, start_frame, end_frame, fps, log_level, log_file):
72 | start_time = 0 if start_frame == 0 else round(start_frame / fps)
73 | end_time = 0 if end_frame == 0 else round(end_frame / fps)
74 |
75 | print_debug(a=['[%s] has start %s end %s' % (name, str(timedelta(seconds=start_time)).split('.')[0], str(timedelta(seconds=end_time)).split('.')[0])], log=log_level > 0, log_file=log_file)
76 |
77 |
78 | def get_timestamp_from_frame(profile):
79 | start_time = 0 if profile['start_frame'] == 0 else profile['start_frame'] / profile['fps']
80 | end_time = 0 if profile['end_frame'] == 0 else profile['end_frame'] / profile['fps']
81 |
82 | profile['start_time_ms'] = floor(start_time * 1000)
83 | profile['end_time_ms'] = floor(end_time * 1000)
84 | profile['start_time'] = str(timedelta(seconds=floor(start_time))).split('.')[0]
85 | profile['end_time'] = str(timedelta(seconds=floor(end_time))).split('.')[0]
86 |
87 |
88 | def create_video_fingerprint(profile, hashfps, log_level, log_file):
89 | video_fingerprint = []
90 |
91 | quarter_frames_or_first_X_mins = min(floor(((profile['total_frames'] / profile['fps']) / 4) * hashfps), floor(max_fingerprint_mins * 60 * hashfps))
92 | video_fingerprint = get_fingerprint_ffmpeg(profile['Path'], hashfps, quarter_frames_or_first_X_mins, log_level, log_file, session_timestamp)
93 |
94 | return video_fingerprint
95 |
96 |
97 | def get_equal_frames(print1, print2, start1, start2):
98 | equal_frames = []
99 |
100 | search_range = floor(min(len(print1), len(print2)) / check_frame)
101 |
102 | for j in range(0, search_range):
103 | frame1 = print1[j * check_frame]
104 | frame2 = print2[j * check_frame]
105 | if frame1 - frame2 < 8:
106 | equal_frames.append((int(start1 + (j * check_frame)), int(start2 + (j * check_frame))))
107 | return equal_frames
108 |
109 |
110 | def get_start_end(print1, print1_fps, print2, print2_fps, log_level):
111 | if not print1 or not print2:
112 | return (0, 0), (0, 0)
113 |
114 | shortest_len = min(len(print1), len(print2))
115 |
116 | if len(print2) == shortest_len:
117 | longest = print1
118 | shortest = print2
119 | swap = False
120 | else:
121 | longest = print2
122 | shortest = print1
123 | swap = True
124 | print_debug(a=['swapped %s' % swap], log=log_level > 1)
125 |
126 | highest_equal_frames = []
127 | for k in range(1, len(longest)):
128 | equal_frames = get_equal_frames(longest[-k:], shortest, len(longest) - k, 0)
129 | if equal_frames and len(equal_frames) > 1:
130 | tmp_duration = equal_frames[-1][0] - equal_frames[0][0]
131 | if len(equal_frames) > len(highest_equal_frames) and tmp_duration >= 0 and tmp_duration < len(shortest):
132 | highest_equal_frames = equal_frames
133 |
134 | if k < len(shortest):
135 | equal_frames = get_equal_frames(longest, shortest[k:], 0, k)
136 | if equal_frames and len(equal_frames) > 1:
137 | tmp_duration = equal_frames[-1][0] - equal_frames[0][0]
138 | if len(equal_frames) > len(highest_equal_frames) and tmp_duration >= 0 and tmp_duration < len(shortest):
139 | highest_equal_frames = equal_frames
140 |
141 | if highest_equal_frames:
142 | if swap:
143 | return (floor(highest_equal_frames[0][1] * (print1_fps / hash_fps)), floor(highest_equal_frames[-1][1] * (print1_fps / hash_fps))), (floor(highest_equal_frames[0][0] * (print2_fps / hash_fps)), floor(highest_equal_frames[-1][0] * (print2_fps / hash_fps)))
144 | else:
145 | return (floor(highest_equal_frames[0][0] * (print1_fps / hash_fps)), floor(highest_equal_frames[-1][0] * (print1_fps / hash_fps))), (floor(highest_equal_frames[0][1] * (print2_fps / hash_fps)), floor(highest_equal_frames[-1][1] * (print2_fps / hash_fps)))
146 | else:
147 | return (0, 0), (0, 0)
148 |
149 |
150 | def read_fingerprint(fingerprint_str, log_level, log_file):
151 | fingerprint = []
152 | if fingerprint_str != '' and len(fingerprint_str) % 16 == 0:
153 | try:
154 | for ndx in range(0, floor(len(fingerprint_str) / 16)):
155 | fingerprint.append(imagehash.hex_to_hash(fingerprint_str[ndx * 16:ndx * 16 + 16]))
156 | print_debug(a=['fingerprint looks valid'], log=log_level > 1, log_file=log_file)
157 | except BaseException as err:
158 | print_debug(a=['error reading season fingerprint'], log=log_level > 0, log_file=log_file)
159 | fingerprint = []
160 | return fingerprint
161 |
162 |
163 | def read_fingerprint_file(file, log_level, log_file):
164 | if not Path(file).exists():
165 | return []
166 |
167 | fingerprint = []
168 | print_debug(a=['loading existing fingerprint [%s]' % file], log=log_level > 1, log_file=log_file)
169 | with Path(file).open('r') as text_file:
170 | fingerprint_str = text_file.read()
171 | fingerprint = read_fingerprint(fingerprint_str, log_level, log_file)
172 | return fingerprint
173 |
174 |
175 | def get_or_create_fingerprint(profile, log_level, log_file):
176 | start = datetime.now()
177 | video = cv2.VideoCapture(profile['Path'])
178 | fps = video.get(cv2.CAP_PROP_FPS)
179 |
180 | profile['fps'] = fps
181 | profile['total_frames'] = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
182 | video.release()
183 |
184 | fingerprint = []
185 |
186 | if Path(data_path / 'fingerprints' / replace(profile['Path']) / 'fingerprint.txt').exists():
187 | fingerprint = read_fingerprint_file(Path(data_path / 'fingerprints' / replace(profile['Path']) / 'fingerprint.txt'), log_level, log_file)
188 |
189 | if not fingerprint:
190 | print_debug(a=['creating new fingerprint for [%s]' % profile['Path']], log=log_level > 1, log_file=log_file)
191 | fingerprint = create_video_fingerprint(profile, hash_fps, log_level, log_file)
192 |
193 | end = datetime.now()
194 | print_debug(a=["processed fingerprint in %s for [%s]" % (str(end - start), profile['Path'])], log=log_level > 0, log_file=log_file)
195 | return fingerprint
196 |
197 |
198 | def check_files_exist(profiles=[]):
199 | if not profiles:
200 | return False
201 | for p in profiles:
202 | if 'Path' not in p or not Path(p['Path']).exists():
203 | return False
204 | return True
205 |
206 |
207 | def save_season_fingerprint(fingerprints, profiles, ndx, filtered_lengths, shortest_duration):
208 | size = len(filtered_lengths)
209 | sum = 0
210 | for f in filtered_lengths:
211 | sum += f
212 | average = int(sum / size)
213 |
214 | name = ''
215 | for profile in profiles:
216 | name += replace(profile['Path'])
217 | hash_object = hashlib.md5(name.encode())
218 | name = hash_object.hexdigest()
219 |
220 | season_fingerprint = {}
221 | season_fingerprint.update(profiles[ndx])
222 |
223 | tmp_start_frame = floor((profiles[ndx]['start_frame'] / profiles[ndx]['fps']) * hash_fps) if profiles[ndx]['start_frame'] > 0 else 0
224 | tmp_end_frame = floor((profiles[ndx]['end_frame'] / profiles[ndx]['fps']) * hash_fps) if profiles[ndx]['end_frame'] > 0 else 0
225 |
226 | trimmed_fingerprint = fingerprints[ndx][tmp_start_frame:tmp_end_frame + 1]
227 |
228 | fingerprint_str = ''
229 | for f in trimmed_fingerprint:
230 | fingerprint_str += str(f)
231 |
232 | season_fingerprint['fingerprint'] = fingerprint_str
233 | season_fingerprint['reference_duration'] = shortest_duration
234 | season_fingerprint['average_frames'] = average
235 | season_fingerprint['average_sample_size'] = len(filtered_lengths)
236 | season_fingerprint['hash_fps'] = hash_fps
237 | season_fingerprint['revision_id'] = revision_id
238 |
239 | path = Path(data_path / 'fingerprints' / (name + '.json'))
240 | Path(data_path / 'fingerprints').mkdir(parents=True, exist_ok=True)
241 | with path.open('w+') as json_file:
242 | json.dump(season_fingerprint, json_file, indent=4)
243 |
244 |
245 | def intro_duration(profile):
246 | intro_duration = profile['end_frame'] - profile['start_frame']
247 | if intro_duration < 0:
248 | intro_duration = 0
249 | return intro_duration
250 |
251 |
252 | def reject_outliers(input_list, iq_range=0.2):
253 | if not input_list:
254 | return input_list
255 |
256 | sr = pandas.Series(input_list, copy=True)
257 | pcnt = (1 - iq_range) / 2
258 | qlow, median, qhigh = sr.dropna().quantile([pcnt, 0.50, 1 - pcnt])
259 | iqr = qhigh - qlow
260 | return sr[(sr - median).abs() <= iqr].values.tolist()
261 |
262 |
263 | def sort_conforming_profile(profile_tuple):
264 | return profile_tuple[1]
265 |
266 |
267 | def correct_errors(fingerprints, profiles, ref_profile, log_level=0, log_file=False):
268 | # skip outlier rejection when there is only the reference profile and one other file
269 | if ref_profile is not None and len(fingerprints) == 2:
270 | diff_from_avg = abs(intro_duration(profiles[1]) - ref_profile['average_frames'])
271 | if diff_from_avg < int(15 * profiles[1]['fps']):
272 | print_debug(a=['single file conforms to reference profile'], log=log_level > 0, log_file=log_file)
273 | return
274 |
275 | # build a list of intro lengths with outliers rejected
276 | lengths = []
277 | for profile in profiles:
278 | if intro_duration(profile) <= int(profile['fps'] * max_intro_length_sec) and \
279 | intro_duration(profile) > int(profile['fps'] * 2):
280 |
281 | lengths.append(intro_duration(profile))
282 | else:
283 | print_debug(a=['excluding profile from pool of durations due to it being to long or short %s start %s end %s' % (profile['Path'], profile['start_frame'], profile['end_frame'])], log=log_level > 1, log_file=log_file)
284 | print_timestamp(profile['Path'], profile['start_frame'], profile['end_frame'], profile['fps'], log_level, log_file)
285 | filtered_lengths = reject_outliers(lengths)
286 |
287 | if len(filtered_lengths) < 1:
288 | for profile in profiles:
289 | profile['start_frame'] = 0
290 | profile['end_frame'] = 0
291 | print_debug(a=['failed to correct - could not establish consistency between episodes'], log=log_level > 0, log_file=log_file)
292 | return
293 |
294 | size = len(filtered_lengths)
295 | sum = 0
296 | for f in filtered_lengths:
297 | sum += f
298 | average = int(sum / size)
299 |
300 | print_debug(a=['average length in frames [%s] from %s of %s files' % (average, len(filtered_lengths), len(profiles))], log=log_level > 0, log_file=log_file)
301 | print_timestamp('average length (time)', 0, average, profiles[0]['fps'], log_level, log_file)
302 |
303 | if average == 0 or int(round(average / profiles[0]['fps'])) < min_intro_length_sec:
304 | for profile in profiles:
305 | profile['start_frame'] = 0
306 | profile['end_frame'] = 0
307 | print_debug(a=['skipping profiles - average duration is too short'], log=log_level > 0, log_file=log_file)
308 | return
309 |
310 | # build a list of conforming and non conforming profiles (int indexes)
311 | # loop through profiles and check if their duration is in the filtered list of intro lengths
312 | conforming_profiles = []
313 | non_conforming_profiles = []
314 | for ndx in range(0, len(profiles)):
315 | diff_from_avg = abs(intro_duration(profiles[ndx]) - average)
316 | print_debug(a=['file [%s] diff from average %s' % (profiles[ndx]['Path'], diff_from_avg)], log=log_level > 1, log_file=log_file)
317 | if intro_duration(profiles[ndx]) in filtered_lengths or \
318 | diff_from_avg < int(15 * profiles[ndx]['fps']):
319 | conforming_profiles.append((ndx, intro_duration(profiles[ndx])))
320 | else:
321 | print_debug(a=['\nrejected file [%s] with start %s end %s' % (profiles[ndx]['Path'], profiles[ndx]['start_frame'], profiles[ndx]['end_frame'])], log_file=log_file)
322 | print_timestamp(profiles[ndx]['Path'], profiles[ndx]['start_frame'], profiles[ndx]['end_frame'], profiles[ndx]['fps'], log_level, log_file)
323 | non_conforming_profiles.append(ndx)
324 |
325 | print_debug(a=['\nrejected start frame values from %s of %s results\n' % (len(non_conforming_profiles), len(profiles))], log=log_level > 0, log_file=log_file)
326 |
327 | if len(conforming_profiles) < 1:
328 | print_debug(a=['all profiles were rejected'], log=log_level > 0, log_file=log_file)
329 | for profile in profiles:
330 | profile['start_frame'] = 0
331 | profile['end_frame'] = 0
332 | return
333 |
334 | # sort the list of conforming profile indexes and find the mean value
335 | # this profile will be the reference profile used when repairing the rejected profiles
336 | conforming_profiles.sort(key=sort_conforming_profile)
337 | shortest_duration = intro_duration(profiles[conforming_profiles[0][0]])
338 | print_debug(a=['shortest duration %s from %s' % (shortest_duration, profiles[conforming_profiles[0][0]]['Path'])], log=log_level > 0, log_file=log_file)
339 | ref_profile_ndx = conforming_profiles[int(floor(len(conforming_profiles) / 2))][0]
340 |
341 | if ref_profile is None:
342 | print_debug(a=['saving season fingerprint'], log=log_level > 0, log_file=log_file)
343 | save_season_fingerprint(fingerprints, profiles, ref_profile_ndx, filtered_lengths, shortest_duration)
344 |
345 | if len(non_conforming_profiles) < 1:
346 | print_debug(a=['no profiles were rejected!'], log=log_level > 0, log_file=log_file)
347 | return
348 |
349 | # reprocess the rejected profiles by comparing them to the reference profile
350 | for nprofile in non_conforming_profiles:
351 | print_debug(a=['reprocessing %s by comparing to %s' % (profiles[nprofile]['Path'], profiles[ref_profile_ndx]['Path'])], log=log_level > 0, log_file=log_file)
352 | tmp_profile = {}
353 | tmp_profile.update(profiles[ref_profile_ndx])
354 | tmp_index = len(fingerprints)
355 |
356 | if ref_profile is None or profiles[ref_profile_ndx]['Path'] != ref_profile['Path']:
357 | tmp_start_frame = floor((profiles[ref_profile_ndx]['start_frame'] / profiles[ref_profile_ndx]['fps']) * hash_fps) if profiles[ref_profile_ndx]['start_frame'] > 0 else 0
358 | tmp_end_frame = floor((profiles[ref_profile_ndx]['end_frame'] / profiles[ref_profile_ndx]['fps']) * hash_fps) if profiles[ref_profile_ndx]['end_frame'] > 0 else 0
359 | fingerprints.append(fingerprints[ref_profile_ndx][tmp_start_frame:tmp_end_frame + 1])
360 | else:
361 | fingerprints.append(fingerprints[ref_profile_ndx])
362 | profiles.append(tmp_profile)
363 |
364 | print_timestamp(profiles[tmp_index]['Path'], profiles[tmp_index]['start_frame'], profiles[tmp_index]['end_frame'], profiles[tmp_index]['fps'], log_level, log_file)
365 |
366 | process_pairs(fingerprints, profiles, tmp_index, nprofile, SECOND, log_level, log_file)
367 | profiles.pop()
368 | fingerprints.pop()
369 |
370 | # repeat building a list of lengths and filtering them
371 | lengths = []
372 | for profile in profiles:
373 | lengths.append(intro_duration(profile))
374 | new_filtered_lengths = reject_outliers(lengths)
375 |
376 | # repeat checking each profile's duration against the new filtered list of lengths
377 | repaired = 0
378 | for nprofile in range(0, len(profiles)):
379 | diff_from_avg = abs(intro_duration(profiles[nprofile]) - average)
380 | guessed_start = floor(profiles[nprofile]['end_frame'] - (shortest_duration / 2))
381 | if guessed_start < 0:
382 | guessed_start = 0
383 | guessed_start_diff = abs(profiles[nprofile]['end_frame'] - guessed_start - average)
384 | if intro_duration(profiles[nprofile]) >= int(min_intro_length_sec * profiles[nprofile]['fps']):
385 | if nprofile in non_conforming_profiles:
386 | repaired += 1
387 | print_debug(a=['\nreprocess successful for file [%s] new start %s end %s' % (profiles[nprofile]['Path'], profiles[nprofile]['start_frame'], profiles[nprofile]['end_frame'])], log=log_level > 0, log_file=log_file)
388 | print_timestamp(profiles[nprofile]['Path'], profiles[nprofile]['start_frame'], profiles[nprofile]['end_frame'], profiles[nprofile]['fps'], log_level, log_file)
389 | '''
390 | elif intro_duration(profiles[nprofile]) > int(2 * profiles[nprofile]['fps']) and guessed_start_diff < int(15 * profiles[nprofile]['fps']):
391 | if nprofile in non_conforming_profiles:
392 | repaired += 1
393 | profiles[nprofile]['start_frame'] = guessed_start
394 | print_debug(a=['\nreprocess successful by guessing start for file [%s] - new start %s end %s' % (profiles[nprofile]['Path'], profiles[nprofile]['start_frame'], profiles[nprofile]['end_frame'])], log=log_level > 0, log_file=log_file)
395 | print_timestamp(profiles[nprofile]['Path'], profiles[nprofile]['start_frame'], profiles[nprofile]['end_frame'], profiles[nprofile]['fps'], log_level, log_file)
396 | '''
397 | elif nprofile in non_conforming_profiles:
398 | print_debug(a=['\nfailed to locate intro by reprocessing %s' % profiles[nprofile]['Path']], log_file=log_file)
399 | print_debug(a=['file [%s] new start %s end %s' % (profiles[nprofile]['Path'], profiles[nprofile]['start_frame'], profiles[nprofile]['end_frame'])], log=log_level > 0, log_file=log_file)
400 | print_timestamp(profiles[nprofile]['Path'], profiles[nprofile]['start_frame'], profiles[nprofile]['end_frame'], profiles[nprofile]['fps'], log_level, log_file)
401 | profiles[nprofile]['start_frame'] = 0
402 | profiles[nprofile]['end_frame'] = 0
403 | print_debug(a=['\nrepaired %s/%s non conforming profiles\n' % (repaired, len(non_conforming_profiles))], log=log_level > 0, log_file=log_file)
404 |
405 |
406 | def process_pairs(fingerprints, profiles, ndx_1, ndx_2, mode, log_level, log_file):
407 |
408 | start_end = get_start_end(fingerprints[ndx_1], profiles[ndx_1]['fps'], fingerprints[ndx_2], profiles[ndx_2]['fps'], log_level)
409 |
410 | if mode == BOTH or mode == FIRST:
411 | profiles[ndx_1]['start_frame'] = start_end[0][0]
412 | if profiles[ndx_1]['start_frame'] < 0:
413 | print_debug(a=["start frame is negative (%s), setting to 0 for [%s]" % (profiles[ndx_1]['start_frame'], profiles[ndx_1]['Path'])], log=log_level > 1)
414 | profiles[ndx_1]['start_frame'] = 0
415 |
416 | profiles[ndx_1]['end_frame'] = start_end[0][1]
417 | if profiles[ndx_1]['end_frame'] < 0:
418 | print_debug(a=["end frame is negative (%s), setting to 0 for [%s]" % (profiles[ndx_1]['end_frame'], profiles[ndx_1]['Path'])], log=log_level > 1)
419 | profiles[ndx_1]['end_frame'] = 0
420 |
421 | print_timestamp(profiles[ndx_1]['Path'], profiles[ndx_1]['start_frame'], profiles[ndx_1]['end_frame'], profiles[ndx_1]['fps'], log_level, log_file)
422 |
423 | if mode == BOTH or mode == SECOND:
424 | profiles[ndx_2]['start_frame'] = start_end[1][0]
425 | if profiles[ndx_2]['start_frame'] < 0:
426 | print_debug(a=["start frame is negative (%s), setting to 0 for [%s]" % (profiles[ndx_2]['start_frame'], profiles[ndx_2]['Path'])], log=log_level > 1)
427 | profiles[ndx_2]['start_frame'] = 0
428 |
429 | profiles[ndx_2]['end_frame'] = start_end[1][1]
430 | if profiles[ndx_2]['end_frame'] < 0:
431 | print_debug(a=["end frame is negative (%s), setting to 0 for [%s]" % (profiles[ndx_2]['end_frame'], profiles[ndx_2]['Path'])], log=log_level > 1)
432 | profiles[ndx_2]['end_frame'] = 0
433 |
434 | print_timestamp(profiles[ndx_2]['Path'], profiles[ndx_2]['start_frame'], profiles[ndx_2]['end_frame'], profiles[ndx_2]['fps'], log_level, log_file)
435 |
436 |
437 | def process_directory(profiles=[], ref_profile=None, hashfps=2, log_level=0, log_file=False, cleanup=True, log_timestamp=None):
438 | global session_timestamp
439 | global hash_fps
440 |
441 | hash_fps = hashfps
442 |
443 | if log_timestamp is not None:
444 | session_timestamp = log_timestamp
445 |
446 | start = datetime.now()
447 | print_debug(a=['started at', start], log=log_level > 0, log_file=log_file)
448 | print_debug(a=["Check Frame: %s\n" % str(check_frame)], log=log_level > 0, log_file=log_file)
449 | print_debug(a=["Hash fps: %s\n" % str(hash_fps)], log=log_level > 0, log_file=log_file)
450 |
451 | if cleanup:
452 | print_debug(a=['fingerprint files will be cleaned up'], log=log_level > 0, log_file=log_file)
453 |
454 | if not check_files_exist(profiles):
455 | print_debug(a=['input files invalid or cannot be accessed'], log=log_level > 0, log_file=log_file)
456 | return {}
457 |
458 | if (ref_profile is not None and len(profiles) < 1) or (ref_profile is None and len(profiles) < 2):
459 | print_debug(a=['fewer than 2 valid fingerprints were found - skipping'], log=log_level > 0, log_file=log_file)
460 | return {}
461 |
462 | print_debug(a=['processing %s files' % len(profiles)], log=log_level > 0, log_file=log_file)
463 |
464 | if cleanup and Path(data_path / 'fingerprints').is_dir():
465 | try:
466 | shutil.rmtree(Path(data_path / 'fingerprints'))
467 | except OSError as e:
468 | print_debug(a=["Error: %s : %s" % ('fingerprints', e.strerror)], log_file=log_file)
469 |
470 | fingerprints = [] # list of hash values
471 |
472 | hashing_start = datetime.now()
473 | for p in profiles:
474 | fingerprint = get_or_create_fingerprint(p, log_level, log_file)
475 | if fingerprint:
476 | fingerprints.append(fingerprint)
477 | hashing_end = datetime.now()
478 | print_debug(a=["got or created fingerprints in %s" % str(hashing_end - hashing_start)], log=log_level > 0, log_file=log_file)
479 |
480 | if ref_profile is not None and 'hash_fps' in ref_profile and ref_profile['hash_fps'] == hash_fps:
481 | fingerprint = read_fingerprint(ref_profile['fingerprint'], log_level, log_file)
482 | if fingerprint and abs(len(fingerprint) - floor(intro_duration(ref_profile) / (ref_profile['fps'] / hash_fps))) < 3:
483 | print_debug(a=["loaded reference profile"], log=log_level > 0, log_file=log_file)
484 | print_timestamp(ref_profile['Path'], ref_profile['start_frame'], ref_profile['end_frame'], ref_profile['fps'], log_level, log_file)
485 | fingerprints.insert(0, fingerprint)
486 | ref_profile.pop('fingerprint', None)
487 | profiles.insert(0, ref_profile)
488 | else:
489 | ref_profile = None
490 | else:
491 | ref_profile = None
492 |
493 | if (ref_profile is not None and len(fingerprints) < 1) or (ref_profile is None and len(fingerprints) < 2):
494 | print_debug(a=['fewer than 2 valid fingerprints were found - skipping'], log=log_level > 0, log_file=log_file)
495 | return {}
496 |
497 | # loop through each pair and store the start/end frames in their profiles
498 | # then do the same for the remaining profile if count is odd
499 | #
500 | # use mode: FIRST, SECOND, BOTH to decide whether to save the values to the first, second, or both profiles
501 | # changing modes is useful for processing a new profile against one that's already processed
502 | # for instance, if a profile is rejected it could be reprocessed against a different profile without risking...
503 | # ...overwriting the the start/end frame values for the reference profile
504 | counter = 0
505 | process_pairs_start = datetime.now()
506 |
507 | if ref_profile is not None:
508 | for i in range(1, len(fingerprints)):
509 | process_pairs(fingerprints, profiles, 0, i, SECOND, log_level, log_file)
510 | else:
511 | while len(fingerprints) - 1 > counter:
512 | process_pairs(fingerprints, profiles, counter, counter + 1, BOTH, log_level, log_file)
513 | counter += 2
514 | if len(fingerprints) % 2 != 0:
515 | process_pairs(fingerprints, profiles, -2, -1, SECOND, log_level, log_file)
516 |
517 | process_pairs_end = datetime.now()
518 | print_debug(a=["processed fingerprint pairs in: " + str(process_pairs_end - process_pairs_start)], log=log_level > 0, log_file=log_file)
519 |
520 | correct_errors_start = datetime.now()
521 | correct_errors(fingerprints, profiles, ref_profile, log_level, log_file)
522 | correct_errors_end = datetime.now()
523 | print_debug(a=["finished error correction in: " + str(correct_errors_end - correct_errors_start)], log=log_level > 0, log_file=log_file)
524 |
525 | if ref_profile is not None:
526 | fingerprints.pop(0)
527 | profiles.pop(0)
528 |
529 | # finally, automatically reject episodes with intros shorted than a specified length (default 15 seconds)
530 | # apply pre-roll if wanted
531 | # use the fps and start/end frame values to calculate the timestamps for the intros and add them to the profiles
532 | for profile in profiles:
533 | if intro_duration(profile) < int(min_intro_length_sec * profile['fps']):
534 | print_debug(a=['%s - intro is less than %s seconds - skipping' % (profile['Path'], min_intro_length_sec)], log=log_level > 1, log_file=log_file)
535 | profile['start_frame'] = 0
536 | profile['end_frame'] = 0
537 | elif preroll_seconds > 0 and profile['end_frame'] > profile['start_frame'] + floor(profile['fps'] * preroll_seconds):
538 | profile['end_frame'] -= floor(profile['fps'] * preroll_seconds)
539 | get_timestamp_from_frame(profile)
540 | print_debug(a=[profile['Path'] + " start time: " + profile['start_time'] + " end time: " + profile['end_time']], log=log_level > 1, log_file=log_file)
541 |
542 | end = datetime.now()
543 | print_debug(a=["ended at", end], log=log_level > 0, log_file=True)
544 | print_debug(a=["run time: " + str(end - start)], log=log_level > 0, log_file=True)
545 |
546 | if cleanup and Path(data_path / 'fingerprints').is_dir():
547 | try:
548 | shutil.rmtree(Path(data_path / 'fingerprints'))
549 | except OSError as e:
550 | print_debug(a=["Error: %s : %s" % ('fingerprints', e.strerror)], log_file=log_file)
551 | return profiles
552 |
553 |
554 | def main(argv):
555 |
556 | path = ''
557 | ref_path = ''
558 | log_level = 0
559 | cleanup = False
560 | log = False
561 | try:
562 | opts, args = getopt.getopt(argv, "hi:dvclr:")
563 | except getopt.GetoptError:
564 | print_debug(['decode.py -i -v (verbose - some logging) -d (debug - most logging) -c (cleanup) -s (slow mode) -l (log to file)\n'])
565 | sys.exit(2)
566 |
567 | for opt, arg in opts:
568 | if opt == '-h':
569 | print_debug(['decode.py -i -v (verbose - some logging) -d (debug - most logging) -c (cleanup) -s (slow mode) -l (log to file)\n'])
570 | sys.exit()
571 | elif opt == '-i':
572 | path = arg
573 | elif opt == '-l':
574 | log = True
575 | elif opt == '-d':
576 | log_level = 2
577 | elif opt == '-v':
578 | log_level = 1
579 | elif opt == '-c':
580 | cleanup = True
581 | elif opt == '-r':
582 | ref_path = arg
583 |
584 | if path == '' or not Path(path).is_dir():
585 | print_debug(['decode.py -i -v (verbose - some logging) -d (debug - most logging) -c (cleanup) -s (slow mode) -l (log to file)\n'])
586 | sys.exit(2)
587 |
588 | common_video_extensions = ['.webm', '.mkv', '.avi', '.mts', '.m2ts', '.ts', '.mov', '.wmv', '.mp4', '.m4v', '.mpg', '.mpeg', '.m2v']
589 |
590 | ref_profile = None
591 | if ref_path != '' and Path(ref_path).exists():
592 | with Path(ref_path).open('r') as profile_json:
593 | ref_profile = json.load(profile_json)
594 |
595 | profiles = []
596 | for child in Path(path).iterdir():
597 | if child.name[0] == '.':
598 | continue
599 | matched_ext = False
600 | for ext in common_video_extensions:
601 | if child.name.endswith(ext):
602 | matched_ext = True
603 | if matched_ext:
604 | profile = {}
605 | profile['Path'] = str(child.resolve())
606 | profiles.append(profile)
607 |
608 | result = process_directory(profiles=profiles, ref_profile=ref_profile, log_level=log_level, log_file=log, cleanup=cleanup)
609 | print(result)
610 |
611 |
612 | if __name__ == "__main__":
613 | main(sys.argv[1:])
614 |
--------------------------------------------------------------------------------
/diff_jellyfin_cache.py:
--------------------------------------------------------------------------------
1 | import sys
2 | import getopt
3 | import json
4 |
5 | from pathlib import Path
6 | from math import floor
7 | from datetime import timedelta
8 |
9 |
10 | def get_timestamp(start_ms, end_ms):
11 | start_time = floor(start_ms / 1000) if start_ms != 0 else 0
12 | end_time = floor(end_ms / 1000) if end_ms != 0 else 0
13 |
14 | start = str(timedelta(seconds=start_time)).split('.')[0]
15 | end = str(timedelta(seconds=end_time)).split('.')[0]
16 |
17 | return (start, end)
18 |
19 |
20 | def get_dir_contents(path: Path, fullpath: bool):
21 | file_paths = []
22 | for child in path.iterdir():
23 | if child.name[0] == '.':
24 | continue
25 | file_paths.append(str(child.resolve()) if fullpath else str(child.name))
26 | file_paths.sort()
27 | return file_paths
28 |
29 |
30 | def print_dir_contents(path: Path, padding: int):
31 | if not path.is_dir():
32 | return
33 |
34 | contents = get_dir_contents(path, False)
35 | for c in contents:
36 | print('%s' % c.rjust(len(c) + padding))
37 |
38 |
39 | def intro_duration(profile):
40 | intro_duration = profile['end_frame'] - profile['start_frame']
41 | if intro_duration < 0:
42 | intro_duration = 0
43 | return intro_duration
44 |
45 |
46 | def get_item(path: Path):
47 | if not path.exists():
48 | return
49 |
50 | profile = None
51 | with path.open('r') as json_file:
52 | profile = json.load(json_file)
53 |
54 | if profile is None:
55 | return
56 | if 'start_time_ms' not in profile or 'end_time_ms' not in profile:
57 | return
58 |
59 | profile['intro_duration'] = intro_duration(profile)
60 |
61 | return profile
62 |
63 |
64 | def get_season(path: Path):
65 | if not path.is_dir():
66 | return
67 |
68 | season = {
69 | 'SeasonFingerprint': None,
70 | 'Episodes': {},
71 | 'SeriesName': None,
72 | 'SeasonName': None,
73 | 'SeriesId': None,
74 | 'SeasonId': None,
75 | }
76 |
77 | contents = get_dir_contents(path, True)
78 | for c in contents:
79 | if c.endswith('.json'):
80 | item = get_item(Path(c))
81 | if not item:
82 | continue
83 |
84 | if 'SeriesName' in item and item['SeriesName'] != '':
85 | season['SeriesName'] = item['SeriesName']
86 | if 'SeasonName' in item and item['SeasonName'] != '':
87 | season['SeasonName'] = item['SeasonName']
88 | if 'SeasonId' in item and item['SeasonId'] != '':
89 | season['SeasonId'] = item['SeasonId']
90 | if 'SeriesId' in item and item['SeriesId'] != '':
91 | season['SeriesId'] = item['SeriesId']
92 |
93 | if c.endswith('season.json'):
94 | season['SeasonFingerprint'] = get_item(Path(c))
95 | else:
96 | season['Episodes'][item['EpisodeId']] = item
97 | return season
98 |
99 |
100 | def get_series(path: Path):
101 | # print('%s:' % str(path).rjust(len(str(path)) + padding))
102 | if not path.is_dir():
103 | return
104 |
105 | series = {
106 | 'Seasons': {},
107 | 'SeriesName': None,
108 | 'SeriesId': None,
109 | }
110 |
111 | contents = get_dir_contents(path, True)
112 | for c in contents:
113 | season = get_season(Path(c))
114 | if season is None:
115 | continue
116 |
117 | if 'SeriesName' in season and season['SeriesName'] != '':
118 | series['SeriesName'] = season['SeriesName']
119 | if 'SeriesId' in season and season['SeriesId'] != '':
120 | series['SeriesId'] = season['SeriesId']
121 |
122 | series['Seasons'][season['SeasonId']] = season
123 |
124 | return series
125 |
126 |
127 | def print_series(path: Path, padding: int):
128 | if not path.is_dir():
129 | return
130 |
131 | series = get_series(path)
132 | if not series or not series['Seasons']:
133 | return
134 |
135 | seriesName = series['SeriesName']
136 | print('%s:' % str(seriesName).rjust(len(str(seriesName)) + padding))
137 |
138 | for season_key in series['Seasons']:
139 | season = series['Seasons'][season_key]
140 | if not season['Episodes']:
141 | continue
142 |
143 | seasonName = season['SeasonName']
144 | print('%s:' % str(seasonName).rjust(len(str(seasonName)) + padding + 2))
145 | episodes = season['Episodes']
146 |
147 | for episode_key in episodes:
148 | episode = episodes[episode_key]
149 | start, end = get_timestamp(episode['start_time_ms'], episode['end_time_ms'])
150 | print_str = '%s - start: %s end %s' % (episode['Name'], start, end)
151 | print(print_str.rjust(len(str(print_str)) + padding + 4))
152 |
153 | if 'SeasonFingerprint' in season and season['SeasonFingerprint'] is not None:
154 | seasonFingerprint = season['SeasonFingerprint']
155 | start, end = get_timestamp(seasonFingerprint['start_time_ms'], seasonFingerprint['end_time_ms'])
156 | print_str = 'season fingerprint [%s - start: %s end %s]' % (seasonFingerprint['Name'], start, end)
157 | print(print_str.rjust(len(str(print_str)) + padding + 4))
158 |
159 |
160 | def filter_dirs(old_path: Path, new_path: Path):
161 | old_path_contents = get_dir_contents(old_path, False)
162 | new_path_contents = get_dir_contents(new_path, False)
163 |
164 | contents_in_both = list(set(old_path_contents).intersection(set(new_path_contents)))
165 | contents_in_both.sort()
166 |
167 | contents_not_in_both = list(set(old_path_contents).symmetric_difference(set(new_path_contents)))
168 | contents_not_in_both.sort()
169 |
170 | if contents_not_in_both:
171 | print('series not in both:')
172 | for e in contents_not_in_both:
173 | if e in old_path_contents:
174 | path = Path(old_path / e)
175 | else:
176 | path = Path(new_path / e)
177 | print('%s:' % str(path).rjust(len(str(path)) + 2))
178 | print_dir_contents(path, 4)
179 | print('\n')
180 |
181 | if path.is_dir():
182 | print_series(path, 4)
183 | return contents_in_both
184 |
185 |
186 | def filter_ids(old_dict: dict, new_dict: dict):
187 | old_list = list(old_dict.keys())
188 | new_list = list(new_dict.keys())
189 |
190 | in_both = list(set(old_list).intersection(set(new_list)))
191 | not_in_both = list(set(old_list).symmetric_difference(set(new_list)))
192 | return in_both, not_in_both
193 |
194 |
195 | def check_if_in_list_of_dict(dict_list, value):
196 | if dict_list is None:
197 | return -1
198 |
199 | for ndx in range(0, len(dict_list)):
200 | if value in dict_list[ndx].values():
201 | return ndx
202 | return -1
203 |
204 |
205 | def diff_data(old_path: Path, new_path: Path):
206 | SeriesIds = filter_dirs(old_path, new_path)
207 | total_episodes = 0
208 | total_diffs = 0
209 | percent_diff = 0
210 |
211 | diff_threshold = 2
212 |
213 | print('\n\n------------------------------------------------\n\n')
214 |
215 | print('series in both:')
216 |
217 | for Id in SeriesIds:
218 | has_logged_series = False
219 |
220 | old_series = get_series(Path(old_path / Id))
221 | new_series = get_series(Path(new_path / Id))
222 |
223 | seasons, seasons_not_in_both = filter_ids(old_series['Seasons'], new_series['Seasons'])
224 |
225 | for season_id in seasons:
226 | has_logged_season = False
227 |
228 | episodes, episodes_not_in_both = filter_ids(old_series['Seasons'][season_id]['Episodes'], new_series['Seasons'][season_id]['Episodes'])
229 | for episode_id in episodes:
230 | total_episodes += 1
231 | old_ep = old_series['Seasons'][season_id]['Episodes'][episode_id]
232 | new_ep = new_series['Seasons'][season_id]['Episodes'][episode_id]
233 |
234 | old_duration = old_ep['intro_duration'] / old_ep['fps'] if old_ep['intro_duration'] > 0 else 0
235 | new_duration = new_ep['intro_duration'] / new_ep['fps'] if new_ep['intro_duration'] > 0 else 0
236 |
237 | diff = abs(old_duration - new_duration)
238 | if diff > diff_threshold:
239 | total_diffs += 1
240 | if not has_logged_series:
241 | seriesName = old_series['SeriesName']
242 | print('%s:' % str(seriesName).rjust(len(str(seriesName)) + 2))
243 | has_logged_series = True
244 |
245 | if not has_logged_season:
246 | seasonName = old_ep['SeasonName']
247 | print('%s:' % str(seasonName).rjust(len(str(seasonName)) + 4))
248 | has_logged_season = True
249 |
250 | log_str = 'intro duration for episode [%s] of season [%s] series [%s] differs' % (old_ep['Name'], old_ep['SeasonName'], old_ep['SeriesName'])
251 | print('%s' % str(log_str).rjust(len(str(log_str)) + 6))
252 | start1, end1 = get_timestamp(old_ep['start_time_ms'], old_ep['end_time_ms'])
253 | log_str = 'old start: %s end %s' % (start1, end1)
254 | print('%s' % str(log_str).rjust(len(str(log_str)) + 6))
255 | start2, end2 = get_timestamp(new_ep['start_time_ms'], new_ep['end_time_ms'])
256 | log_str = 'new start: %s end %s\n' % (start2, end2)
257 | print('%s' % str(log_str).rjust(len(str(log_str)) + 6))
258 | log_str = 'diff %s seconds' % diff
259 | print('%s' % str(log_str).rjust(len(str(log_str)) + 6))
260 |
261 | percent_diff = (total_diffs / total_episodes) * 100
262 | print('%s/%s episodes ( %s percent ) changed more than %s sec' % (total_diffs, total_episodes, percent_diff, diff_threshold))
263 |
264 |
265 | def main(argv):
266 | old_cache_path_str = ''
267 | new_cache_path_str = ''
268 |
269 | try:
270 | opts, args = getopt.getopt(argv, "ho:n:")
271 | except getopt.GetoptError:
272 | print('diff_jellyfin_cache.py -o -n ')
273 | sys.exit(2)
274 |
275 | for opt, arg in opts:
276 | if opt == '-h':
277 | print('diff_jellyfin_cache.py -o -n ')
278 | sys.exit()
279 | elif opt == '-o':
280 | old_cache_path_str = arg
281 | elif opt == '-n':
282 | new_cache_path_str = arg
283 |
284 | if old_cache_path_str == '' or new_cache_path_str == '':
285 | print('enter both cache paths')
286 | return
287 |
288 | if not Path(old_cache_path_str).exists() or not Path(old_cache_path_str).is_dir() \
289 | or not Path(new_cache_path_str).exists() or not Path(new_cache_path_str).is_dir():
290 | print('cache paths aren\'t valid')
291 | return
292 |
293 | old_path = Path(old_cache_path_str)
294 | new_path = Path(new_cache_path_str)
295 |
296 | diff_data(old_path, new_path)
297 |
298 |
299 | if __name__ == "__main__":
300 | main(sys.argv[1:])
301 |
--------------------------------------------------------------------------------
/docs/_config.yml:
--------------------------------------------------------------------------------
1 | title: TV Intro Detection
2 | description: This project tries to detect intros of tv series by comparing pairs of episodes to find the largest common subset of frames.
3 |
4 | remote_theme: just-the-docs/just-the-docs
5 |
6 | # Color scheme supports "light" (default) and "dark"
7 | color_scheme: dark
8 |
9 | kramdown:
10 | syntax_highlighter_opts:
11 | block:
12 | line_numbers: false
13 |
14 |
--------------------------------------------------------------------------------
/docs/docker.md:
--------------------------------------------------------------------------------
1 | # Running with Docker
2 |
3 | ### Docker Compose - Scanner & Skipper
4 |
5 | ```
6 | ---
7 | version: "3.8"
8 |
9 | services:
10 | Jellyfin-Intro-Scanner:
11 | image: ghcr.io/mueslimak3r/jellyfin-intro-scanner:latest
12 |
13 | container_name: Jellyfin-Intro-Scanner
14 | environment:
15 | - JELLYFIN_URL=http://Jellyfin:port
16 | - JELLYFIN_USERNAME=username
17 | - JELLYFIN_PASSWORD=password
18 | volumes:
19 | - /path/to/media/on/host:/path/to/media/on/Jellyfin/container
20 | - /path/to/config:/app/config
21 | restart: unless-stopped
22 |
23 | Jellyfin-Intro-Skipper:
24 | image: ghcr.io/mueslimak3r/jellyfin-intro-skipper:latest
25 |
26 | container_name: Jellyfin-Intro-Skipper
27 | environment:
28 | - JELLYFIN_URL=http://Jellyfin:port
29 | - JELLYFIN_USERNAME=username
30 | - JELLYFIN_PASSWORD=password
31 | volumes:
32 | - /path/to/config:/app/config
33 | restart: unless-stopped
34 | ```
35 |
36 | ### Docker Run - Scanner
37 | ```
38 | docker run -d \
39 | --name=Jellyfin-Intro-Scanner \
40 | -e JELLYFIN_URL=http://Jellyfin:port \
41 | -e JELLYFIN_USERNAME=username \
42 | -e JELLYFIN_PASSWORD='password' \
43 | -v /path/to/media/on/host:/path/to/media/on/Jellyfin/container \
44 | -v /path/to/config:/app/config \
45 | --restart unless-stopped \
46 | ghcr.io/mueslimak3r/jellyfin-intro-scanner:latest
47 | ```
48 |
49 | ### Docker Run - Skipper
50 | ```
51 | docker run -d \
52 | --name=Jellyfin-Intro-Skipper \
53 | -e JELLYFIN_URL=http://Jellyfin:port \
54 | -e JELLYFIN_USERNAME=username \
55 | -e JELLYFIN_PASSWORD='password' \
56 | -v /path/to/config:/app/config \
57 | --restart unless-stopped \
58 | ghcr.io/mueslimak3r/jellyfin-intro-skipper:latest
59 | ```
60 |
61 | ### Parameters - used by both scanner and skipper
62 |
63 | | Parameter | Function | Description |
64 | |:-|:-:|-:|
65 | | Required | ```-e JELLYFIN_URL=http://Jellyfin:port``` | Jellyfin URL |
66 | |---
67 | | Required | ```-e JELLYFIN_USERNAME=username``` | Jellyfin User Username |
68 | |---
69 | | Required | ```-e JELLYFIN_PASSWORD='password'``` | Jellyfin User Password |
70 | |---
71 | | Required | ```-v /path/to/config:/app/config``` | Location of config/data on disk. Must use the same locations for Jellyfin-Intro-Scanner & Jellyfin-Intro-Skipper containers to work correctly together. |
72 | |---
73 | | Required | ```-v /path/to/media/on/host:/path/to/media/on/Jellyfin/container``` | Location of media library on disk. If you use the same volume path for your Jellyfin container, you don't have to edit ```path_map.txt``` in your config folder. (If you need to change it you must first create a ```path_map.txt``` in your config folder. ***Not in the data subfolder***). |
74 | |---
75 | | Optional | ```-e REVERSE_SORT=TRUE``` | Process shows in reverse order |
76 | |---
77 | | Optional | ```-e PATH_MAP="/srv/mount1/tv::/data/tv1,/srv/mount2/tv::/data/tv2"``` | Specify host:container path mapping. Mappings specified here are added to those specified in ```path_map.txt``` |
78 | |---
79 | | Optional | ```-e CONFIG_DIR=/config``` | Use a different directory to store config files. The directory specified should be reflected in the ```/app/config``` path mapping. |
80 | |---
81 | | Optional | ```-e DATA_DIR=/config/data``` | Use a different directory to store cached data. Modifying this will likely require a new path mapping such as ```-v /path/to/data:/data``` |
82 | |---
83 | | Optional | ```-e LOG_LEVEL=INFO/VERBOSE/DEBUG``` | Change the log level (default is verbose for docker) |
84 | | Optional | ```-e AUTO_SKIP_COOLDOWN=4``` | Specify the cooldown time (seconds) for the auto skipper (default 2) |
85 | {:.table-striped}
86 |
87 | ### Docker with network shares
88 |
89 | Network shares need to be mounted with a driver. This example uses a Windows host, two SMB shares, and a local drive.
90 |
91 | ```
92 | ---
93 | version: "3.8"
94 |
95 | volumes:
96 | mymount:
97 | name: mymount
98 | driver_opts:
99 | type: cifs
100 | o: "user=Guest,iocharset=utf8,vers=3.1.1,rw"
101 | device: //192.168.0.101/MyMount
102 | myothermount:
103 | name: myothermount
104 | driver_opts:
105 | type: cifs
106 | o: "user=Guest,iocharset=utf8,vers=3.1.1,rw"
107 | device: //192.168.0.102/MyOtherMount
108 |
109 | services:
110 | Jellyfin-Intro-Scanner:
111 | image: ghcr.io/mueslimak3r/jellyfin-intro-scanner:latest
112 |
113 | container_name: Jellyfin-Intro-Scanner
114 | environment:
115 | - JELLYFIN_URL=http://192.168.0.1212:8096
116 | - JELLYFIN_USERNAME=myuser
117 | - JELLYFIN_PASSWORD=mypassword
118 | - PATH_MAP=/mnt/mymount/TV::/jellyfin-mymount/TV,/mnt/myothermount/TV::/jellyfin-myothermount/TV,/mnt/x/TV/::/jellyfin-x/TV
119 | # - LOG_LEVEL=DEBUG
120 | volumes:
121 | - mymount:/mnt/mymount
122 | - myothermount:/mnt/myothermount
123 | - /x/TV:/mnt/x/TV
124 | - /c/Tools/Jellyfin-Intro-Skip/config:/app/config
125 | restart: unless-stopped
126 |
127 | Jellyfin-Intro-Skipper:
128 | image: ghcr.io/mueslimak3r/jellyfin-intro-skipper:latest
129 |
130 | container_name: Jellyfin-Intro-Skipper
131 | environment:
132 | - JELLYFIN_URL=http://192.168.0.1212:8096
133 | - JELLYFIN_USERNAME=myuser
134 | - JELLYFIN_PASSWORD=mypassword
135 | volumes:
136 | - /c/Tools/Jellyfin-Intro-Skip/config:/app/config
137 | restart: unless-stopped
138 | ```
139 |
--------------------------------------------------------------------------------
/docs/index.md:
--------------------------------------------------------------------------------
1 | # Project Overview
2 |
3 | This project tries to detect intros of tv series by comparing pairs of episodes to find the largest common subset of frames.
4 |
5 | ### How the script compares videos
6 | Each frame from the first quarter of each episode is extracted and a hash (https://pypi.org/project/ImageHash/) is made on the frame. Each frame hash is added to a long video hash.
7 | In pairs the longest identical string is searched from the two video hashes.
8 | Assumption: this is the intro
9 |
10 | ### How the script finds your videos
11 | When using jellyfin.py, the script queries the jellyfin server and is sent a list of all the tv shows in your library. With that list of shows, it then queries the jellyfin server for the seasons and episodes for each of the shows. Included in the results of those queries is the location on the filesystem for each item (show, season, episode).
12 |
13 | To account for jellyfin potentially using a different filesystem path to access the media than the system running the script would (eg: running in docker, running the script on a separate computer with the media drives mounted as network shares), path mapping is used to translate the path jellyfin sees into a path that the script can use.
14 |
15 | When running decode.py on its own, the -i parameter is used to specify a folder that contains video files. decode.py doesn't do any searching beyond that single folder, so typically the provided folder will be a "season folder" that contains all the video files for that season of a show.
16 |
17 | Individual shows or seasons can be ignored by creating an empty file named `.ignore-intros` inside its folder.
18 |
19 | ### Examples
20 | scan your jellyfin library, store the result in json, verbose logging enabled, logging debug output to file enabled
21 |
22 | ```
23 | export JELLYFIN_URL="https://myurl" && export JELLYFIN_USERNAME="myusername" && export JELLYFIN_PASSWORD='mypassword'
24 |
25 | jellyfin.py -j -v -l
26 | ```
27 |
28 | or in reverse order
29 |
30 | ```
31 | jellyfin.py -j -v -l --reverse
32 | ```
33 |
34 | monitor your jellyfin sessions and automatically skip intros using the stored json data
35 |
36 | ```
37 | export JELLYFIN_URL="https://myurl" && export JELLYFIN_USERNAME="myusername" && export JELLYFIN_PASSWORD='mypassword'
38 |
39 | jellyfin_auto_skip.py
40 | ```
41 |
42 | manually scan a directory containing at least 2 video files, debug logging enabled, logging debug output to file enabled, delete fingerprint data afterward
43 |
44 | ```
45 | decode.py -i /path/to/tv/season -d -l -c
46 | ```
47 |
48 | make the script aware of your host:container path mapping by editing `path_map.txt`
49 |
50 | ```
51 | # use this file if you run jellyfin in a container
52 | # examples:
53 | #
54 | # linux client, Jellyfin on linux
55 | #
56 | # /host/system/tv/path::/jellyfin/container/tv/path
57 | #
58 | # or
59 | # Windows client, Jellyfin on Windows
60 | #
61 | # X:\some\path\TV::Y:\some\path\TV
62 | #
63 | # or
64 | # Windows client, Jellyfin on linux
65 | #
66 | # or
67 | # X:\some\path\TV::/jellyfin/container/tv/path
68 | ```
69 |
--------------------------------------------------------------------------------
/docs/troubleshooting.md:
--------------------------------------------------------------------------------
1 | # Troubleshooting
2 | If the `decode.py` script is killed while processing media you may encounter issues the next time you run it. This is because corrupt fingerprint files are likely left over from the killed session.
3 | Simply remove the directory fingerprints before running the script again.
4 |
5 | This can be avoided by using the "clean" flag `-c`, which clears the `/config/data/fingerprints` folder on startup and after completing.
6 | This issue is mostly only relevant when running `decode.py` on its own. Using `jellyfin.py` avoids the need to manage the `fingerprints` folder manually
7 |
--------------------------------------------------------------------------------
/docs/usage.md:
--------------------------------------------------------------------------------
1 | # Running The Scripts
2 |
3 | 1. Install python3 (tested with 3.8.9+)
4 | 2. Install ffmpeg
5 | 3. Install python dependencies from `requirements.txt`
6 | 4. To use with Jellyfin, run `jellyfin.py`. This will query Jellyfin for a list of series and their paths.
7 | 5. To process a directory manually, run `decode.py` and pass the parameter `-i` with the path to a directory containing at least **two** episodes of the same season
8 |
9 | By default there is little/no output to stdout or stderr until the script has finished processing some media. Run `jellyfin.py` or `decode.py` with the `-d` parameter for verbose output
10 |
11 | When using `jellyfin.py`, the results can be saved to `json` using the `-j` parameter. These will be saved in a sub-directory in `pwd`. Saving the results as json also allows them to be checked in subsequent runs to skip already processed files.
12 |
13 | Individual shows or seasons can be ignored by creating an empty file named `.ignore-intros` inside its folder.
14 |
15 | ### Examples
16 | scan your jellyfin library, store the result in json, verbose logging enabled, logging debug output to file enabled
17 |
18 | ```
19 | export JELLYFIN_URL="https://myurl" && export JELLYFIN_USERNAME="myusername" && export JELLYFIN_PASSWORD='mypassword'
20 |
21 | jellyfin.py -j -v -l
22 | ```
23 |
24 | or in reverse order
25 |
26 | ```
27 | jellyfin.py -j -v -l --reverse
28 | ```
29 |
30 | monitor your jellyfin sessions and automatically skip intros using the stored json data
31 |
32 | ```
33 | export JELLYFIN_URL="https://myurl" && export JELLYFIN_USERNAME="myusername" && export JELLYFIN_PASSWORD='mypassword'
34 |
35 | jellyfin_auto_skip.py
36 | ```
37 |
38 | manually scan a directory containing at least 2 video files, debug logging enabled, logging debug output to file enabled, delete fingerprint data afterward
39 |
40 | ```
41 | decode.py -i /path/to/tv/season -d -l -c
42 | ```
43 |
44 | make the script aware of your host:container path mapping by editing `path_map.txt`
45 |
46 | ```
47 | # use this file if you run jellyfin in a container
48 | # example:
49 | # /host/system/tv/path::/jellyfin/container/tv/path
50 |
51 | /srv/my-mnt-title/media/tv::/data/tv
52 | ```
53 |
--------------------------------------------------------------------------------
/ffmpeg_fingerprint.py:
--------------------------------------------------------------------------------
1 | import os
2 | import re
3 | import cv2
4 | import imagehash
5 | import sys
6 | import getopt
7 | from subprocess import Popen, PIPE, SubprocessError
8 |
9 | from pathlib import Path
10 | from PIL import Image
11 | from datetime import datetime
12 |
13 | config_path = Path(os.environ['CONFIG_DIR']) if 'CONFIG_DIR' in os.environ else Path(Path.cwd() / 'config')
14 | data_path = Path(os.environ['DATA_DIR']) if 'DATA_DIR' in os.environ else Path(config_path / 'data')
15 |
16 | session_timestamp = datetime.now().strftime("%Y_%m_%d-%I_%M_%S_%p")
17 |
18 | img_extension = '.jpeg'
19 | img_size = (384, 216)
20 |
21 |
22 | def print_debug(a=[], log=True, log_file=False):
23 | # Here a is the array holding the objects
24 | # passed as the argument of the function
25 | output = ' '.join([str(elem) for elem in a])
26 | if log:
27 | print(output, file=sys.stderr)
28 | if log_file:
29 | log_path = config_path / 'logs'
30 | log_path.mkdir(parents=True, exist_ok=True)
31 | with (log_path / ('log_%s.txt' % session_timestamp)).open('a') as logger:
32 | logger.write(output + '\n')
33 |
34 |
35 | def write_fingerprint(path, fingerprint):
36 | Path(data_path / 'fingerprints' / replace(str(path))).mkdir(parents=True, exist_ok=True)
37 | path = Path(data_path / 'fingerprints' / replace(str(path)) / 'fingerprint.txt')
38 | with path.open('w+') as text_file:
39 | text_file.write(fingerprint)
40 |
41 |
42 | def replace(s):
43 | return re.sub('[^A-Za-z0-9]+', '', s)
44 |
45 |
46 | def get_frames(path, hash_fps, frame_nb, log_level, log_file):
47 | if path is None or path == '' or frame_nb == 0:
48 | return False
49 | print_debug(a=['running ffmpeg'], log=log_level > 0, log_file=log_file)
50 | start = datetime.now()
51 |
52 | command = ["ffmpeg", "-i", path, "-vf", 'fps=%s' % str(hash_fps), "-frames:v", str(frame_nb), "-f", "image2pipe", "-pix_fmt", "rgb24", "-vcodec", "rawvideo", "-s", "384x216", "-"]
53 |
54 | with Path(os.devnull).open('w') as devnull_fp:
55 | proc = Popen(command, stdout=PIPE, stderr=devnull_fp, bufsize=10**8)
56 |
57 | filein = proc.stdout
58 | bytes_list = []
59 | for _ in range(frame_nb):
60 | try:
61 | output = filein.read(img_size[0] * img_size[1] * 3)
62 | except SubprocessError as err:
63 | filein.close()
64 | proc.kill()
65 | return [], False
66 | bytes_list.append(output)
67 | filein.close()
68 | proc.wait()
69 | end = datetime.now()
70 | print_debug(a=["ran ffmpeg in %s" % str(end - start)], log=log_level > 0, log_file=log_file)
71 | return bytes_list, proc.returncode == 0
72 |
73 |
74 | def get_fingerprint_ffmpeg(path, hash_fps, frame_nb, log_level=1, log_file=False, log_timestamp=None):
75 | global session_timestamp
76 |
77 | if path is None or path == '' or frame_nb == 0:
78 | return []
79 |
80 | if log_timestamp is not None:
81 | session_timestamp = log_timestamp
82 |
83 | bytes_list, ret = get_frames(path, hash_fps, frame_nb, log_level, log_file)
84 |
85 | if not bytes_list or not ret:
86 | print_debug(a=['ffmpeg error'], log=log_level > 0, log_file=log_file)
87 | return []
88 |
89 | fingerprint_str = ""
90 | fingerprint_list = []
91 |
92 | start = datetime.now()
93 | ndx = 0
94 | for frame_bytes in bytes_list:
95 | img = Image.frombytes('RGB', img_size, frame_bytes)
96 | frame_fingerprint = imagehash.dhash(img)
97 | fingerprint_str += str(frame_fingerprint)
98 | fingerprint_list.append(frame_fingerprint)
99 | ndx += 1
100 |
101 | if fingerprint_str != '':
102 | write_fingerprint(path, fingerprint_str)
103 | end = datetime.now()
104 | print_debug(a=["made hash in %s" % str(end - start)], log=log_level > 0, log_file=log_file)
105 | return fingerprint_list
106 |
107 |
108 | def main(argv):
109 |
110 | dir = ''
111 | log_level = 0
112 | try:
113 | opts, args = getopt.getopt(argv, "hi:dvc")
114 | except getopt.GetoptError:
115 | print_debug(['decode.py -i -v (verbose - some logging) -d (debug - most logging) -c (cleanup) -s (slow mode)\n'])
116 | sys.exit(2)
117 |
118 | for opt, arg in opts:
119 | if opt == '-h':
120 | print_debug(['decode.py -i -v (verbose - some logging) -d (debug - most logging) -c (cleanup) -s (slow mode)\n'])
121 | sys.exit()
122 | elif opt == '-i':
123 | dir = arg
124 | elif opt == '-d':
125 | log_level = 2
126 | elif opt == '-v':
127 | log_level = 1
128 |
129 | if dir == '' or not Path(dir).is_dir():
130 | print_debug(['decode.py -i -v (verbose - some logging) -d (debug - most logging) -c (cleanup) -s (slow mode)\n'])
131 | sys.exit(2)
132 |
133 | common_video_extensions = ['.webm', '.mkv', '.avi', '.mts', '.m2ts', '.ts', '.mov', '.wmv', '.mp4', '.m4v', '.mpg', '.mpeg', '.m2v']
134 |
135 | if Path(dir).is_dir():
136 | start = datetime.now()
137 | for child in Path(dir).iterdir():
138 | if child.name[0] == '.':
139 | continue
140 | matched_ext = False
141 | for ext in common_video_extensions:
142 | if child.name.endswith(ext):
143 | matched_ext = True
144 | if matched_ext:
145 | max_fingerprint_mins = 10
146 | path = str(child.resolve())
147 | video = cv2.VideoCapture(path)
148 | frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
149 | fps = video.get(cv2.CAP_PROP_FPS)
150 | quarter_frames_or_first_X_mins = min(int(frames / 4), int(fps * 60 * max_fingerprint_mins))
151 | video.release()
152 | result = get_fingerprint_ffmpeg(path, quarter_frames_or_first_X_mins, log_level, True, None)
153 | end = datetime.now()
154 | print_debug(["total runtime %s" % str(end - start)])
155 | else:
156 | print_debug(['input directory invalid or cannot be accessed'])
157 | return {}
158 |
159 |
160 | if __name__ == "__main__":
161 | main(sys.argv[1:])
162 |
--------------------------------------------------------------------------------
/jellyfin.py:
--------------------------------------------------------------------------------
1 | import os
2 | import re
3 | import sys
4 | import getopt
5 |
6 | import jellyfin_queries
7 | import json
8 | import shutil
9 | import hashlib
10 |
11 |
12 | from time import sleep
13 | from math import floor
14 | from pathlib import Path
15 | from datetime import datetime
16 | from jellyfin_api_client import jellyfin_login, jellyfin_logout
17 | from decode import process_directory, read_fingerprint, create_video_fingerprint
18 |
19 | server_url = os.environ['JELLYFIN_URL'] if 'JELLYFIN_URL' in os.environ else ''
20 | server_username = os.environ['JELLYFIN_USERNAME'] if 'JELLYFIN_USERNAME' in os.environ else ''
21 | server_password = os.environ['JELLYFIN_PASSWORD'] if 'JELLYFIN_PASSWORD' in os.environ else ''
22 | env_path_map_str = os.environ['PATH_MAP'] if 'PATH_MAP' in os.environ else ''
23 | env_reverse_sort_str = os.environ['REVERSE_SORT'] if 'REVERSE_SORT' in os.environ else ''
24 | env_log_level_str = os.environ['LOG_LEVEL'] if 'LOG_LEVEL' in os.environ else ''
25 |
26 |
27 | config_path = Path(os.environ['CONFIG_DIR']) if 'CONFIG_DIR' in os.environ else Path(Path.cwd() / 'config')
28 | data_path = Path(os.environ['DATA_DIR']) if 'DATA_DIR' in os.environ else Path(config_path / 'data')
29 |
30 | minimum_episode_duration = 15 # minutes
31 | maximum_episodes_per_season = 30 # meant to skip daily shows like jeopardy
32 | sleep_after_finish_sec = 300 # sleep for 5 minutes after the script finishes. If it runs automatically this prevents it rapidly looping
33 |
34 | session_timestamp = datetime.now().strftime("%Y_%m_%d-%I_%M_%S_%p")
35 |
36 | hash_fps = 2
37 |
38 | revision_id = 2.0
39 |
40 |
41 | def print_debug(a=[], log=True, log_file=False):
42 | # Here a is the array holding the objects
43 | # passed as the argument of the function
44 | output = ' '.join([str(elem) for elem in a])
45 | if log:
46 | print(output, file=sys.stderr)
47 | if log_file:
48 | log_path = config_path / 'logs'
49 | log_path.mkdir(parents=True, exist_ok=True)
50 | with (log_path / ('log_%s.txt' % session_timestamp)).open('a') as logger:
51 | logger.write(output + '\n')
52 |
53 |
54 | def replace(s):
55 | return re.sub('[^A-Za-z0-9]+', '', s)
56 |
57 |
58 | def get_path_map(log_level=0):
59 | path_map = []
60 |
61 | if env_path_map_str != '':
62 | env_maps = env_path_map_str.strip().split(',')
63 | for m in env_maps:
64 | map = m.strip().split('::')
65 | if len(map) != 2:
66 | continue
67 | path_map.append((Path(map[0].replace('\\', '/')), Path(map[1].replace('\\', '/'))))
68 |
69 | if (config_path / 'path_map.txt').exists():
70 | with (config_path / 'path_map.txt').open('r') as file:
71 | for line in file:
72 | if line.startswith('#'):
73 | continue
74 | map = line.strip().split('::')
75 | if len(map) != 2:
76 | continue
77 | path_map.append((Path(map[0].replace('\\', '/')), Path(map[1].replace('\\', '/'))))
78 |
79 | if log_level > 1:
80 | print_debug(a=['path maps: %s' % path_map], log=True)
81 | for path in path_map:
82 | if Path(path[0]).exists() and Path(path[0]).is_dir():
83 | print_debug(a=['top level contents of mapped path [%s]' % str(path[0])], log=True)
84 | for child in Path(path[0]).iterdir():
85 | print_debug(a=[str(child.resolve())], log=True)
86 | else:
87 | print_debug(a=['mapped path [%s] isn\'t accessible' % str(path[0])], log=True)
88 |
89 | if Path(path[1]).exists() and Path(path[1]).is_dir():
90 | print_debug(a=['top level contents of mapped path [%s]' % str(path[1])], log=True)
91 | for child in Path(path[1]).iterdir():
92 | print_debug(a=[str(child.resolve())], log=True)
93 | else:
94 | print_debug(a=['mapped path [%s] isn\'t accessible' % str(path[1])], log=True)
95 |
96 | return path_map
97 |
98 |
99 | def check_season_valid(season=None, episodes=[], repair=False, debug=False):
100 | if season is None or not episodes:
101 | return []
102 |
103 | path = data_path / 'jellyfin_cache' / str(season['SeriesId']) / str(season['SeasonId'])
104 |
105 | filtered_episodes = []
106 | failed_to_find_files = False
107 |
108 | if path.exists():
109 | for episode in episodes:
110 | if not Path(episode['Path']).exists():
111 | failed_to_find_files = True
112 | continue
113 |
114 | should_add = True
115 | if Path(path / (str(episode['EpisodeId']) + '.json')).exists():
116 | should_add = False
117 | if repair:
118 | with Path(path / (str(episode['EpisodeId']) + '.json')).open('r') as json_file:
119 | profile = json.load(json_file)
120 | if 'start_frame' in profile and 'end_frame' in profile:
121 | start_frame = int(profile['start_frame'])
122 | end_frame = int(profile['end_frame'])
123 | if start_frame == 0 and end_frame == 0:
124 | should_add = True
125 | print_debug(a=['will repair ep [%s] of season [%s] of show [%s]' % (episode['Name'], season['Name'], season['SeriesName'])], log=debug, log_file=debug)
126 | if should_add:
127 | filtered_episodes.append(episode)
128 | else:
129 | for episode in episodes:
130 | if not Path(episode['Path']).exists():
131 | failed_to_find_files = True
132 | continue
133 | filtered_episodes.append(episode)
134 |
135 | if failed_to_find_files:
136 | print_debug(a=['skipping season [%s] of show [%s] - failed to access some of the media files' % (season['Name'], season['SeriesName'])], log=debug, log_file=debug)
137 | print_debug(a=['path for the first episode [%s]' % episodes[0]['Path']], log=debug, log_file=debug)
138 |
139 | if not filtered_episodes:
140 | return []
141 | if len(filtered_episodes) > maximum_episodes_per_season:
142 | print_debug(a=['skipping season [%s] of show [%s] - it contains %s episodes (more than max %s)' % (season['Name'], season['SeriesName'], len(filtered_episodes), maximum_episodes_per_season)], log=debug, log_file=debug)
143 | return []
144 |
145 | duration_mins = int(filtered_episodes[0]['Duration'])
146 | if duration_mins > 0:
147 | duration_mins = duration_mins / 60 / 1000
148 | if filtered_episodes[0]['Duration'] < minimum_episode_duration * 60 * 1000:
149 | print_debug(a=['skipping season [%s] of show [%s] - episodes are too short (%s minutes) (less than minimum %s minutes)' % (season['Name'], season['SeriesName'], duration_mins, minimum_episode_duration)], log=debug, log_file=debug)
150 | return []
151 |
152 | season_hash_exists = 1 if season['SeasonFingerprint'] is not None else 0
153 | if len(filtered_episodes) + season_hash_exists < 2:
154 | print_debug(a=['skipping season [%s] of show [%s] - it doesn\'t contain at least 2 episodes' % (season['Name'], season['SeriesName'])], log=debug, log_file=debug)
155 | return []
156 |
157 | if len(filtered_episodes) > 0 and len(episodes) - len(filtered_episodes) > 0:
158 | print_debug(a=['season [%s] of show [%s] - will skip %s of %s episodes' % (season['Name'], season['SeriesName'], len(episodes) - len(filtered_episodes), len(episodes))], log=True, log_file=debug)
159 | return filtered_episodes
160 |
161 |
162 | def get_file_paths(season=None):
163 | if season is None or 'Episodes' not in season:
164 | return []
165 |
166 | file_paths = []
167 | for episode in season['Episodes']:
168 | file_paths.append(episode['Path'])
169 | return file_paths
170 |
171 |
172 | def check_if_in_list_of_dict(dict_list, value):
173 | if dict_list is None:
174 | return -1
175 |
176 | for ndx in range(0, len(dict_list)):
177 | if value in dict_list[ndx].values():
178 | return ndx
179 | return -1
180 |
181 |
182 | def remake_season_fingerprint(episodes=[], season_fingerprint=None, debug=False):
183 | if not episodes:
184 | return None
185 |
186 | ndx = -1
187 | if 'EpisodeId' in season_fingerprint:
188 | ep_id = season_fingerprint['EpisodeId']
189 | ndx = check_if_in_list_of_dict(episodes, ep_id)
190 |
191 | if ndx == -1 and 'Name' in season_fingerprint:
192 | ndx = check_if_in_list_of_dict(episodes, season_fingerprint['Name'])
193 |
194 | if ndx == -1:
195 | trimmed_path = season_fingerprint['Path' if 'Path' in season_fingerprint else 'path'].split('/')[-1]
196 | for i in range(0, len(episodes)):
197 | if trimmed_path in str(episodes[i]['Path']):
198 | ndx = i
199 | break
200 |
201 | if ndx == -1:
202 | print_debug(a=['failed to match season fingerprint to episode in jellyfin'], log=debug, log_file=debug)
203 | return None
204 |
205 | profile = season_fingerprint
206 | profile.update(episodes[ndx])
207 | if 'path' in profile:
208 | profile.pop('path', None)
209 | profile['fingerprint'] = None
210 | profile['revision_id'] = revision_id
211 |
212 | fingerprint = create_video_fingerprint(profile, hash_fps, 2 if debug else 0, debug)
213 |
214 | if not fingerprint:
215 | print_debug(a=['failed to create new fingerprint'], log=debug, log_file=debug)
216 | return None
217 |
218 | tmp_start_frame = floor((profile['start_frame'] / profile['fps']) * hash_fps) if profile['start_frame'] > 0 else 0
219 | tmp_end_frame = floor((profile['end_frame'] / profile['fps']) * hash_fps) if profile['end_frame'] > 0 else 0
220 |
221 | try:
222 | trimmed_fingerprint = fingerprint[tmp_start_frame:tmp_end_frame + 1]
223 | fingerprint_str = ''
224 | for f in trimmed_fingerprint:
225 | fingerprint_str += str(f)
226 | profile['fingerprint'] = fingerprint_str
227 | profile['hash_fps'] = hash_fps
228 | return profile
229 | except BaseException as err:
230 | print_debug(a=['failed to trim new fingerprint'], log=debug, log_file=debug)
231 | return None
232 |
233 |
234 | def intro_duration(profile):
235 | intro_duration = profile['end_frame'] - profile['start_frame']
236 | if intro_duration < 0:
237 | intro_duration = 0
238 | return intro_duration
239 |
240 |
241 | def get_season_fingerprint(season=None, episodes=[], debug=False):
242 | if season is None:
243 | return None
244 |
245 | season_fp_dict = None
246 | path = Path(data_path / 'jellyfin_cache' / str(season['SeriesId']) / str(season['SeasonId']) / ('season' + '.json'))
247 | if path.exists():
248 | with path.open('r') as json_file:
249 | season_fp_dict = json.load(json_file)
250 |
251 | if season_fp_dict is None:
252 | return None
253 |
254 | fingerprint_list = []
255 | if 'revision_id' in season_fp_dict and season_fp_dict['revision_id'] == revision_id \
256 | and 'fingerprint' in season_fp_dict and \
257 | 'hash_fps' in season_fp_dict and season_fp_dict['hash_fps'] == hash_fps:
258 | fingerprint_list = read_fingerprint(season_fp_dict['fingerprint'], 2 if debug else 0, debug)
259 |
260 | profile_modified = False
261 |
262 | if fingerprint_list and abs(len(fingerprint_list) - floor(intro_duration(season_fp_dict) / (season_fp_dict['fps'] / hash_fps))) > 2:
263 | print_debug(a=['season fingerprint is wrong length %s instead of %s for season %s of show %s' % (len(fingerprint_list),
264 | floor(intro_duration(season_fp_dict) / (season_fp_dict['fps'] / hash_fps)),
265 | season['Name'], season['SeriesName'])], log=debug, log_file=debug)
266 |
267 | if not fingerprint_list or abs(len(fingerprint_list) - floor(intro_duration(season_fp_dict) / (season_fp_dict['fps'] / hash_fps))) > 2:
268 | print_debug(a=['trying to remake season fingerprint for season %s of show %s' % (season['Name'], season['SeriesName'])], log=debug, log_file=debug)
269 | season_fp_dict = remake_season_fingerprint(episodes, season_fp_dict, debug)
270 | profile_modified = True
271 | else:
272 | print_debug(a=['found valid season fingerprint for season %s of show %s' % (season['Name'], season['SeriesName'])], log=debug, log_file=debug)
273 |
274 | if profile_modified and season_fp_dict is not None:
275 | with path.open('w+') as json_file:
276 | json.dump(season_fp_dict, json_file, indent=4)
277 | return season_fp_dict
278 |
279 |
280 | def get_jellyfin_shows(reverse_sort=False, repair=False, log_level=0, log_file=False):
281 | if server_url == '' or server_username == '' or server_password == '':
282 | print_debug(a=['missing server info'])
283 | return
284 |
285 | path_map = get_path_map(log_level)
286 |
287 | client = jellyfin_login(server_url, server_username, server_password, "TV Intro Detection Scanner")
288 | shows_query = jellyfin_queries.get_shows(client, path_map, reverse_sort)
289 | if not shows_query:
290 | print_debug(a=['Error - got 0 shows from jellyfin'])
291 | return []
292 | print_debug(a=['jellyfin has %s shows' % len(shows_query)])
293 | if repair:
294 | print_debug(a=['repair mode is enabled'])
295 |
296 | shows = []
297 | season_count = 0
298 | episode_count = 0
299 | for show in shows_query:
300 | should_skip_series = False
301 | if 'Path' in show and show['Path'] and Path(show['Path']).is_dir():
302 | for child in Path(show['Path']).iterdir():
303 | if child.name == '.ignore-intros':
304 | print_debug(a=['ignoring series [%s]' % show['Name']], log=log_level > 0)
305 | should_skip_series = True
306 | break
307 | if should_skip_series:
308 | continue
309 |
310 | seasons = []
311 | seasons_query = jellyfin_queries.get_seasons(client, path_map, show)
312 |
313 | if not seasons_query:
314 | continue
315 |
316 | for season in seasons_query:
317 | should_skip_season = False
318 | if 'Path' in season and season['Path'] and Path(season['Path']).is_dir():
319 | for child in Path(season['Path']).iterdir():
320 | if child.name == '.ignore-intros':
321 | print_debug(a=['ignoring season [%s] of show [%s]' % (season['Name'], show['Name'])], log=log_level > 0)
322 | should_skip_season = True
323 | break
324 | if should_skip_season:
325 | continue
326 |
327 | episodes = jellyfin_queries.get_episodes(client, path_map, season)
328 | if not episodes:
329 | continue
330 |
331 | season['SeasonFingerprint'] = get_season_fingerprint(season=season, episodes=episodes, debug=log_level > 1)
332 |
333 | season['Episodes'] = check_season_valid(season, episodes, repair, debug=log_level > 1)
334 | if season['Episodes']:
335 | episode_count += len(season['Episodes'])
336 | seasons.append(season)
337 | if seasons:
338 | season_count += len(seasons)
339 | show['Seasons'] = seasons
340 | shows.append(show)
341 |
342 | jellyfin_logout()
343 |
344 | print_debug(a=['found %s qualifying shows' % len(shows)], log_file=log_file and episode_count > 0)
345 | print_debug(a=['found %s qualifying seasons' % season_count], log_file=log_file and episode_count > 0)
346 | print_debug(a=['found %s qualifying episodes\n' % episode_count], log_file=log_file and episode_count > 0)
347 |
348 | return shows
349 |
350 |
351 | def copy_season_fingerprint(result: list = [], dir_path: Path = None, debug: bool = False, log_file: bool = False):
352 | if not result or dir_path is None:
353 | return
354 |
355 | name = ''
356 | for profile in result:
357 | name += replace(profile['Path'])
358 | hash_object = hashlib.md5(name.encode())
359 | name = hash_object.hexdigest()
360 |
361 | src_path = Path(data_path / 'fingerprints' / (name + '.json'))
362 | dst_path = Path(dir_path / ('season' + '.json'))
363 | dir_path.mkdir(parents=True, exist_ok=True)
364 | if src_path.exists():
365 | if debug:
366 | print_debug(a=['saving season fingerprint to jellyfin_cache'], log_file=log_file)
367 | shutil.copyfile(src_path, dst_path)
368 |
369 |
370 | def save_season(season=None, result=None, save_json=False, debug=False, log_file=False):
371 | if not result or season is None:
372 | return
373 | path = data_path / 'jellyfin_cache' / str(season['SeriesId']) / str(season['SeasonId'])
374 | if save_json:
375 | copy_season_fingerprint(result, path, debug, log_file)
376 |
377 | for ndx in range(0, len(season['Episodes'])):
378 | if ndx >= len(result):
379 | if debug:
380 | print_debug(a=['episode index past bounds of result'], log_file=log_file)
381 | break
382 | if season['Episodes'][ndx]['Path'] == result[ndx]['Path']:
383 | season['Episodes'][ndx].update(result[ndx])
384 | print(season['Episodes'][ndx])
385 | season['Episodes'][ndx]['created'] = str(datetime.now())
386 | if save_json:
387 | Path(path).mkdir(parents=True, exist_ok=True)
388 | with Path(path / (str(season['Episodes'][ndx]['EpisodeId']) + '.json')).open('w+') as json_file:
389 | json.dump(season['Episodes'][ndx], json_file, indent=4)
390 | elif debug:
391 | print_debug(a=['index mismatch'], log_file=log_file)
392 |
393 |
394 | def process_jellyfin_shows(log_level=0, log_file=False, save_json=False, reverse_sort=False, repair=False):
395 | start = datetime.now()
396 | print_debug(a=["\n\nstarted new session at %s\n" % start])
397 | if reverse_sort:
398 | print_debug(['will process shows in reverse order'])
399 |
400 | shows = get_jellyfin_shows(reverse_sort, repair, log_level, log_file)
401 |
402 | if (data_path / 'fingerprints').is_dir():
403 | try:
404 | shutil.rmtree(data_path / 'fingerprints')
405 | except OSError as e:
406 | print_debug(a=["Error: %s : %s" % ('deleting fingerprints directory', e.strerror)], log_file=log_file)
407 |
408 | show_ndx = 0
409 | total_processed = 0
410 | for show in shows:
411 | show_ndx += 1
412 | show_start_time = datetime.now()
413 |
414 | print_debug(a=['%s/%s - %s' % (show_ndx, len(shows), show['Name'])], log_file=log_file)
415 |
416 | season_ndx = 1
417 | for season in show['Seasons']:
418 | season_ndx += 1
419 |
420 | season_start_time = datetime.now()
421 | file_paths = get_file_paths(season)
422 | result = []
423 |
424 | if file_paths:
425 | print_debug(a=['%s/%s - %s - %s episodes' % (season_ndx, len(show['Seasons']), season['Name'], len(season['Episodes']))], log_file=log_file)
426 | result = process_directory(profiles=season['Episodes'], ref_profile=season['SeasonFingerprint'], hashfps=hash_fps,
427 | cleanup=False, log_level=log_level, log_file=log_file, log_timestamp=session_timestamp)
428 | if result:
429 | save_season(season, result, save_json, log_level > 0, log_file)
430 | total_processed += len(result)
431 | else:
432 | print_debug(a=['no results - the decoder may not have access to the specified media files'], log_file=log_file)
433 | if (data_path / 'fingerprints').is_dir():
434 | try:
435 | shutil.rmtree(data_path / 'fingerprints')
436 | except OSError as e:
437 | print_debug(a=["Error: %s : %s" % ('deleting fingerprints directory', e.strerror)], log_file=log_file)
438 | season_end_time = datetime.now()
439 | if result:
440 | print_debug(a=['processed season [%s] in %s' % (season['Name'], str(season_end_time - season_start_time))], log_file=log_file)
441 | if file_paths:
442 | sleep(2)
443 | show_end_time = datetime.now()
444 | print_debug(a=['processed show [%s] in %s\n' % (show['Name'], str(show_end_time - show_start_time))], log_file=log_file)
445 |
446 | end = datetime.now()
447 | print_debug(a=["total runtime: " + str(end - start)], log_file=log_file and total_processed > 0)
448 | if sleep_after_finish_sec > 0:
449 | print_debug(a=['sleeping for %s seconds' % sleep_after_finish_sec])
450 | sleep(300)
451 |
452 |
453 | def main(argv):
454 | log_level = 0
455 | save_json = False
456 | log = False
457 | reverse_sort = False
458 | repair = False
459 |
460 | try:
461 | opts, args = getopt.getopt(argv, "hdvjlr", ["reverse", "repair"])
462 | except getopt.GetoptError:
463 | print_debug(['jellyfin.py -d (debug) -j (save json) -l (log to file)'])
464 | print_debug(['saving to json is currently the only way to skip previously processed files in subsequent runs\n'])
465 | sys.exit(2)
466 |
467 | for opt, arg in opts:
468 | if opt == '-h':
469 | print_debug(['jellyfin.py -d (debug) -j (save json) -l (log to file)'])
470 | print_debug(['saving to json is currently the only way to skip previously processed files in subsequent runs\n'])
471 | sys.exit()
472 | elif opt == '-d':
473 | log_level = 2
474 | elif opt == '-v':
475 | log_level = 1
476 | elif opt == '-j':
477 | save_json = True
478 | elif opt == '-l':
479 | log = True
480 | elif opt in ("-r", "--reverse"):
481 | reverse_sort = True
482 | elif opt in ("--repair"):
483 | repair = True
484 |
485 | if server_url == '' or server_username == '' or server_password == '':
486 | print_debug(['you need to export env variables: JELLYFIN_URL, JELLYFIN_USERNAME, JELLYFIN_PASSWORD\n'])
487 | return
488 |
489 | if env_reverse_sort_str == 'TRUE':
490 | reverse_sort = True
491 | elif env_reverse_sort_str == 'FALSE':
492 | reverse_sort = False
493 |
494 | if env_log_level_str == 'DEBUG':
495 | log_level = 2
496 | elif env_log_level_str == 'VERBOSE':
497 | log_level = 1
498 | elif env_log_level_str == 'INFO':
499 | log_level = 0
500 | process_jellyfin_shows(log_level, log, save_json, reverse_sort, repair)
501 |
502 |
503 | if __name__ == "__main__":
504 | main(sys.argv[1:])
505 |
--------------------------------------------------------------------------------
/jellyfin_api_client.py:
--------------------------------------------------------------------------------
1 | '''
2 | Big thanks to the jellyfin/jellyfin-mpv-shim devs for most of the code in this file!
3 | Adapted from:
4 | https://github.com/jellyfin/jellyfin-mpv-shim/blob/ed8a61d6984c79ac81ef9db1f84af940ca036e0f/jellyfin_mpv_shim/clients.py
5 | Main project:
6 | https://github.com/jellyfin/jellyfin-mpv-shim
7 | '''
8 |
9 | import sys
10 | import json
11 | import uuid as UUID
12 | import time
13 | import logging
14 | import re
15 | import os
16 |
17 | from pathlib import Path
18 | from jellyfin_apiclient_python import JellyfinClient
19 | from jellyfin_apiclient_python.connection_manager import CONNECTION_STATE
20 | from getpass import getpass
21 | from typing import Optional
22 |
23 | # server_url = os.environ['JELLYFIN_URL']
24 | # server_username = os.environ['JELLYFIN_USERNAME']
25 | # server_password = os.environ['JELLYFIN_PASSWORD']
26 |
27 | jellyfin_client_manager = None
28 | jellyfin_current_client = None
29 |
30 | global_app_name = "tv-intro-detection"
31 | global_user_app_name = "TV Intro Detection"
32 |
33 | CLIENT_VERSION = "0.0.1"
34 | USER_AGENT = "%s/%s" % (global_user_app_name, CLIENT_VERSION)
35 | CAPABILITIES = {
36 | "PlayableMediaTypes": "",
37 | "SupportsMediaControl": False,
38 | "SupportedCommands": (),
39 | }
40 |
41 | connect_retry_mins = 0
42 |
43 | ignore_ssl_cert = False
44 | config_path = Path(os.environ['CONFIG_DIR']) if 'CONFIG_DIR' in os.environ else Path(Path.cwd() / 'config')
45 |
46 | credentials_location = Path(config_path / (global_app_name + '-cred.json'))
47 |
48 | log = logging.getLogger("clients")
49 | path_regex = re.compile("^(https?://)?([^/:]+)(:[0-9]+)?(/.*)?$")
50 |
51 |
52 | def expo(max_value: Optional[int] = None):
53 | n = 0
54 | while True:
55 | a = 2 ** n
56 | if max_value is None or a < max_value:
57 | yield a
58 | n += 1
59 | else:
60 | yield max_value
61 |
62 |
63 | class ClientManager(object):
64 | def __init__(self):
65 | self.callback = lambda client, event_name, data: None
66 | self.credentials = []
67 | self.clients = {}
68 | self.usernames = {}
69 | self.is_stopping = False
70 |
71 | def cli_connect(self):
72 | is_logged_in = self.try_connect()
73 | add_another = False
74 |
75 | if "add" in sys.argv:
76 | add_another = True
77 |
78 | while not is_logged_in or add_another:
79 | server = input(_("Server URL: "))
80 | username = input(_("Username: "))
81 | password = getpass(_("Password: "))
82 |
83 | is_logged_in = self.login(server, username, password)
84 |
85 | if is_logged_in:
86 | log.info(_("Successfully added server."))
87 | add_another = input(_("Add another server?") + " [y/N] ")
88 | add_another = add_another in ("y", "Y", "yes", "Yes")
89 | else:
90 | log.warning(_("Adding server failed."))
91 |
92 | @staticmethod
93 | def client_factory():
94 | uuid = str(UUID.uuid4())
95 | if Path(config_path / (global_app_name + '-uuid.txt')).exists():
96 | with Path(config_path / (global_app_name + '-uuid.txt')).open('r') as uuid_file:
97 | uuid = uuid_file.read()
98 | else:
99 | with Path(config_path / (global_app_name + '-uuid.txt')).open('w+') as uuid_file:
100 | uuid_file.write(uuid)
101 | client = JellyfinClient(allow_multiple_clients=True)
102 | client.config.data["app.default"] = True
103 | client.config.app(
104 | global_user_app_name, CLIENT_VERSION, global_user_app_name, uuid
105 | )
106 | client.config.data["http.user_agent"] = USER_AGENT
107 | client.config.data["auth.ssl"] = not ignore_ssl_cert
108 | return client
109 |
110 | def _connect_all(self):
111 | is_logged_in = False
112 | for server in self.credentials:
113 | if self.connect_client(server):
114 | is_logged_in = True
115 | return is_logged_in
116 |
117 | def try_connect(self):
118 | if credentials_location.exists():
119 | with credentials_location.open('r') as cf:
120 | self.credentials = json.load(cf)
121 |
122 | if "Servers" in self.credentials:
123 | credentials_old = self.credentials
124 | self.credentials = []
125 | for server in credentials_old["Servers"]:
126 | server["uuid"] = str(UUID.uuid4())
127 | server["username"] = ""
128 | self.credentials.append(server)
129 |
130 | is_logged_in = self._connect_all()
131 | if connect_retry_mins and not is_logged_in:
132 | log.warning(
133 | "Connection failed. Will retry for {0} minutes.".format(
134 | connect_retry_mins
135 | )
136 | )
137 | for attempt in range(connect_retry_mins * 2):
138 | time.sleep(30)
139 | is_logged_in = self._connect_all()
140 | if is_logged_in:
141 | break
142 |
143 | return is_logged_in
144 |
145 | def save_credentials(self):
146 | if credentials_location.exists():
147 | with credentials_location.open('w') as cf:
148 | json.dump(self.credentials, cf)
149 |
150 | def login(
151 | self, server: str, username: str, password: str, force_unique: bool = False
152 | ):
153 | if server.endswith("/"):
154 | server = server[:-1]
155 |
156 | protocol, host, port, path = path_regex.match(server).groups()
157 |
158 | if not protocol:
159 | log.warning("Adding http:// because it was not provided.")
160 | protocol = "http://"
161 |
162 | if protocol == "http://" and not port:
163 | log.warning("Adding port 8096 for insecure local http connection.")
164 | log.warning(
165 | "If you want to connect to standard http port 80, use :80 in the url."
166 | )
167 | port = ":8096"
168 |
169 | server = "".join(filter(bool, (protocol, host, port, path)))
170 |
171 | client = self.client_factory()
172 | client.auth.connect_to_address(server)
173 | result = client.auth.login(server, username, password)
174 | if "AccessToken" in result:
175 | credentials = client.auth.credentials.get_credentials()
176 | server = credentials["Servers"][0]
177 | if force_unique:
178 | server["uuid"] = server["Id"]
179 | else:
180 | server["uuid"] = str(UUID.uuid4())
181 | server["username"] = username
182 | if force_unique and server["Id"] in self.clients:
183 | return client
184 | self.connect_client(server)
185 | self.credentials.append(server)
186 | self.save_credentials()
187 | return client
188 | return None
189 |
190 | def setup_client(self, client: "JellyfinClient", server):
191 | def event(event_name, data):
192 | if event_name == "WebSocketDisconnect":
193 | timeout_gen = expo(100)
194 | if server["uuid"] in self.clients:
195 | while not self.is_stopping:
196 | timeout = next(timeout_gen)
197 | log.info(
198 | "No connection to server. Next try in {0} second(s)".format(
199 | timeout
200 | )
201 | )
202 | self._disconnect_client(server=server)
203 | time.sleep(timeout)
204 | if self.connect_client(server):
205 | break
206 | else:
207 | self.callback(client, event_name, data)
208 |
209 | client.callback = event
210 | client.callback_ws = event
211 | client.start(websocket=True)
212 |
213 | client.jellyfin.post_capabilities(CAPABILITIES)
214 |
215 | def remove_client(self, uuid: str):
216 | self.credentials = [
217 | server for server in self.credentials if server["uuid"] != uuid
218 | ]
219 | self.save_credentials()
220 | self._disconnect_client(uuid=uuid)
221 |
222 | def connect_client(self, server):
223 | if self.is_stopping:
224 | return False
225 |
226 | is_logged_in = False
227 | client = self.client_factory()
228 | state = client.authenticate({"Servers": [server]}, discover=False)
229 | server["connected"] = state["State"] == CONNECTION_STATE["SignedIn"]
230 | if server["connected"]:
231 | is_logged_in = True
232 | self.clients[server["uuid"]] = client
233 | self.setup_client(client, server)
234 | if server.get("username"):
235 | self.usernames[server["uuid"]] = server["username"]
236 |
237 | return is_logged_in
238 |
239 | def _disconnect_client(self, uuid: Optional[str] = None, server=None):
240 | if uuid is None and server is not None:
241 | uuid = server["uuid"]
242 |
243 | if uuid not in self.clients:
244 | return
245 |
246 | if server is not None:
247 | server["connected"] = False
248 |
249 | client = self.clients[uuid]
250 | del self.clients[uuid]
251 | client.stop()
252 |
253 | def remove_all_clients(self):
254 | self.stop_all_clients()
255 | self.credentials = []
256 | self.save_credentials()
257 |
258 | def stop_all_clients(self):
259 | for key, client in list(self.clients.items()):
260 | del self.clients[key]
261 | client.stop()
262 |
263 | def stop(self):
264 | self.is_stopping = True
265 | for client in self.clients.values():
266 | client.stop()
267 |
268 | def get_username_from_client(self, client):
269 | # This is kind of convoluted. It may fail if a server
270 | # was added before we started saving usernames.
271 | for uuid, client2 in self.clients.items():
272 | if client2 is client:
273 | if uuid in self.usernames:
274 | return self.usernames[uuid]
275 | for server in self.credentials:
276 | if server["uuid"] == uuid:
277 | return server.get("username", "Unknown")
278 | break
279 |
280 | return "Unknown"
281 |
282 |
283 | def initialize_jellyfin_api_client():
284 | global jellyfin_client_manager
285 | jellyfin_client_manager = ClientManager()
286 |
287 |
288 | def jellyfin_login(server_url, server_username, server_password, app_name=None):
289 | global jellyfin_client_manager
290 | global jellyfin_current_client
291 | global global_app_name
292 | global global_user_app_name
293 |
294 | if app_name is not None:
295 | global_user_app_name = app_name
296 | global_app_name = re.sub('[^0-9a-zA-Z]', '-', app_name)
297 |
298 | if jellyfin_client_manager is not None:
299 | jellyfin_logout()
300 | initialize_jellyfin_api_client()
301 | jellyfin_current_client = jellyfin_client_manager.login(server_url, server_username, server_password)
302 | return jellyfin_current_client
303 |
304 |
305 | def jellyfin_logout():
306 | global jellyfin_client_manager
307 | if jellyfin_client_manager is not None:
308 | jellyfin_client_manager.stop()
309 | jellyfin_client_manager = None
310 |
311 |
312 | def jellyfin_client():
313 | global jellyfin_current_client
314 |
315 | if jellyfin_current_client is None:
316 | jellyfin_login()
317 | return jellyfin_current_client
318 |
--------------------------------------------------------------------------------
/jellyfin_auto_skip.py:
--------------------------------------------------------------------------------
1 | import os
2 | import json
3 | import arrow
4 | import signal
5 | import sys
6 | import getopt
7 |
8 | from pathlib import Path
9 | from time import sleep
10 | from datetime import datetime, timezone
11 | from jellyfin_api_client import jellyfin_login, jellyfin_logout
12 |
13 | server_url = os.environ['JELLYFIN_URL'] if 'JELLYFIN_URL' in os.environ else ''
14 | server_username = os.environ['JELLYFIN_USERNAME'] if 'JELLYFIN_USERNAME' in os.environ else ''
15 | server_password = os.environ['JELLYFIN_PASSWORD'] if 'JELLYFIN_PASSWORD' in os.environ else ''
16 |
17 | mon_all_users = os.environ['MONITOR_ALL_USERS'] if 'MONITOR_ALL_USERS' in os.environ else ''
18 | env_cooldown_str = os.environ['AUTO_SKIP_COOLDOWN'] if 'AUTO_SKIP_COOLDOWN' in os.environ else ''
19 |
20 | config_path = Path(os.environ['CONFIG_DIR']) if 'CONFIG_DIR' in os.environ else Path(Path.cwd() / 'config')
21 | data_path = Path(os.environ['DATA_DIR']) if 'DATA_DIR' in os.environ else Path(config_path / 'data')
22 |
23 | TICKS_PER_MS = 10000
24 | preroll_seconds = 3
25 | minimum_intro_length = 10 # seconds
26 | cooldown_time = 5
27 |
28 | client = None
29 | should_exit = False
30 |
31 | SESSIONS_CULL_COOLDOWN = 300
32 | SESSION_CULL_STALE_AGE = 60
33 | active_sessions = {}
34 | last_sessions_cull = datetime.now(timezone.utc)
35 |
36 |
37 | def monitor_sessions(monitor_all_users=False):
38 | global should_exit
39 | global active_sessions
40 |
41 | if client is None:
42 | return False
43 |
44 | start = datetime.now(timezone.utc)
45 | try:
46 | sessions = client.jellyfin.sessions()
47 | except BaseException as err:
48 | should_exit = True
49 | print("error communicating with the server %s" % err)
50 | print('will exit')
51 | return False
52 |
53 | for session in sessions:
54 | if not monitor_all_users and session['UserId'] != client.auth.jellyfin_user_id():
55 | continue
56 | if 'PlayState' not in session or session['PlayState']['CanSeek'] is False:
57 | continue
58 | if 'Capabilities' not in session or session['Capabilities']['SupportsMediaControl'] is False:
59 | continue
60 | if 'LastPlaybackCheckIn' not in session:
61 | continue
62 | if 'NowPlayingItem' not in session:
63 | continue
64 |
65 | sessionId = session['Id']
66 |
67 | # print('user id %s' % session['UserId'])
68 | print('\nclient: [%s] session: [%s]' % (session['DeviceName'], sessionId))
69 |
70 | lastPlaybackTime = arrow.get(session['LastPlaybackCheckIn']).to('utc').datetime
71 | timeDiff = start - lastPlaybackTime
72 |
73 | item = session['NowPlayingItem']
74 | print('seconds since last client playback check in: %s' % timeDiff.seconds)
75 | if not session['PlayState']['IsPaused'] and timeDiff.seconds < 8 and 'Id' in item:
76 | if 'SeriesName' in item and 'SeasonName' in item and 'Name' in item:
77 | print('currently playing %s - %s - Episode [%s]' % (item['SeriesName'], item['SeasonName'], item['Name']))
78 | print('item id %s' % item['Id'])
79 | else:
80 | print('not playing or hasn\'t checked in')
81 | continue
82 |
83 | if 'SeriesId' not in item or 'SeasonId' not in item:
84 | print('playing item isn\'t a series')
85 | continue
86 |
87 | position_ticks = int(session['PlayState']['PositionTicks'])
88 | print('current position %s minutes' % (((position_ticks / TICKS_PER_MS) / 1000) / 60))
89 |
90 | file_path = Path(data_path / 'jellyfin_cache' / str(item['SeriesId']) / str(item['SeasonId']) / (str(item['Id']) + '.json'))
91 | start_time_ticks = 0
92 | end_time_ticks = 0
93 | if file_path.exists():
94 | with file_path.open('r') as json_file:
95 | dict = json.load(json_file)
96 | if 'start_time_ms' in dict and 'end_time_ms' in dict:
97 | start_time_ticks = int(dict['start_time_ms']) * TICKS_PER_MS
98 | end_time_ticks = int(dict['end_time_ms']) * TICKS_PER_MS
99 | else:
100 | print('couldn\'t find json file for item. This likely means the episode hasn\'t been processed - checked for file [%s]' % str(file_path))
101 | continue
102 |
103 | if start_time_ticks == 0 and end_time_ticks == 0:
104 | print('no useable intro data - start_time and end_time are both 0')
105 | continue
106 |
107 | print('pos %ss intro start %ss end %ss' % (position_ticks / TICKS_PER_MS / 1000, start_time_ticks / TICKS_PER_MS / 1000, end_time_ticks / TICKS_PER_MS / 1000))
108 |
109 | # ignore any weird timestamps/check in times that sometimes show up when starting playback and seeking
110 | # todo: fix this absolute mess
111 | failedCheck = True
112 | if sessionId in active_sessions:
113 | cachedLastPlaybackTime, cachedPositionTicks = active_sessions[sessionId]
114 | cachedLastPlaybackTimeDiff = lastPlaybackTime - cachedLastPlaybackTime
115 | print('diff between client check in timestamps %s' % cachedLastPlaybackTimeDiff)
116 |
117 | posTicksDiff = position_ticks - cachedPositionTicks
118 | posTimeDiff = start - cachedLastPlaybackTime
119 | if cachedLastPlaybackTimeDiff.seconds < 0 or cachedLastPlaybackTimeDiff.seconds > cooldown_time + 8:
120 | print('bad session continuity in check in time, ignoring')
121 | elif posTicksDiff < 0 or posTicksDiff > (posTimeDiff.seconds + 2) * 1000 * TICKS_PER_MS:
122 | print('bad session continuity in position time, ignoring')
123 | print('posTicksDiff %s should be less than %s' % (posTicksDiff, (posTimeDiff.seconds + 2) * 1000 * TICKS_PER_MS))
124 | else:
125 | failedCheck = False
126 | active_sessions[sessionId] = (lastPlaybackTime, position_ticks)
127 |
128 | if failedCheck or position_ticks < start_time_ticks or position_ticks > end_time_ticks:
129 | continue
130 |
131 | if position_ticks < TICKS_PER_MS * 500:
132 | print('position is less than 0.5 seconds, ignoring to prevent skipping while buffering')
133 | continue
134 |
135 | if end_time_ticks - start_time_ticks < minimum_intro_length * 1000 * TICKS_PER_MS:
136 | print('ignoring episode - intro is less than %ss' % minimum_intro_length)
137 | continue
138 |
139 | preroll_ticks = preroll_seconds * 1000 * TICKS_PER_MS
140 | if end_time_ticks - preroll_ticks >= 0:
141 | end_time_ticks -= preroll_ticks
142 |
143 | print('trying to send seek to client')
144 | client.jellyfin.sessions(handler="/%s/Message" % sessionId, action="POST", json={
145 | "Text": "Skipping Intro",
146 | "TimeoutMs": 5000
147 | })
148 |
149 | sleep(1)
150 | params = {
151 | "SeekPositionTicks": end_time_ticks
152 | }
153 | client.jellyfin.sessions(handler="/%s/Playing/seek" % sessionId, action="POST", params=params)
154 | sleep(10)
155 | return True
156 |
157 |
158 | def init_client():
159 | global client
160 | global should_exit
161 |
162 | print('initializing client')
163 | if client is not None and not should_exit:
164 | try:
165 | jellyfin_logout()
166 | except BaseException as err:
167 | print("error communicating with the server %s" % err)
168 | print('will exit')
169 | should_exit = True
170 | client = None
171 | return
172 | if not should_exit:
173 | sleep(1)
174 | try:
175 | client = jellyfin_login(server_url, server_username, server_password, "TV Intro Auto Skipper")
176 | except BaseException as err:
177 | print("error communicating with the server %s" % err)
178 | print('will exit')
179 | should_exit = True
180 |
181 |
182 | def monitor_loop(monitor_all_users=False):
183 | global should_exit
184 | global active_sessions
185 | global last_sessions_cull
186 |
187 | if server_url == '' or server_username == '' or server_password == '':
188 | print('missing server info')
189 | return
190 |
191 | init_client()
192 | if should_exit:
193 | return
194 |
195 | print('will monitor [%s]' % ('all users' if monitor_all_users else 'current user'))
196 | print('cooldown between check-ins is set to %s seconds' % cooldown_time)
197 | print('listening for jellyfin sessions...')
198 | while not should_exit:
199 | if not monitor_sessions(monitor_all_users) and not should_exit:
200 | init_client()
201 | currTime = datetime.now(timezone.utc)
202 | sessionCullTimeDiff = currTime - last_sessions_cull
203 | if sessionCullTimeDiff.seconds > SESSIONS_CULL_COOLDOWN:
204 | print('\nchecking for stale sessions')
205 | sessionsToRemove = []
206 | for sessionId in active_sessions:
207 | cachedLastPlaybackTime, cachedPositionTicks = active_sessions[sessionId]
208 | playbackCheckinDiff = currTime - cachedLastPlaybackTime
209 | if playbackCheckinDiff.seconds > SESSION_CULL_STALE_AGE:
210 | sessionsToRemove.append(sessionId)
211 | for sessionToRemove in sessionsToRemove:
212 | if sessionToRemove in active_sessions:
213 | active_sessions.pop(sessionToRemove)
214 | print('removed stale session %s' % sessionId)
215 | last_sessions_cull = datetime.now(timezone.utc)
216 | sleep(cooldown_time)
217 |
218 | if client is not None:
219 | try:
220 | jellyfin_logout()
221 | except BaseException as err:
222 | print("error communicating with the server %s" % err)
223 | print('will exit')
224 | return
225 |
226 |
227 | def main(argv):
228 | global cooldown_time
229 |
230 | all_users = mon_all_users
231 |
232 | try:
233 | opts, args = getopt.getopt(argv, 'hac:', ['cooldown'])
234 | except getopt.GetoptError:
235 | print('jellyfin_auto_skip.py -a (all users)\n')
236 | sys.exit(2)
237 |
238 | for opt, arg in opts:
239 | if opt == '-h':
240 | print('jellyfin_auto_skip.py -a (all users)\n')
241 | sys.exit()
242 | elif opt == '-a':
243 | all_users = True
244 | elif opt in ('-c', '--cooldown'):
245 | if arg != '' and arg.isnumeric():
246 | cooldown_nb = int(arg)
247 | if cooldown_nb > 0 and cooldown_nb < 300:
248 | cooldown_time = cooldown_nb
249 |
250 | if server_url == '' or server_username == '' or server_password == '':
251 | print('you need to export env variables: JELLYFIN_URL, JELLYFIN_USERNAME, JELLYFIN_PASSWORD\n')
252 | return
253 |
254 | if mon_all_users == 'TRUE':
255 | all_users = True
256 | elif mon_all_users == 'FALSE':
257 | all_users = False
258 |
259 | if env_cooldown_str != '' and env_cooldown_str.isnumeric():
260 | cooldown_nb = int(env_cooldown_str)
261 | if cooldown_nb > 0 and cooldown_nb < 300:
262 | cooldown_time = cooldown_nb
263 |
264 | monitor_loop(all_users)
265 |
266 |
267 | def receiveSignal(signalNumber, frame):
268 | global should_exit
269 |
270 | if signalNumber == signal.SIGINT:
271 | print('will exit')
272 | should_exit = True
273 | return
274 |
275 |
276 | if __name__ == "__main__":
277 | signal.signal(signal.SIGINT, receiveSignal)
278 | main(sys.argv[1:])
279 |
--------------------------------------------------------------------------------
/jellyfin_queries.py:
--------------------------------------------------------------------------------
1 | from time import sleep
2 | from pathlib import Path
3 |
4 |
5 | def map_path(path, path_map):
6 | new_path = path
7 |
8 | repr_path = path.replace('\\', '/')
9 | for mapping in path_map:
10 | path_map_parts_list = list(Path(mapping[1]).parts)
11 | jellyfin_path_parts_list = list(Path(repr_path).parts)
12 |
13 | path_map_parts = set(path_map_parts_list)
14 | jellyfin_path_parts = set(jellyfin_path_parts_list)
15 |
16 | if path_map_parts.issubset(jellyfin_path_parts):
17 | new_path = str(Path(mapping[0]).joinpath(Path(*jellyfin_path_parts_list[len(path_map_parts_list):])))
18 | break
19 | return new_path
20 |
21 |
22 | def get_shows(client=None, path_map=[], reverse_sort=False):
23 | if client is None:
24 | return []
25 |
26 | shows = []
27 |
28 | try:
29 | result = client.jellyfin.user_items(params={
30 | 'Recursive': True,
31 | 'includeItemTypes': (
32 | "Series"
33 | ),
34 | 'SortBy': 'DateCreated,SortName',
35 | 'SortOrder': 'Ascending' if reverse_sort else 'Descending',
36 | 'enableImages': False,
37 | 'enableUserData': False,
38 | 'Fields': (
39 | "Path"
40 | ),
41 | # added this limit for safety in case someone runs this without understanding what it does
42 | # remove the limit to process all shows
43 | # 'Limit': 1
44 | })
45 |
46 | if 'Items' in result and result['Items']:
47 | for item in result['Items']:
48 | show = {}
49 | show['Name'] = item['Name']
50 | show['SeriesId'] = item['Id']
51 | show['Path'] = map_path(item['Path'], path_map) if 'Path' in item else None
52 | shows.append(show)
53 | except BaseException as err:
54 | return []
55 | sleep(0.2)
56 | return shows
57 |
58 |
59 | def get_seasons(client=None, path_map=[], series=None):
60 | if client is None or series is None:
61 | return []
62 |
63 | seasons = []
64 |
65 | try:
66 | result = client.jellyfin.get_seasons(series['SeriesId'])
67 |
68 | if 'Items' in result and result['Items']:
69 | for item in result['Items']:
70 | season = {}
71 | season['Name'] = item['Name']
72 | season['SeriesName'] = series['Name']
73 | season['SeriesId'] = series['SeriesId']
74 | season['SeasonId'] = item['Id']
75 | season['Path'] = map_path(item['Path'], path_map) if 'Path' in item else None
76 | seasons.append(season)
77 | except BaseException as err:
78 | return []
79 | sleep(0.2)
80 | return seasons
81 |
82 |
83 | def get_episodes(client=None, path_map=[], season=None):
84 | if client is None or season is None:
85 | return []
86 |
87 | episodes = []
88 |
89 | try:
90 | result = client.jellyfin.shows("/%s/Episodes" % season['SeriesId'], {
91 | 'UserId': "{UserId}",
92 | 'SeasonId': season['SeasonId'],
93 | 'Fields': (
94 | "Path"
95 | )
96 | })
97 |
98 | if 'Items' in result and result['Items']:
99 | for item in result['Items']:
100 | episode = {}
101 | episode['Name'] = item['Name']
102 | episode['SeriesName'] = season['SeriesName']
103 | episode['SeasonName'] = season['Name']
104 | episode['Duration'] = int(item['RunTimeTicks']) / 10000
105 | episode['SeriesId'] = season['SeriesId']
106 | episode['SeasonId'] = season['SeasonId']
107 | episode['EpisodeId'] = item['Id']
108 | episode['ProviderIds'] = {}
109 | if 'ProviderIds' in item:
110 | episode['ProviderIds'] = item['ProviderIds'].deepcopy()
111 | if 'Path' in item:
112 | episode['Path'] = map_path(item['Path'], path_map)
113 | episodes.append(episode)
114 | except BaseException as err:
115 | return []
116 | return episodes
117 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | arrow==1.2.2
2 | certifi==2023.7.22
3 | charset-normalizer==2.0.12
4 | idna==3.3
5 | ImageHash==4.2.1
6 | jellyfin-apiclient-python==1.8.1
7 | numpy==1.22.0
8 | opencv-python==4.5.5.64
9 | pandas==1.4.1
10 | Pillow==10.0.1
11 | python-dateutil==2.8.2
12 | pytz==2022.1
13 | PyWavelets==1.3.0
14 | requests==2.31.0
15 | scipy==1.10.0
16 | six==1.16.0
17 | urllib3==1.26.18
18 | websocket-client==1.3.1
19 |
--------------------------------------------------------------------------------
/tox.ini:
--------------------------------------------------------------------------------
1 | [flake8]
2 | ignore = E501,E116,W293,W605,F841
3 | max-complexity = 30
4 | builtins = _
--------------------------------------------------------------------------------
/unused/ytube_scrape.py:
--------------------------------------------------------------------------------
1 | import os
2 | import sys
3 | import getopt
4 | from pathlib import Path
5 | from datetime import datetime
6 |
7 | config_path = os.environ['CONFIG_DIR'] if 'CONFIG_DIR' in os.environ else './config'
8 | data_path = os.environ['DATA_DIR'] if 'DATA_DIR' in os.environ else os.path.join(config_path, 'data')
9 |
10 | session_timestamp = datetime.now().strftime("%Y_%m_%d-%I_%M_%S_%p")
11 |
12 |
13 | def print_debug(a=[], log=True, log_file=False):
14 | # Here a is the array holding the objects
15 | # passed as the argument of the function
16 | output = ' '.join([str(elem) for elem in a])
17 | if log:
18 | print(output, file=sys.stderr)
19 | if log_file:
20 | log_path = os.path.join(config_path, 'logs')
21 | Path(log_path).mkdir(parents=True, exist_ok=True)
22 | with open(os.path.join(log_path, 'log_%s.txt' % session_timestamp), "a") as logger:
23 | logger.write(output + '\n')
24 |
25 |
26 | def get_video(name='', log_level=0, log_file=False, cleanup=True, log_timestamp=None):
27 | print(name)
28 |
29 |
30 | def main(argv):
31 |
32 | name = ''
33 | log_level = 0
34 | cleanup = False
35 | log = False
36 | try:
37 | opts, args = getopt.getopt(argv, "hi:dvcl")
38 | except getopt.GetoptError:
39 | print_debug(['ytube_scrape.py -i -v (verbose - some logging) -d (debug - most logging) -l (log to file)\n'])
40 | sys.exit(2)
41 |
42 | for opt, arg in opts:
43 | if opt == '-h':
44 | print_debug(['decode.py -i -v (verbose - some logging) -d (debug - most logging) -l (log to file)\n'])
45 | sys.exit()
46 | elif opt == '-i':
47 | name = arg
48 | elif opt == '-l':
49 | log = True
50 | elif opt == '-d':
51 | log_level = 2
52 | elif opt == '-v':
53 | log_level = 1
54 |
55 | result = get_video(name=name, log_level=log_level, log_file=log, cleanup=cleanup)
56 | print(result)
57 |
58 |
59 | if __name__ == "__main__":
60 | main(sys.argv[1:])
61 |
--------------------------------------------------------------------------------