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 | |
df68438a RE |
30 | MEDIA_TYPE = 'mediagoblin.media_types.audio' |
31 | ||
64712915 | 32 | |
ec4261a4 | 33 | def sniff_handler(media_file, **kw): |
df68438a | 34 | _log.info('Sniffing {0}'.format(MEDIA_TYPE)) |
196a5181 | 35 | try: |
4f4f2531 | 36 | transcoder = AudioTranscoder() |
ec4261a4 | 37 | data = transcoder.discover(media_file.name) |
4f4f2531 JW |
38 | except BadMediaFail: |
39 | _log.debug('Audio discovery raised BadMediaFail') | |
df68438a | 40 | return None |
ec4261a4 | 41 | |
4f4f2531 | 42 | if data.is_audio == True and data.is_video == False: |
df68438a | 43 | return MEDIA_TYPE |
10085b77 | 44 | |
df68438a | 45 | return None |
5a34a80d | 46 | |
64712915 | 47 | |
fb46fa66 | 48 | def process_audio(proc_state): |
45ab3e07 | 49 | """Code to process uploaded audio. Will be run by celery. |
5a34a80d | 50 | |
45ab3e07 SS |
51 | A Workbench() represents a local tempory dir. It is automatically |
52 | cleaned up when this function exits. | |
53 | """ | |
fb46fa66 E |
54 | entry = proc_state.entry |
55 | workbench = proc_state.workbench | |
45ab3e07 | 56 | audio_config = mgg.global_config['media_type:mediagoblin.media_types.audio'] |
5a34a80d JW |
57 | |
58 | queued_filepath = entry.queued_media_file | |
59 | queued_filename = workbench.localized_file( | |
60 | mgg.queue_store, queued_filepath, | |
61 | 'source') | |
b781c3c9 | 62 | name_builder = FilenameBuilder(queued_filename) |
5a34a80d | 63 | |
b781c3c9 | 64 | webm_audio_filepath = create_pub_filepath( |
5a34a80d JW |
65 | entry, |
66 | '{original}.webm'.format( | |
67 | original=os.path.splitext( | |
68 | queued_filepath[-1])[0])) | |
69 | ||
b781c3c9 JK |
70 | if audio_config['keep_original']: |
71 | with open(queued_filename, 'rb') as queued_file: | |
72 | original_filepath = create_pub_filepath( | |
73 | entry, name_builder.fill('{basename}{ext}')) | |
74 | ||
75 | with mgg.public_store.get_file(original_filepath, 'wb') as \ | |
76 | original_file: | |
77 | _log.debug('Saving original...') | |
78 | original_file.write(queued_file.read()) | |
79 | ||
80 | entry.media_files['original'] = original_filepath | |
81 | ||
10085b77 | 82 | transcoder = AudioTranscoder() |
5a34a80d | 83 | |
25e39842 | 84 | with NamedTemporaryFile(dir=workbench.dir) as webm_audio_tmp: |
64712915 | 85 | progress_callback = ProgressCallback(entry) |
5a34a80d JW |
86 | |
87 | transcoder.transcode( | |
88 | queued_filename, | |
b781c3c9 | 89 | webm_audio_tmp.name, |
64712915 JW |
90 | quality=audio_config['quality'], |
91 | progress_callback=progress_callback) | |
5a34a80d | 92 | |
a855e92a | 93 | transcoder.discover(webm_audio_tmp.name) |
5a34a80d JW |
94 | |
95 | _log.debug('Saving medium...') | |
b781c3c9 JK |
96 | mgg.public_store.get_file(webm_audio_filepath, 'wb').write( |
97 | webm_audio_tmp.read()) | |
5a34a80d | 98 | |
b781c3c9 | 99 | entry.media_files['webm_audio'] = webm_audio_filepath |
5a34a80d | 100 | |
c7cf6235 | 101 | # entry.media_data_init(length=int(data.audiolength)) |
5a34a80d | 102 | |
10085b77 JW |
103 | if audio_config['create_spectrogram']: |
104 | spectrogram_filepath = create_pub_filepath( | |
105 | entry, | |
106 | '{original}-spectrogram.jpg'.format( | |
107 | original=os.path.splitext( | |
108 | queued_filepath[-1])[0])) | |
109 | ||
25e39842 | 110 | with NamedTemporaryFile(dir=workbench.dir, suffix='.ogg') as wav_tmp: |
549000d9 | 111 | _log.info('Creating OGG source for spectrogram') |
10085b77 JW |
112 | transcoder.transcode( |
113 | queued_filename, | |
114 | wav_tmp.name, | |
549000d9 JW |
115 | mux_string='vorbisenc quality={0} ! oggmux'.format( |
116 | audio_config['quality'])) | |
10085b77 JW |
117 | |
118 | thumbnailer = AudioThumbnailer() | |
119 | ||
25e39842 | 120 | with NamedTemporaryFile(dir=workbench.dir, suffix='.jpg') as spectrogram_tmp: |
10085b77 JW |
121 | thumbnailer.spectrogram( |
122 | wav_tmp.name, | |
123 | spectrogram_tmp.name, | |
196a5181 JW |
124 | width=mgg.global_config['media:medium']['max_width'], |
125 | fft_size=audio_config['spectrogram_fft_size']) | |
10085b77 JW |
126 | |
127 | _log.debug('Saving spectrogram...') | |
128 | mgg.public_store.get_file(spectrogram_filepath, 'wb').write( | |
129 | spectrogram_tmp.read()) | |
130 | ||
131 | entry.media_files['spectrogram'] = spectrogram_filepath | |
132 | ||
25e39842 | 133 | with NamedTemporaryFile(dir=workbench.dir, suffix='.jpg') as thumb_tmp: |
10085b77 JW |
134 | thumbnailer.thumbnail_spectrogram( |
135 | spectrogram_tmp.name, | |
136 | thumb_tmp.name, | |
137 | (mgg.global_config['media:thumb']['max_width'], | |
138 | mgg.global_config['media:thumb']['max_height'])) | |
139 | ||
140 | thumb_filepath = create_pub_filepath( | |
141 | entry, | |
142 | '{original}-thumbnail.jpg'.format( | |
143 | original=os.path.splitext( | |
144 | queued_filepath[-1])[0])) | |
145 | ||
146 | mgg.public_store.get_file(thumb_filepath, 'wb').write( | |
147 | thumb_tmp.read()) | |
148 | ||
149 | entry.media_files['thumb'] = thumb_filepath | |
150 | else: | |
5a34a80d | 151 | entry.media_files['thumb'] = ['fake', 'thumb', 'path.jpg'] |
196a5181 | 152 | |
36ae6bcb SS |
153 | # Remove queued media file from storage and database. |
154 | # queued_filepath is in the task_id directory which should | |
155 | # be removed too, but fail if the directory is not empty to be on | |
156 | # the super-safe side. | |
157 | mgg.queue_store.delete_file(queued_filepath) # rm file | |
158 | mgg.queue_store.delete_dir(queued_filepath[:-1]) # rm dir | |
159 | entry.queued_media_file = [] |