/
def main_page(request):
# Mp3 format options
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
if request.method == 'POST':
form = DownloadForm(
request.POST)
if form.is_valid():
try:
video_url = form.cleaned_data.get('url')
with youtube_dl.YoutubeDL(ydl_opts) as downloaded:
downloaded.download([video_url])
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(
datetime.now().strftime('%Y-%m-%d %H:%M:%S')+' Type:{} Filename:{} Line:{}ERROR: {}'.
format(exc_type, fname, exc_tb.tb_lineno, e))
return render(request, 'main_page.html', {'form': form})
form = DownloadForm()
return render(request, 'main_page.html', {'form': form})