Skip to content

Commit 96686c0

Browse files
committed
skip Video and Audio files unable to create their sample file
1 parent 1992e3d commit 96686c0

File tree

2 files changed

+69
-58
lines changed

2 files changed

+69
-58
lines changed

finder/contrib/audio/models.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,26 @@ def get_sample_url(self, realm):
2828
sample_path = f'{self.id}/{self.get_sample_path(sample_start, sample_duration)}'
2929
if not realm.sample_storage.exists(sample_path):
3030
suffix = Path(sample_path).suffix
31-
with realm.original_storage.open(self.file_path) as handle, NamedTemporaryFile(suffix=suffix) as tempfile:
32-
process = (
33-
ffmpeg.input('pipe:0').audio
34-
.filter('atrim', start=sample_start, duration=sample_duration)
35-
.output(tempfile.name)
36-
.run_async(pipe_stdin=True, overwrite_output=True, quiet=True)
37-
)
38-
for chunk in handle.chunks():
39-
try:
40-
process.stdin.write(chunk)
41-
except BrokenPipeError:
42-
break # end of sample reached
43-
process.stdin.close()
44-
process.wait()
45-
tempfile.flush()
46-
realm.sample_storage.save(sample_path, tempfile)
47-
31+
try:
32+
with NamedTemporaryFile(suffix=suffix) as tempfile:
33+
process = (
34+
ffmpeg.input('pipe:0').audio
35+
.filter('atrim', start=sample_start, duration=sample_duration)
36+
.output(tempfile.name)
37+
.run_async(pipe_stdin=True, overwrite_output=True, quiet=True)
38+
)
39+
with realm.original_storage.open(self.file_path) as handle:
40+
for chunk in handle.chunks():
41+
try:
42+
process.stdin.write(chunk)
43+
except BrokenPipeError:
44+
break # end of sample reached
45+
process.stdin.close()
46+
process.wait()
47+
tempfile.flush()
48+
realm.sample_storage.save(sample_path, tempfile)
49+
except Exception:
50+
return
4851
return realm.sample_storage.url(sample_path)
4952

5053
def get_sample_path(self, sample_start, sample_duration):

finder/contrib/video/models.py

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,35 @@ def get_sample_url(self, realm):
3232
sample_path = f'{self.id}/{self.get_sample_path(sample_start)}'
3333
if not realm.sample_storage.exists(sample_path):
3434
suffix = Path(sample_path).suffix
35-
with (realm.original_storage.open(self.file_path) as handle, NamedTemporaryFile(suffix=suffix) as tempfile):
36-
in_stream = ffmpeg.input('pipe:0')
37-
video_stream = (
38-
in_stream.video
39-
.filter('crop', 'min(iw,ih)', 'min(iw,ih)')
40-
.filter('scale', self.thumbnail_size, -1)
41-
)
42-
process = (
43-
ffmpeg.concat(video_stream, in_stream.audio, v=1, a=1)
44-
.output(
45-
tempfile.name,
46-
ss=sample_start,
47-
t=sample_duration,
35+
try:
36+
with NamedTemporaryFile(suffix=suffix) as tempfile:
37+
in_stream = ffmpeg.input('pipe:0')
38+
video_stream = (
39+
in_stream.video
40+
.filter('crop', 'min(iw,ih)', 'min(iw,ih)')
41+
.filter('scale', self.thumbnail_size, -1)
4842
)
49-
.run_async(pipe_stdin=True, overwrite_output=True, quiet=True)
50-
)
51-
for chunk in handle.chunks():
52-
try:
53-
process.stdin.write(chunk)
54-
except BrokenPipeError:
55-
break # sample frame found
56-
process.stdin.close()
57-
process.wait()
58-
tempfile.flush()
59-
realm.sample_storage.save(sample_path, tempfile)
43+
process = (
44+
ffmpeg.concat(video_stream, in_stream.audio, v=1, a=1)
45+
.output(
46+
tempfile.name,
47+
ss=sample_start,
48+
t=sample_duration,
49+
)
50+
.run_async(pipe_stdin=True, overwrite_output=True, quiet=True)
51+
)
52+
with realm.original_storage.open(self.file_path) as handle:
53+
for chunk in handle.chunks():
54+
try:
55+
process.stdin.write(chunk)
56+
except BrokenPipeError:
57+
break # sample frame found
58+
process.stdin.close()
59+
process.wait()
60+
tempfile.flush()
61+
realm.sample_storage.save(sample_path, tempfile)
62+
except Exception:
63+
return
6064
return realm.sample_storage.url(sample_path)
6165

6266
def get_thumbnail_url(self, realm):
@@ -66,23 +70,27 @@ def get_thumbnail_url(self, realm):
6670
suffix = '.jpg'
6771
poster_path = f'{self.id}/{self.get_sample_path(sample_start, suffix=suffix)}'
6872
if not realm.sample_storage.exists(poster_path):
69-
with realm.original_storage.open(self.file_path) as handle, NamedTemporaryFile(suffix=suffix) as tempfile:
70-
process = (
71-
ffmpeg.input('pipe:0').video
72-
.filter('crop', 'min(iw,ih)', 'min(iw,ih)')
73-
.filter('scale', self.thumbnail_size, -1)
74-
.output(tempfile.name, ss=sample_start, vframes=1)
75-
.run_async(pipe_stdin=True, overwrite_output=True, quiet=False)
76-
)
77-
for chunk in handle.chunks():
78-
try:
79-
process.stdin.write(chunk)
80-
except BrokenPipeError:
81-
break # sample frame found
82-
process.stdin.close()
83-
process.wait()
84-
tempfile.flush()
85-
realm.sample_storage.save(poster_path, tempfile)
73+
try:
74+
with NamedTemporaryFile(suffix=suffix) as tempfile:
75+
process = (
76+
ffmpeg.input('pipe:0').video
77+
.filter('crop', 'min(iw,ih)', 'min(iw,ih)')
78+
.filter('scale', self.thumbnail_size, -1)
79+
.output(tempfile.name, ss=sample_start, vframes=1)
80+
.run_async(pipe_stdin=True, overwrite_output=True, quiet=True)
81+
)
82+
with realm.original_storage.open(self.file_path) as handle:
83+
for chunk in handle.chunks():
84+
try:
85+
process.stdin.write(chunk)
86+
except BrokenPipeError:
87+
break # sample frame found
88+
process.stdin.close()
89+
process.wait()
90+
tempfile.flush()
91+
realm.sample_storage.save(poster_path, tempfile)
92+
except Exception:
93+
return self.fallback_thumbnail_url
8694
return realm.sample_storage.url(poster_path)
8795

8896
def get_sample_path(self, sample_start, suffix=None):

0 commit comments

Comments
 (0)