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