Creating a Simple YouTube Video Download App in Flutter
Tags: python, beginners, tutorial, flutter
Published: 2024-02-09 14:53:50

In today’s digital age, online videos have become an integral part of our lives. YouTube, being the most popular video-sharing platform, is home to a vast collection of videos covering various topics. Often, we come across videos that we’d like to download for offline viewing or sharing with friends. While YouTube’s terms of service prohibit downloading videos without permission, there are legitimate use cases for downloading videos, such as educational content or videos for personal use.

In this tutorial, we will create a simple YouTube video download app using Flutter. We will use the youtube_explode_dart package to fetch video information and download videos. Note that downloading YouTube videos without the permission of the content creator may violate YouTube's terms of service and copyright laws. Ensure you have the right to download and use videos before proceeding.

**Prerequisites**
Before we get started, make sure you have the following installed:

Flutter
Dart
Step 1: Create a New Flutter Project
Let’s start by creating a new Flutter project. Open your terminal and run the following commands:

```
flutter create youtube_downloader
cd youtube_downloader
```
This will create a new Flutter project named “youtube_downloader.”

#Step 2: Add Dependencies# 
To download YouTube videos, we will use the `youtube_explode_dart` package. Open the `pubspec.yaml` file in your project and add the following dependency:

```
dependencies:
  flutter:
    sdk: flutter
  youtube_explode_dart: ^2.1.3
```

#Step 3: Design the User Interface# 
Let’s design a simple user interface for our app. Open the lib/main.dart file and replace its contents with the following code:

```
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'YouTube Downloader',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('YouTube Downloader'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Enter YouTube URL:',
              style: TextStyle(fontSize: 16),
            ),
            SizedBox(height: 20),
            Container(
              width: 300,
              child: TextField(
                decoration: InputDecoration(
                  hintText: 'https://www.youtube.com/watch?v=...',
                ),
              ),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                // Implement video download logic here
              },
              child: Text('Download Video'),
            ),
          ],
        ),
      ),
    );
  }
}
```
This code sets up a basic Flutter app with a text input field where users can enter the YouTube video URL and a button to trigger the download process.

## Step 4: Implement Video Download Logic

Now, let’s implement the video download logic. Replace the comment in the onPressed callback of the "Download Video" button with the following code:

```
import 'package:youtube_explode_dart/youtube_explode_dart.dart';

void _downloadVideo(String url) async {
  var ytExplode = YoutubeExplode();
  var video = await ytExplode.videos.get(url);

  var manifest = await ytExplode.videos.streamsClient.getManifest(video);

  var streamInfo = manifest.audioOnly.first;
  var videoStream = manifest.video.first;

  var audioStream = ytExplode.videos.streamsClient.get(streamInfo);
  var videoFile = await ytExplode.videos.streamsClient.get(videoStream);

  // Implement file saving logic here
}

ElevatedButton(
  onPressed: () {
    String videoUrl = 'https://www.youtube.com/watch?v=...'; // Replace with user input
    _downloadVideo(videoUrl);
  },
  child: Text('Download Video'),
),

```
In this code, we use the `youtube_explode_dart` package to fetch video information and download the video. Replace `'https://www.youtube.com/watch?v=...'` with the user-provided URL.

## Step 5: Save the Video File
To save the downloaded video, you can use the dart:io package to write the video file to the device's storage. Here's how you can do it:

```
import 'dart:io';

void _saveVideo(File videoFile, String videoTitle) async {
  final appDocDir = await getApplicationDocumentsDirectory();
  final savePath = appDocDir.path + '/$videoTitle.mp4';

  final videoBytes = await videoFile.readAsBytes();
  final File file = File(savePath);

  await file.writeAsBytes(videoBytes);

  // Show a success message or handle errors here
}
```

Step 6: Display Progress and Errors
You can implement progress tracking and error handling to provide a better user experience. Use Flutter’s widgets like `CircularProgressIndicator` for progress and `SnackBar` for error messages.

In this tutorial, we’ve created a simple YouTube video download app using Flutter. While this app demonstrates the basic functionality, remember to comply with YouTube’s terms of service and copyright laws when downloading videos. Additionally, you can enhance the app by adding features like video quality selection, video format options, and a user-friendly interface.

Feel free to explore more Flutter packages and libraries to improve the functionality and user experience of your YouTube video downloader app. Happy coding!

with this i have create my site that helps user to [download youtube videos](https://savetube.li/).

LOVE It