send the actual data
This commit is contained in:
		
							parent
							
								
									0c9053a4d7
								
							
						
					
					
						commit
						7fdcbecf93
					
				
					 3 changed files with 54 additions and 3 deletions
				
			
		| 
						 | 
					@ -6,3 +6,4 @@ class Config:
 | 
				
			||||||
            ''.join(random.choices(string.ascii_letters, k=20))
 | 
					            ''.join(random.choices(string.ascii_letters, k=20))
 | 
				
			||||||
    DATA_DIR   = os.environ.get('DATA_DIR') or '/home/orange/devel/prolefeeder/tmp'
 | 
					    DATA_DIR   = os.environ.get('DATA_DIR') or '/home/orange/devel/prolefeeder/tmp'
 | 
				
			||||||
    MAX_LENGTH = os.environ.get('MAX_LENGTH') or 180
 | 
					    MAX_LENGTH = os.environ.get('MAX_LENGTH') or 180
 | 
				
			||||||
 | 
					    KBITS      = os.environ.get('KBITS') or 128
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,14 +1,14 @@
 | 
				
			||||||
from datetime import datetime, timedelta
 | 
					from datetime import datetime, timedelta
 | 
				
			||||||
import subprocess
 | 
					import subprocess
 | 
				
			||||||
 | 
					
 | 
				
			||||||
KBITS = 128
 | 
					from config import Config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def generate_test_file(fn, delta):
 | 
					def generate_test_file(fn, delta):
 | 
				
			||||||
    subprocess.call(['ffmpeg',
 | 
					    subprocess.call(['ffmpeg',
 | 
				
			||||||
                     '-f', 'lavfi',
 | 
					                     '-f', 'lavfi',
 | 
				
			||||||
                     '-i', 'sine=frequency=1000:duration=%d' % delta.total_seconds(),
 | 
					                     '-i', 'sine=frequency=1000:duration=%d' % delta.total_seconds(),
 | 
				
			||||||
                     '-b:a', '%dk' % KBITS,
 | 
					                     '-b:a', '%dk' % Config.KBITS,
 | 
				
			||||||
                     fn])
 | 
					                     fn])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,6 +6,10 @@ from wtforms import DateTimeField, DecimalField, SubmitField
 | 
				
			||||||
from wtforms.validators import DataRequired, NumberRange
 | 
					from wtforms.validators import DataRequired, NumberRange
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from datetime import datetime, timedelta
 | 
					from datetime import datetime, timedelta
 | 
				
			||||||
 | 
					import os
 | 
				
			||||||
 | 
					import random
 | 
				
			||||||
 | 
					import re
 | 
				
			||||||
 | 
					import subprocess
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from config import Config
 | 
					from config import Config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -47,7 +51,53 @@ def download_file(filename):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def prepare_download(form):
 | 
					def prepare_download(form):
 | 
				
			||||||
    return 'test.mp3'
 | 
					    """Prepare a download given the user's request form"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def start_time_for_source_fn(fn):
 | 
				
			||||||
 | 
					        return datetime.strptime(fn, 'qfhi-%Y%m%d-%H%M.mp3')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def length_for_source_fn(fn):
 | 
				
			||||||
 | 
					        size = os.stat(os.path.join(app.config['DATA_DIR'], fn)).st_size
 | 
				
			||||||
 | 
					        length = timedelta(minutes=size / (1000*app.config['KBITS']/8) / 60)
 | 
				
			||||||
 | 
					        return length
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Get a sorted list of all source files with start time and length
 | 
				
			||||||
 | 
					    sources = []
 | 
				
			||||||
 | 
					    for fn in os.listdir(app.config['DATA_DIR']):
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
 | 
					            start_time = start_time_for_source_fn(fn)
 | 
				
			||||||
 | 
					            length = length_for_source_fn(fn)
 | 
				
			||||||
 | 
					            sources.append({'fn': fn, 'start_time': start_time, 'length': length})
 | 
				
			||||||
 | 
					        except ValueError:
 | 
				
			||||||
 | 
					            pass
 | 
				
			||||||
 | 
					    sources = sorted(sources, key=lambda s: s['start_time'])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Only interested in the source files from the start file
 | 
				
			||||||
 | 
					    start_index = None
 | 
				
			||||||
 | 
					    for i, source in enumerate(sources):
 | 
				
			||||||
 | 
					        if source['start_time'] <= form.start_time.data < source['start_time'] + source['length']:
 | 
				
			||||||
 | 
					            start_index = i
 | 
				
			||||||
 | 
					    sources = sources[start_index:]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Seek into the first file
 | 
				
			||||||
 | 
					    ss = (form.start_time.data - sources[0]['start_time']).total_seconds()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Let ffmpeg do the rest of the job
 | 
				
			||||||
 | 
					    # XXX Necessary to limit the concat files?
 | 
				
			||||||
 | 
					    output_filename = '{}_{}.mp3'.format(form.start_time.data, form.length.data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    c = ['ffmpeg', '-y']
 | 
				
			||||||
 | 
					    c += ['-ss', str(ss)]
 | 
				
			||||||
 | 
					    c += ['-i', 'concat:' + '|'.join([os.path.join(app.config['DATA_DIR'],
 | 
				
			||||||
 | 
					        source['fn']) for source in sources])]
 | 
				
			||||||
 | 
					    c += ['-codec', 'copy']
 | 
				
			||||||
 | 
					    c += ['-t', str(form.length.data * 60)]
 | 
				
			||||||
 | 
					    c += [os.path.join(app.config['DATA_DIR'], output_filename)]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    app.logger.debug(' '.join(c))
 | 
				
			||||||
 | 
					    subprocess.call(c)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return output_filename
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue