网站后台管理界面代码,世界500强中国企业,做公众号还是网站,校园网站设计方案2024最新YT-DLP使用demo网页端渲染 前提摘要1.使用python的fastapi库和jinjia2库进行前端渲染2.代码实现1#xff09;目录结构2#xff09;代码style.cssindex.htmlresult.htmlmain.pyrun.py 3#xff09;运行测试命令端运行 3.项目下载地址 前提摘要
2024最新python使用yt… 2024最新YT-DLP使用demo网页端渲染 前提摘要1.使用python的fastapi库和jinjia2库进行前端渲染2.代码实现1目录结构2代码style.cssindex.htmlresult.htmlmain.pyrun.py 3运行测试命令端运行 3.项目下载地址 前提摘要
2024最新python使用yt-dlp
1.使用python的fastapi库和jinjia2库进行前端渲染 需要下载下面对应的python第三方库 pip install fastapi uvicorn python-multipart jinja2 yt-dlp 功能如下
使用YT-DLP缓存文件到本地预览文件信息2.代码实现
1目录结构 2代码
style.css
body {font-family: Arial, sans-serif;margin: 0;padding: 20px;background-color: #f0f0f0;
}.container {max-width: 800px;margin: 0 auto;background-color: white;padding: 20px;border-radius: 8px;box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}/* 添加 logo 相关样式 */
.logo {display: flex;justify-content: center;gap: 8px;margin-bottom: 20px;
}.youtube-icon, .download-icon {width: 32px; /* 调整图标大小 */height: 32px; /* 调整图标大小 */
}.header {text-align: center;margin-bottom: 30px;
}h1 {font-size: 24px;margin: 10px 0;
}.subtitle {color: #666;margin-bottom: 20px;
}form {margin: 20px 0;
}.input-group {display: flex;gap: 10px;
}input[typetext] {flex: 1;padding: 10px;border: 1px solid #ddd;border-radius: 4px;
}button {background-color: #007bff;color: white;padding: 10px 20px;border: none;border-radius: 4px;cursor: pointer;
}button:hover {background-color: #0056b3;
}.disclaimer {text-align: center;color: #666;font-size: 14px;margin: 20px 0;
}.info-icon {color: #007bff;cursor: help;
}.footer {text-align: center;margin-top: 30px;color: #666;font-size: 14px;
}index.html
!DOCTYPE html
html langzh
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleYouTube 视频下载器/titlelink relstylesheet href{{ url_for(static, pathstyle.css) }}
/head
bodydiv classcontainerdiv classheaderdiv classlogosvg classyoutube-icon viewBox0 0 24 24path fill#FF0000 dM23.498 6.186a3.016 3.016 0 0 0-2.122-2.136C19.505 3.545 12 3.545 12 3.545s-7.505 0-9.377.505A3.017 3.017 0 0 0 .502 6.186C0 8.07 0 12 0 12s0 3.93.502 5.814a3.016 3.016 0 0 0 2.122 2.136c1.871.505 9.376.505 9.376.505s7.505 0 9.377-.505a3.015 3.015 0 0 0 2.122-2.136C24 15.93 24 12 24 12s0-3.93-.502-5.814zM9.545 15.568V8.432L15.818 12l-6.273 3.568z//svgsvg classdownload-icon viewBox0 0 24 24path fill#4285f4 dM19 9h-4V3H9v6H5l7 7 7-7zM5 18v2h14v-2H5z//svg/divh1YT-DLP-DEMO/h1p classsubtitleYTDownload/p/divform action/download methodPOSTdiv classinput-groupinput typetext nameurl placeholderhttps://www.youtube.com/watch?v... requiredbutton typesubmit下载/button/div/formdiv classfooterp由 FastAPI 和 yt-dlp 提供技术支持/p/div/div
/body
/html result.html
!DOCTYPE html
html
headtitleDownload Result/titlelink relstylesheet href{{ url_for(static, pathstyle.css) }}
/head
bodydiv classcontainer{% if success %}div classresult-infodiv classvideo-headerdiv classvideo-detailsh2{{ video_info.title }}/h2div classmeta-infopstrong作者:/strong {{ video_info.author }}/ppstrong时长:/strong {{ video_info.length }}/ppstrong观看次数:/strong {{ {:,}.format(video_info.views) }}/p/div/div/divdiv classdownload-sectionsdiv classpreview-sectionh3视频预览/h3div classpreview-containervideo controlssource src{{ video_path }} typevideo/mp4您的浏览器不支持视频标签。/video/diva href{{ video_path }} classdownload-button downloadspan classicon⭳/span 下载视频/a/divdiv classpreview-sectionh3音频预览/h3div classpreview-containeraudio controlssource src{{ audio_path }} typeaudio/mp4您的浏览器不支持音频标签。/audio/diva href{{ audio_path }} classdownload-button downloadspan classicon⭳/span 下载音频/a/div/div/div{% else %}div classerror-containerh2下载失败/h2p classerror-message{{ error }}/p/div{% endif %}a href/ classback-button返回首页/a/div
/body
/html main.py
from fastapi import FastAPI, Request, Form
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
import yt_dlp
from pathlib import Path
import osapp FastAPI()# 配置静态文件和模板
app.mount(/static, StaticFiles(directorystatic), namestatic)
templates Jinja2Templates(directorytemplates)# 配置下载目录
VIDEO_DIR Path(downloads/video)
AUDIO_DIR Path(downloads/audio)
VIDEO_DIR.mkdir(parentsTrue, exist_okTrue)
AUDIO_DIR.mkdir(parentsTrue, exist_okTrue)def download_youtube_video(url):try:# yt-dlp 基础配置common_opts {cookiefile: rwww.youtube.com_cookies.txt,quiet: False,no_warnings: False,verbose: True,proxy: http://127.0.0.1:10809,socket_timeout: 30,retries: 3,nocheckcertificate: True,prefer_insecure: True}# 视频下载选项video_opts {**common_opts,format: best[extmp4][height720]/best[height720]/best,outtmpl: str(VIDEO_DIR / %(title)s.%(ext)s),}# 音频下载选项audio_opts {**common_opts,format: bestaudio[extm4a]/bestaudio,outtmpl: str(AUDIO_DIR / %(title)s.%(ext)s),}# 获取视频信息with yt_dlp.YoutubeDL(common_opts) as ydl:info ydl.extract_info(url, downloadFalse)title info[title]duration info[duration]thumbnail info[thumbnail]author info.get(uploader, Unknown)views info.get(view_count, 0)# 下载视频with yt_dlp.YoutubeDL(video_opts) as ydl:ydl.download([url])# 下载音频with yt_dlp.YoutubeDL(audio_opts) as ydl:ydl.download([url])# 获取下载后的文件路径video_file next(VIDEO_DIR.glob(f{title}.*))audio_file next(AUDIO_DIR.glob(f{title}.*))return {status: success,title: title,author: author,duration: f{duration // 60}:{duration % 60:02d},views: views,thumbnail: thumbnail,video_path: str(video_file.name),audio_path: str(audio_file.name)}except Exception as e:return {status: error, message: str(e)}app.get(/)
async def home(request: Request):return templates.TemplateResponse(index.html, {request: request})app.post(/download)
async def download_video_route(request: Request, url: str Form(...)):try:result download_youtube_video(url)if result[status] success:video_info {title: result[title],author: result[author],length: result[duration],views: result[views],thumbnail: result[thumbnail]}return templates.TemplateResponse(result.html, {request: request,video_info: video_info,video_path: f/downloads/video/{result[video_path]},audio_path: f/downloads/audio/{result[audio_path]},success: True})else:raise Exception(result[message])except Exception as e:return templates.TemplateResponse(result.html, {request: request,error: str(e),success: False})# 配置下载目录的静态文件服务
app.mount(/downloads, StaticFiles(directorydownloads), namedownloads) run.py
import uvicorn
import osdef check_directories():确保必要的目录存在directories [static,templates,downloads,downloads/video,downloads/audio]for directory in directories:if not os.path.exists(directory):os.makedirs(directory)print(fCreated directory: {directory})if __name__ __main__:# 检查并创建必要的目录check_directories()# 配置并启动服务器uvicorn.run(main:app, host127.0.0.1, port8000, reloadTrue,reload_dirs[templates, static],log_levelinfo) 3运行测试
命令端运行 pip install fastapi uvicorn python-multipart jinja2 yt-dlp python run.py 3.项目下载地址
https://github.com/unkownc/python_demo/tree/main 文章转载自: http://www.morning.zzbwjy.cn.gov.cn.zzbwjy.cn http://www.morning.wlstn.cn.gov.cn.wlstn.cn http://www.morning.dwgcx.cn.gov.cn.dwgcx.cn http://www.morning.nmtyx.cn.gov.cn.nmtyx.cn http://www.morning.wdprz.cn.gov.cn.wdprz.cn http://www.morning.qtwd.cn.gov.cn.qtwd.cn http://www.morning.fqtzn.cn.gov.cn.fqtzn.cn http://www.morning.hyjpl.cn.gov.cn.hyjpl.cn http://www.morning.pkdng.cn.gov.cn.pkdng.cn http://www.morning.nqlkb.cn.gov.cn.nqlkb.cn http://www.morning.kcfnp.cn.gov.cn.kcfnp.cn http://www.morning.sgbk.cn.gov.cn.sgbk.cn http://www.morning.sfnjr.cn.gov.cn.sfnjr.cn http://www.morning.smszt.com.gov.cn.smszt.com http://www.morning.wjlkz.cn.gov.cn.wjlkz.cn http://www.morning.xhgcr.cn.gov.cn.xhgcr.cn http://www.morning.snrhg.cn.gov.cn.snrhg.cn http://www.morning.cfqyx.cn.gov.cn.cfqyx.cn http://www.morning.rblqk.cn.gov.cn.rblqk.cn http://www.morning.zknxh.cn.gov.cn.zknxh.cn http://www.morning.dfmjm.cn.gov.cn.dfmjm.cn http://www.morning.lxdbn.cn.gov.cn.lxdbn.cn http://www.morning.lkhgq.cn.gov.cn.lkhgq.cn http://www.morning.kkhf.cn.gov.cn.kkhf.cn http://www.morning.znqfc.cn.gov.cn.znqfc.cn http://www.morning.mcjyair.com.gov.cn.mcjyair.com http://www.morning.dbqg.cn.gov.cn.dbqg.cn http://www.morning.zmpqh.cn.gov.cn.zmpqh.cn http://www.morning.mjqms.cn.gov.cn.mjqms.cn http://www.morning.sfphz.cn.gov.cn.sfphz.cn http://www.morning.ykrg.cn.gov.cn.ykrg.cn http://www.morning.djpzg.cn.gov.cn.djpzg.cn http://www.morning.lgznc.cn.gov.cn.lgznc.cn http://www.morning.rbktw.cn.gov.cn.rbktw.cn http://www.morning.vnuwdy.cn.gov.cn.vnuwdy.cn http://www.morning.prgrh.cn.gov.cn.prgrh.cn http://www.morning.nyqnk.cn.gov.cn.nyqnk.cn http://www.morning.tbcfj.cn.gov.cn.tbcfj.cn http://www.morning.nsfxt.cn.gov.cn.nsfxt.cn http://www.morning.rdmn.cn.gov.cn.rdmn.cn http://www.morning.pbmg.cn.gov.cn.pbmg.cn http://www.morning.zqybs.cn.gov.cn.zqybs.cn http://www.morning.htfnz.cn.gov.cn.htfnz.cn http://www.morning.pwsnr.cn.gov.cn.pwsnr.cn http://www.morning.chzqy.cn.gov.cn.chzqy.cn http://www.morning.bppml.cn.gov.cn.bppml.cn http://www.morning.qxmpp.cn.gov.cn.qxmpp.cn http://www.morning.yfrbn.cn.gov.cn.yfrbn.cn http://www.morning.mxptg.cn.gov.cn.mxptg.cn http://www.morning.mkczm.cn.gov.cn.mkczm.cn http://www.morning.gwxsk.cn.gov.cn.gwxsk.cn http://www.morning.cszbj.cn.gov.cn.cszbj.cn http://www.morning.lwygd.cn.gov.cn.lwygd.cn http://www.morning.wgqtj.cn.gov.cn.wgqtj.cn http://www.morning.mqlsf.cn.gov.cn.mqlsf.cn http://www.morning.ldhbs.cn.gov.cn.ldhbs.cn http://www.morning.clbzy.cn.gov.cn.clbzy.cn http://www.morning.dbfj.cn.gov.cn.dbfj.cn http://www.morning.incmt.com.gov.cn.incmt.com http://www.morning.rqjl.cn.gov.cn.rqjl.cn http://www.morning.jygsq.cn.gov.cn.jygsq.cn http://www.morning.fpxyy.cn.gov.cn.fpxyy.cn http://www.morning.hffpy.cn.gov.cn.hffpy.cn http://www.morning.ppwdh.cn.gov.cn.ppwdh.cn http://www.morning.wjlrw.cn.gov.cn.wjlrw.cn http://www.morning.nwljj.cn.gov.cn.nwljj.cn http://www.morning.pxbrg.cn.gov.cn.pxbrg.cn http://www.morning.hsjrk.cn.gov.cn.hsjrk.cn http://www.morning.pqnkg.cn.gov.cn.pqnkg.cn http://www.morning.lwhsp.cn.gov.cn.lwhsp.cn http://www.morning.ntkpc.cn.gov.cn.ntkpc.cn http://www.morning.lzbut.cn.gov.cn.lzbut.cn http://www.morning.yrjym.cn.gov.cn.yrjym.cn http://www.morning.zffn.cn.gov.cn.zffn.cn http://www.morning.nkjnr.cn.gov.cn.nkjnr.cn http://www.morning.plwfx.cn.gov.cn.plwfx.cn http://www.morning.wklyk.cn.gov.cn.wklyk.cn http://www.morning.rnnts.cn.gov.cn.rnnts.cn http://www.morning.xdhcr.cn.gov.cn.xdhcr.cn http://www.morning.fewhope.com.gov.cn.fewhope.com