mediasnacks 0.20.0 → 0.20.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mediasnacks",
3
- "version": "0.20.0",
3
+ "version": "0.20.1",
4
4
  "description": "Utilities for optimizing and preparing videos and images",
5
5
  "license": "MIT",
6
6
  "author": "Eric Fortis",
package/src/detectdups.js CHANGED
@@ -3,6 +3,7 @@
3
3
  import { parseOptions } from './utils/parseOptions.js'
4
4
  import { ffmpeg, assertUserHasFFmpeg, videoAttrs } from './utils/ffmpeg.js'
5
5
 
6
+ const STDEV_THRESHOLD = 0.2
6
7
 
7
8
  const MAN = `
8
9
  Usage: mediasnacks detectdups [options] <video>
@@ -73,7 +74,7 @@ async function main() {
73
74
  analyze(dups, seek, duration)
74
75
  }
75
76
 
76
- async function detectDuplicateFramesNums(video, seek, duration) {
77
+ export async function detectDuplicateFramesNums(video, seek, duration) {
77
78
  const { stderr } = await ffmpeg([
78
79
  '-v', 'info',
79
80
  '-stats',
@@ -81,7 +82,6 @@ async function detectDuplicateFramesNums(video, seek, duration) {
81
82
  '-t', duration,
82
83
  '-i', video,
83
84
  '-vf', [
84
- 'scale=320:-1',
85
85
  'tblend=all_mode=difference',
86
86
  'format=gray',
87
87
  'showinfo',
@@ -89,16 +89,23 @@ async function detectDuplicateFramesNums(video, seek, duration) {
89
89
  '-f', 'null', '-',
90
90
  ])
91
91
 
92
- const reBlackFramesNum = /n:\s*(\d+).*?mean:\[0]/
92
+ const reNearBlackFrames = /n:\s*(\d+).*?mean:\[0].*?stdev:\[([0-9.]+)]/
93
93
  const dupFrames = []
94
94
  for (const line of stderr.split('\n')) {
95
- const match = line.match(reBlackFramesNum)
96
- if (match)
97
- dupFrames.push(Number(match[1]))
95
+ const match = line.match(reNearBlackFrames)
96
+ if (match) {
97
+ const stdev = parseFloat(match[2])
98
+ if (stdev <= STDEV_THRESHOLD) {
99
+ const frameNum = parseInt(match[1], 10)
100
+ dupFrames.push(frameNum)
101
+ }
102
+ }
98
103
  }
99
104
  return dupFrames
100
105
  }
101
106
 
107
+ // This is only good for when there's one repeated frame in a cycle.
108
+ // i.e. it's the wrong approach for e.g. 25 to 60, in which N=2 and N=3
102
109
  function analyze(dup_frames, seek, duration) {
103
110
  const histogram = {}
104
111
  for (let i = 1; i < dup_frames.length; i++) {
@@ -110,11 +117,28 @@ function analyze(dup_frames, seek, duration) {
110
117
  start_sec: seek,
111
118
  end_sec: seek + duration
112
119
  },
113
- histogram
120
+ histogram,
121
+ n: maxFreqKey(histogram)
114
122
  }, null, 2))
115
123
  }
116
124
 
117
- main().catch(err => {
118
- console.error(err.message || err)
119
- process.exit(1)
120
- })
125
+ function maxFreqKey(hist) {
126
+ let maxKey = null
127
+ let maxVal = -1
128
+ for (const [key, val] of Object.entries(hist))
129
+ if (val > maxVal) {
130
+ maxVal = val
131
+ maxKey = key
132
+ }
133
+ return maxKey !== null
134
+ ? Number(maxKey)
135
+ : null
136
+ }
137
+
138
+
139
+
140
+ if (import.meta.main)
141
+ main().catch(err => {
142
+ console.error(err.message || err)
143
+ process.exit(1)
144
+ })
@@ -0,0 +1,36 @@
1
+ import { test } from 'node:test'
2
+ import { join } from 'node:path'
3
+ import { equal } from 'node:assert/strict'
4
+ import { cli } from './utils/test-utils.js'
5
+
6
+ const rel = f => join(import.meta.dirname, f)
7
+
8
+ function detect(video) {
9
+ const { stdout } = cli('detectdups', rel(video), 0, 7)
10
+ return JSON.parse(stdout).n
11
+ }
12
+
13
+
14
+ test('no dups', () =>
15
+ equal(detect('fixtures/big-buck-bunny/bbb_24fps_no_dups.mp4'), null))
16
+
17
+
18
+ // These fixtures are badly retimed (non-interpolated, just duplicating a frame)
19
+
20
+ test('24 to 48 (has dup at n=2)', () =>
21
+ equal(detect('fixtures/big-buck-bunny/bbb_24_to_48fps_dup.mp4'), 2))
22
+
23
+ test('25 to 50 (has dup at n=2)', () =>
24
+ equal(detect('fixtures/big-buck-bunny/bbb_25_to_50fps_dup.mp4'), 2))
25
+
26
+
27
+ test('24 to 30 (has dup at n=5)', () =>
28
+ equal(detect('fixtures/big-buck-bunny/bbb_24_to_30fps_dup.mp4'), 5))
29
+
30
+ test('25 to 30 (has dup at n=6)', () =>
31
+ equal(detect('fixtures/big-buck-bunny/bbb_25_to_30fps_dup.mp4'), 6))
32
+
33
+
34
+ test('24 to 25 (has dup at n=25)', () =>
35
+ equal(detect('fixtures/big-buck-bunny/bbb_24_to_25fps_dup.mp4'), 25))
36
+
@@ -0,0 +1,71 @@
1
+ # Generating Fixtures
2
+
3
+
4
+ ## 1. Download a video (Big Buck Bunny)
5
+
6
+ ```sh
7
+ curl https://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_1080p_h264.mov -o bbb_full_24fps.mov
8
+ ```
9
+
10
+ ## 2. Extract a scene
11
+ Using the third scene because it's complex enough to cover many edge cases.
12
+ It has a bird flapping its wings with motion blur. Also, animated text titles,
13
+ and fairly static frames after the title ends.
14
+
15
+ The scene is 1080p, 24fps, 7.28sec, h.264.
16
+ ```sh
17
+ brew tap ericfortis/fcpscene
18
+ brew install fcpscene
19
+ fcpscene -m files bbb_full_24fps.mov
20
+
21
+ cp bbb/bbb_full_24fps_003.mov ./bbb_24fps_no_dups.mov
22
+ rm -rf bbb
23
+ rm bbb_full_24fps.mov
24
+ ```
25
+
26
+ ## 3. Re-encode the scene 24fps
27
+ This way all videos will share the same encoding, and no audio.
28
+
29
+ ```sh
30
+ ffmpeg -i bbb_24fps_no_dups.mov \
31
+ -c:v libx264 -crf 18 -preset slow \
32
+ -an \
33
+ bbb_24fps_no_dups.mp4
34
+ ```
35
+
36
+ ## 4. Retime (no dups) speed stretch to 25fps
37
+ For a good (no dups) 25fps, retiming by speeding it up.
38
+ ```sh
39
+ ffmpeg -i bbb_24fps_no_dups.mp4 \
40
+ -vf "setpts=24/25*PTS" \
41
+ -r 25 \
42
+ bbb_25fps_no_dups.mp4
43
+ ```
44
+
45
+
46
+ ## 5. Retime by inserting duplicates (no interpolation)
47
+ ```sh
48
+ for TARGET_FPS in 48 30 25; do
49
+ ffmpeg -i bbb_24fps_no_dups.mp4 \
50
+ -vf fps=$TARGET_FPS \
51
+ -c:v libx264 -crf 18 -preset slow \
52
+ -an \
53
+ "bbb_24_to_${TARGET_FPS}fps_dup.mp4"
54
+ done
55
+
56
+ for TARGET_FPS in 60 50 30; do
57
+ ffmpeg -i bbb_25fps_no_dups.mp4 \
58
+ -vf fps=$TARGET_FPS \
59
+ -c:v libx264 -crf 18 -preset slow \
60
+ -an \
61
+ "bbb_25_to_${TARGET_FPS}fps_dup.mp4"
62
+ done
63
+ ```
64
+ Counting the cycle from 1 (not from 0):
65
+
66
+ - 24 to 48 (inserts dup at n=2) 0101
67
+ - 25 to 50 (inserts dup at n=2) 0101
68
+ - 24 to 30 (inserts dup at n=5) 0000100001
69
+ - 25 to 30 (inserts dup at n=6) 000001000001
70
+ - 24 to 25 (inserts dup at n=25) (0*24)1
71
+ - 25 to 60 (inserts dup at n=2 and n=3) 01011
@@ -1,8 +1,7 @@
1
1
  import { join } from 'node:path'
2
2
  import { tmpdir } from 'node:os'
3
3
  import { spawnSync } from 'node:child_process'
4
- import { createHash } from 'node:crypto'
5
- import { mkdtempSync, readFileSync, mkdirSync, writeFileSync } from 'node:fs'
4
+ import { mkdtempSync, mkdirSync, writeFileSync } from 'node:fs'
6
5
 
7
6
  const rel = f => join(import.meta.dirname, f)
8
7
 
@@ -11,13 +10,7 @@ export function mkTempDir(prefix = 'test-') {
11
10
  }
12
11
 
13
12
  export function cli(...args) {
14
- spawnSync(rel('../cli.js'), args)
15
- }
16
-
17
- export function sha1(filePath) {
18
- return createHash('sha1')
19
- .update(readFileSync(filePath))
20
- .digest('base64')
13
+ return spawnSync(rel('../cli.js'), args)
21
14
  }
22
15
 
23
16
  export function dir(...args) {