Commit | Line | Data |
---|---|---|
5a34a80d JW |
1 | # GNU MediaGoblin -- federated, autonomous media hosting |
2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. | |
3 | # | |
4 | # This program is free software: you can redistribute it and/or modify | |
5 | # it under the terms of the GNU Affero General Public License as published by | |
6 | # the Free Software Foundation, either version 3 of the License, or | |
7 | # (at your option) any later version. | |
8 | # | |
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU Affero General Public License for more details. | |
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | import logging | |
25e39842 | 18 | from tempfile import NamedTemporaryFile |
5a34a80d JW |
19 | import os |
20 | ||
21 | from mediagoblin import mg_globals as mgg | |
b781c3c9 | 22 | from mediagoblin.processing import (create_pub_filepath, BadMediaFail, |
64712915 | 23 | FilenameBuilder, ProgressCallback) |
5a34a80d | 24 | |
b781c3c9 JK |
25 | from mediagoblin.media_types.audio.transcoders import (AudioTranscoder, |
26 | AudioThumbnailer) | |
5a34a80d | 27 | |
10085b77 | 28 | _log = logging.getLogger(__name__) |
5a34a80d | 29 | |
64712915 | 30 | |
ec4261a4 | 31 | def sniff_handler(media_file, **kw): |
196a5181 | 32 | try: |
4f4f2531 | 33 | transcoder = AudioTranscoder() |
ec4261a4 | 34 | data = transcoder.discover(media_file.name) |
4f4f2531 JW |
35 | except BadMediaFail: |
36 | _log.debug('Audio discovery raised BadMediaFail') | |
37 | return False | |
ec4261a4 | 38 | |
4f4f2531 JW |
39 | if data.is_audio == True and data.is_video == False: |
40 | return True | |
10085b77 JW |
41 | |
42 | return False | |
5a34a80d | 43 | |
64712915 | 44 | |
5a34a80d JW |
45 | def process_audio(entry): |
46 | audio_config = mgg.global_config['media_type:mediagoblin.media_types.audio'] | |
47 | ||
48 | workbench = mgg.workbench_manager.create_workbench() | |
49 | ||
50 | queued_filepath = entry.queued_media_file | |
51 | queued_filename = workbench.localized_file( | |
52 | mgg.queue_store, queued_filepath, | |
53 | 'source') | |
b781c3c9 | 54 | name_builder = FilenameBuilder(queued_filename) |
5a34a80d | 55 | |
b781c3c9 | 56 | webm_audio_filepath = create_pub_filepath( |
5a34a80d JW |
57 | entry, |
58 | '{original}.webm'.format( | |
59 | original=os.path.splitext( | |
60 | queued_filepath[-1])[0])) | |
61 | ||
b781c3c9 JK |
62 | if audio_config['keep_original']: |
63 | with open(queued_filename, 'rb') as queued_file: | |
64 | original_filepath = create_pub_filepath( | |
65 | entry, name_builder.fill('{basename}{ext}')) | |
66 | ||
67 | with mgg.public_store.get_file(original_filepath, 'wb') as \ | |
68 | original_file: | |
69 | _log.debug('Saving original...') | |
70 | original_file.write(queued_file.read()) | |
71 | ||
72 | entry.media_files['original'] = original_filepath | |
73 | ||
10085b77 | 74 | transcoder = AudioTranscoder() |
5a34a80d | 75 | |
25e39842 | 76 | with NamedTemporaryFile(dir=workbench.dir) as webm_audio_tmp: |
64712915 | 77 | progress_callback = ProgressCallback(entry) |
5a34a80d JW |
78 | |
79 | transcoder.transcode( | |
80 | queued_filename, | |
b781c3c9 | 81 | webm_audio_tmp.name, |
64712915 JW |
82 | quality=audio_config['quality'], |
83 | progress_callback=progress_callback) | |
5a34a80d | 84 | |
a855e92a | 85 | transcoder.discover(webm_audio_tmp.name) |
5a34a80d JW |
86 | |
87 | _log.debug('Saving medium...') | |
b781c3c9 JK |
88 | mgg.public_store.get_file(webm_audio_filepath, 'wb').write( |
89 | webm_audio_tmp.read()) | |
5a34a80d | 90 | |
b781c3c9 | 91 | entry.media_files['webm_audio'] = webm_audio_filepath |
5a34a80d | 92 | |
c7cf6235 | 93 | # entry.media_data_init(length=int(data.audiolength)) |
5a34a80d | 94 | |
10085b77 JW |
95 | if audio_config['create_spectrogram']: |
96 | spectrogram_filepath = create_pub_filepath( | |
97 | entry, | |
98 | '{original}-spectrogram.jpg'.format( | |
99 | original=os.path.splitext( | |
100 | queued_filepath[-1])[0])) | |
101 | ||
25e39842 | 102 | with NamedTemporaryFile(dir=workbench.dir, suffix='.ogg') as wav_tmp: |
549000d9 | 103 | _log.info('Creating OGG source for spectrogram') |
10085b77 JW |
104 | transcoder.transcode( |
105 | queued_filename, | |
106 | wav_tmp.name, | |
549000d9 JW |
107 | mux_string='vorbisenc quality={0} ! oggmux'.format( |
108 | audio_config['quality'])) | |
10085b77 JW |
109 | |
110 | thumbnailer = AudioThumbnailer() | |
111 | ||
25e39842 | 112 | with NamedTemporaryFile(dir=workbench.dir, suffix='.jpg') as spectrogram_tmp: |
10085b77 JW |
113 | thumbnailer.spectrogram( |
114 | wav_tmp.name, | |
115 | spectrogram_tmp.name, | |
196a5181 JW |
116 | width=mgg.global_config['media:medium']['max_width'], |
117 | fft_size=audio_config['spectrogram_fft_size']) | |
10085b77 JW |
118 | |
119 | _log.debug('Saving spectrogram...') | |
120 | mgg.public_store.get_file(spectrogram_filepath, 'wb').write( | |
121 | spectrogram_tmp.read()) | |
122 | ||
123 | entry.media_files['spectrogram'] = spectrogram_filepath | |
124 | ||
25e39842 | 125 | with NamedTemporaryFile(dir=workbench.dir, suffix='.jpg') as thumb_tmp: |
10085b77 JW |
126 | thumbnailer.thumbnail_spectrogram( |
127 | spectrogram_tmp.name, | |
128 | thumb_tmp.name, | |
129 | (mgg.global_config['media:thumb']['max_width'], | |
130 | mgg.global_config['media:thumb']['max_height'])) | |
131 | ||
132 | thumb_filepath = create_pub_filepath( | |
133 | entry, | |
134 | '{original}-thumbnail.jpg'.format( | |
135 | original=os.path.splitext( | |
136 | queued_filepath[-1])[0])) | |
137 | ||
138 | mgg.public_store.get_file(thumb_filepath, 'wb').write( | |
139 | thumb_tmp.read()) | |
140 | ||
141 | entry.media_files['thumb'] = thumb_filepath | |
142 | else: | |
5a34a80d | 143 | entry.media_files['thumb'] = ['fake', 'thumb', 'path.jpg'] |
196a5181 | 144 | |
5a34a80d JW |
145 | mgg.queue_store.delete_file(queued_filepath) |
146 | ||
e2007352 JW |
147 | # clean up workbench |
148 | workbench.destroy_self() |