# How to preserve videos

# Prerequisites

1. Download yt-dlp with the package manager of your choice. Examples: ```bash
    sudo wget https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -O /usr/local/bin/yt-dlp
    sudo chmod a+rx /usr/local/bin/yt-dlp
    
    python3 -m pip install -U yt-dlp
    
    yay yt-dlp
    
    sudo apt install yt-dlp
    
    sudo nala install yt-dlp
    ```
2. Download ffmpg with the package manager of your choice. Examples: ```bash
    yay ffmpeg
    
    sudo apt install ffmpeg
    
    sudo nala install ffmpeg
    ```
3. Download optional dependencies if necessary from [here ](https://github.com/yt-dlp/yt-dlp#dependencies)
4. Make a directory where your config files will sit
5. Create your first config. Example: ```bash
    # best video and audio
    -f bestvideo+bestaudio 
    
    # Just go on even if everything burns
    -ciw 
    
    # verbose
    -v
    
    # Thumbnail
    --write-thumbnail
    --embed-thumbnail
    
    # Add chapters
    --embed-chapters
    
    # Embed info json
    --write-info-json
    --embed-info-json 
    
    # Metadata
    --embed-metadata
    
    # Description
    --write-description
    
    # Comments
    #--write-comments
    
    # Mark Video as watched. May count towards view count.
    --mark-watched
    
    # Merge downloaded video and audio into a mkv file
    --merge-output-format mkv
    ```
6. More file examples can be found [here](http://192.168.0.69:3000/ProfessionalUwU/yt-dlp)

# Time to archive

#### How to archive an entire YouTube channel

1. Create a directory where you want your files to be
2. [Download](https://gitea.hopeless-cloud.xyz/Hopeless-cloud/Scripts) or create the necessary files to create directories, move files and download videos. Examples: ```bash
    #!/bin/sh
    
    if [ ! -d "video" ] ; then
    mkdir video && mkdir metadata && mkdir thumbnails && mkdir descriptions && echo "Created directories"
    fi
    ```
    
    ```bash
    #!/bin/bash
    
    if compgen -G "*.mkv" > /dev/null ; then
    mv *.mkv video/ && mv *.webp thumbnails/ && mv *.description descriptions/ && mv *.info.json metadata/ && echo "Moved files"
    fi
    ```
    
    ```bash
    #!/bin/bash
    yt-dlp --config-location /srv/dev-disk-by-uuid-fa1e36be-9ccc-4549-8734-fe6117b30317/croni/andre/andre/Archive/yt-dlp/yt-dlp_video.conf --download-archive youtube_backup_downloaded.txt --yes-playlist 'https://youtube.com/playlist?list=PLeEbifL6wH9okMEgTQP459RcTrjBWkF_b'
    ```
3. Execute these files however you want. I personally would recommend to set up a cron job to periodically download new videos. Example: ```bash
    nohup ./backup.sh > backup.log &
    ```
4. If you execute the command above your only way to know what yt-dlp is currently doing is to tail the log with `tail -f backup.log`
5. While waiting for the download to finish you can create the necessary directories to organize all your downloaded files. Example: ```bash
    #!/bin/sh
    
    if [ ! -d "video" ] ; then
    mkdir video && mkdir metadata && mkdir thumbnails && mkdir descriptions && echo "Created directories"
    fi
    ```
6. When the download is finished an you created the directories it is time to move them to their dedicated directory. Example: ```bash
    #!/bin/bash
    
    if compgen -G "*.mkv" > /dev/null ; then
    mv *.mkv video/ && mv *.webp thumbnails/ && mv *.description descriptions/ && mv *.info.json metadata/ && echo "Moved files"
    fi
    ```
7. You successfully archived an entire YouTube channel <span aria-label="rocket" class="emoji">🚀</span>

#### <span aria-label="rocket" class="emoji">Tips</span>

- <span aria-label="rocket" class="emoji">Make your files executable with `sudo chmod +x file.sh`</span>
- <span aria-label="rocket" class="emoji">You can execute the files with `./file.sh`</span>
- <span aria-label="rocket" class="emoji">This guide can be adapted to download playlist and even single videos</span>
    - <span aria-label="rocket" class="emoji">If you only want to download one video then remove `--yes-playlist`</span>