首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用- youtube API v3 - React JS将Youtube视频转换为mp3

使用- youtube API v3 - React JS将Youtube视频转换为mp3
EN

Stack Overflow用户
提问于 2021-11-21 10:18:30
回答 1查看 621关注 0票数 0

我想使用Reactjs将视频转换为mp3。

我用Youtube API v3从Youtube制作了一个API Key。

我需要获取mp3下载链接!

使用此应用程序,您只需将链接放入文本输入即可获取视频信息。

代码语言:javascript
运行
复制
import React, { useRef, useState } from "react"
import "./style.scss"
import logo from "./logo.png"

export default function App() {
    const videoLink = useRef("")
    const [video, setVideo] = useState(false)
    const [items, setItems] = useState([])

    const matchYoutubeUrl = (url) => {
        var p = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/
        var matches = url.match(p)
        if (matches) {
            return matches[1]
        } else {
            alert("Please enter a valid link!")
        }
        return false;
    }
    const convertVideo = () => {
        if (videoLink.current.value === "") {
            alert("Don't forget the link...")
        } else {
            const newUrl = matchYoutubeUrl(videoLink.current.value)
            if (newUrl !== false) {
                setVideo(true)
                const apiUrl = "https://www.googleapis.com/youtube/v3/videos?part=snippet&id=" + newUrl + "&key=<api_key>"
                fetch(apiUrl)
                    .then(response => response.json())
                    .then(data => {
                        setItems(data.items)
                        console.log(data)
                    })
            }
        }
    }
    return (
        <>
            <img src={logo} id="logo" alt={logo} />
            <h1>Converter Youtube</h1>
            <div id="App">
                <div className="inputs">
                    <input type="text" placeholder="Video Link" ref={videoLink} autoFocus />
                    <input type="button" value="CONVERT VIDEO" onClick={() => convertVideo()} id="convert" />
                </div>
                <div style={{display: video ? "inline" : "none"}}>
                    {
                        items.map((item, key) => {
                                var description
                                if (item.snippet.description !== "") {
                                    description = item.snippet.description.substring(0, 100) + "..."
                                }
                                return (
                                    <div key={key}>
                                        <img src={item.snippet.thumbnails.high.url} height={item.snippet.thumbnails.default.height+50} width={item.snippet.thumbnails.default.width+50} alt={item.snippet.title} id="image" />
                                        <div id="channel"><p id="title">{item.snippet.title}</p></div>
                                        <p id="description">{description}</p>
                                        <a href="sdadsa.png" download={`${item.snippet.title}.mp3`}>
                                            <button type="button">Download</button>
                                        </a>
                                    </div>
                                )
                            }
                        )
                    }
                </div>
            </div>
            <div id="App" className="footer">
                <span>Copyright 2021</span>
            </div>
        </>
    )
}

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2021-11-21 12:10:51

根据我从主要问题下面的评论中所了解到的。这是你需要做的。

预演逻辑( Run):

  1. 用户添加链接后,通过api将链接发送到Node.js
  2. 等服务器。您可以使用youtube-dl-exec
  3. Once等npm包下载视频文件,您可以使用文件源将其转换为mp3格式。您可以使用像video-to-audio

这样的包

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70053598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档