我一直在尝试创建一种直接从 python 代码流式传输 youtube url(最好只有音频,尽管这并不重要)的方法。我尝试了很多东西,但似乎没有一个真正有效。到目前为止,我能够使用 youtube 数据 api 搜索视频或播放列表,抓取第一个视频或播放列表并将其传递给 pafy 以获得不同的流媒体网址。有谁知道一种无需先下载视频即可通过 python 播放 youtube 音频/视频的方法?我认为可以使用 mplayer 或 vlc 等 cmd 行工具使用子进程为 cmd 行弹出一个 cmd 并传入 url,但我被卡住了。需要任何帮助。请!这是我的以下代码:
import argparse
import pafy
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
DEVELOPER_KEY = 'DEVELOPER KEY'
YOUTUBE_API_SERVICE_NAME = 'youtube'
YOUTUBE_API_VERSION = 'v3'
def pafy_video(video_id):
url = 'https://www.youtube.com/watch?v={0}'.format(video_id)
vid = pafy.new(url)
def pafy_playlist(playlist_id)
url = "https://www.youtube.com/playlist?list={0}".format(playlist_id)
playlist = pafy.get_playlist(url)
def youtube_search(options):
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)
search_response = youtube.search().list(
q='Hello world',
part='id,snippet',
maxResults=options.max_results
).execute()
videos …
Run Code Online (Sandbox Code Playgroud) 我有一个简单的 python Flask 应用程序。当我的一位用户在我的主页上发表评论时,我想将他们直接重定向回主页。目前,我有处理主页和评论的相同路线。它看起来像这样:
@app.route('/home', methods=['POST', 'GET'])
@login_required
def home():
form = CommentForm()
user = User.query.filter_by(username=current_user.username).first()
house = House.query.filter_by(owner_id = current_user.id).first()
neighbors = Neighbors.query.filter_by(houseid=house.id).all()
if user.id == current_user.id:
if form.validate_on_submit():
time = strftime("%a, %b %d %Y %X", localtime())
comment = Commentsection(houseid = house.id, commentbody = form.body.data, times = str(time), commenter=user.username, commenterid=user.id, commenterpic = user.image_file )
db.session.add(comment)
db.session.commit()
link = url_for('add', invite_token=house.linkinv, _external=True)
comments = Commentsection.query.filter_by(houseid=house.id).order_by(Commentsection.id.desc()).all()
return redirect(url_for('home', title='Home', house=house, form=form, comments=comments, link=link, neighbors=neighbors))
else:
link = url_for('add', invite_token=house.linkinv, _external=True)
comments …
Run Code Online (Sandbox Code Playgroud) 我的程序中有两个数组,一个包含充当键的字符串列表,另一个包含包含键的对象。
let classKeys = ['math', 'robotics'];
let classesInList = [{key : "WIHUGDYVWJ", className : 'math'},{key : "qwljdhkuqwdnqwdk", className : 'english'},{key : "likubqwd", className : 'robotics'},];
Run Code Online (Sandbox Code Playgroud)
仅当 classKeys 中存在键(作为 className 属性)时,我才需要返回另一个包含 classesInList 等对象的数组
例如,在这种情况下,我需要根据 classesInList 检查 classKeys 并创建一个包含两个项目的数组:
[{key : "WIHUGDYVWJ", className : 'math'},{key : "likubqwd", className : 'robotics'}]
Run Code Online (Sandbox Code Playgroud)
由于英语不在 classKeys 中,因此它从 classesInList 中删除。我该怎么办?
旁注:我使用的是 react native,所以我可以访问 es6 属性和函数。
我完全被困在这个意义上。无论这看起来多么简单,我都找不到答案。