Create Tiktok video downloader using Next.js with tailwind css
To create a TikTok video downloader using Next.js and Tailwind CSS, you would need to follow a similar process to the one outlined in my previous answer, but with the addition of styling the frontend using Tailwind CSS. Here’s an example of what the code might look like:
- First, you would need to install the packages “tiktok-scraper” and “tailwindcss” by running the following command:
npm install tiktok-scraper tailwindcss
2. Next, you would create an API route in your Next.js application that handles the request to download a video, as well as a React component that displays a form for the user to enter a TikTok video URL:
import tikTokScraper from 'tiktok-scraper';
import axios from 'axios';
import { NextPage } from 'next';
import { useState } from 'react';
const DownloadPage: NextPage = () => {
const [url, setUrl] = useState('');
const handleSubmit = async (e) => {
e.preventDefault();
try {
const videoData = await tikTokScraper.getVideoMeta(url);
const videoUrl = videoData.videoUrl;
const video = await axios({
url: videoUrl,
method: 'GET',
responseType: 'stream',
});
const { data } = video;
const fileName = `${videoData.authorMeta.name}-${videoData.id}.mp4`;
const contentType = 'video/mp4';
const headers = {
'Content-Disposition': `attachment; filename="${fileName}"`,
'Content-Type': contentType
}
res.setHeader('Content-Disposition', headers['Content-Disposition']);
res.setHeader('Content-Type', headers['Content-Type']);
data.pipe(res);
} catch (err) {
console.error(err);
}
}
return (
<form onSubmit={handleSubmit} className="bg-gray-200 p-6 rounded-lg">
<label className="block text-gray-700 font-medium mb-2">
TikTok Video URL:
</label>
<input
className="bg-white focus:outline-none focus:shadow-outline border border-gray-300 rounded-lg py-2 px-4 block w-full appearance-none leading-normal"
type="text"
value={url}
onChange={(e) => setUrl(e.target.value)}
/>
<button className="bg-indigo-500 hover:bg-indigo-600 text-white font-medium py-2 px-4 rounded-lg mt-4">
Download
</button>
</form>
);
};
export default DownloadPage;
3. Then you need to import tailwindcss in your Nextjs application, you can do that by adding the following in your next.config.js
const withCSS = require('@zeit/next-css')
module.exports = withCSS({
cssModules:false,
cssLoaderOptions: {
url: false
},
webpack(config, options) {
config.module.rules.push({
test: /.css$/,
use: [
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
plugins: [require('tailwindcss'), require('autoprefixer')]
}
}
]
});
return config;
}
});
Then you can create a CSS file and import tailwindcss to use its classes
```css
@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';
Note that this is just an example and there are many ways to improve this functionality, also as I mentioned before, TikTok’s terms of service prohibit downloading videos without permission from the video owner, so you should always obtain the necessary rights before proceeding. Additionally, TikTok may change the URLs of their videos frequently, so this approach may not be reliable in the long term.