aboutsummaryrefslogtreecommitdiffstats
path: root/YouTube_to_WebDAV.ipynb
blob: ba7bfb0034bc6cfe6c1c2cec25367fd7ed6837dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# YouTube to WebDAV\n",
        "This script allows for a YouTube video to be downloaded and then automatically uplaoded via WebDAV\n",
        "\n",
        "Usecase is useful for running Syncplay or similar software in situations where running yt-dlp may not be viable\n",
        "\n",
        "This approach can still fail in cases where JS solver is required\n",
        "\n",
        "# What this does?\n",
        "- Downloads vidoe via yt-dlp\n",
        "- Downloads all official subtitles (non-auto-generated)\n",
        "- Mux into single MKV file\n",
        "- Upload via WebDav\n",
        "\n",
        "Optionally if you are using Nextcloud you can also automatically generate a share\n",
        "\n",
        "# Usage\n",
        "Fill in all the fields below. Then `Run all`"
      ],
      "metadata": {
        "id": "L9cXBP9OPmFt"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "vHGZttQWK8QJ"
      },
      "outputs": [],
      "source": [
        "# Install the dependencies needed\n",
        "!pip install yt-dlp webdavclient3"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from webdav3.client import Client\n",
        "webdav_url = \"\"  #@param {type:\"string\"}\n",
        "username = \"\"  #@param {type:\"string\"}\n",
        "password = \"\"  #@param {type:\"string\"}\n",
        "options = {\n",
        "    'webdav_hostname': webdav_url,\n",
        "    'webdav_login':    username,\n",
        "    'webdav_password': password\n",
        "}\n",
        "client = Client(options)\n",
        "try:\n",
        "  client.list()\n",
        "  print(\"Success! Connection successful\")\n",
        "except:\n",
        "  print(\"Login failed. Please check that your login and WebDAV url are correct\")"
      ],
      "metadata": {
        "cellView": "form",
        "id": "GAdz5SlkOIJO"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "youtube_url = \"\" #@param{type:\"string\"}\n",
        "\n",
        "!yt-dlp -f 'bv*+ba/best' \\\n",
        "  --merge-output-format mkv \\\n",
        "  --embed-subs \\\n",
        "  --write-subs \\\n",
        "  --sub-langs all \\\n",
        "  \"$youtube_url\""
      ],
      "metadata": {
        "cellView": "form",
        "id": "GtiPVBYSQ1_6"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Uploads the file to the path specified\n",
        "download_path = \"\" #@param{type:\"string\"}\n",
        "\n",
        "import glob\n",
        "\n",
        "mkv_files = glob.glob(\"/content/*.mkv\")\n",
        "sub_files = glob.glob(\"/content/*.vtt\") + glob.glob(\"/content/*.srt\")\n",
        "if not mkv_files:\n",
        "    raise FileNotFoundError(\"MKV file not found! Did the download fail?\")\n",
        "original_mkv_path = mkv_files[0]\n",
        "remote_filename = f\"{download_path}/\" + original_mkv_path.split(\"/\")[-1]\n",
        "client.upload_sync(remote_path=remote_filename, local_path=original_mkv_path)\n",
        "\n",
        "print(f\"Done! Your file has been uploaded to {remote_filename}\")"
      ],
      "metadata": {
        "cellView": "form",
        "id": "3xYJpTl5SeVz"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "# OPTIONAL NEXTCLOUD AUTO SHARE\n",
        "- This uses Nextcloud API to create a public share URL automatically. Completes the workflow"
      ],
      "metadata": {
        "id": "uabtse49Vy-L"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import requests\n",
        "nextcloud_url = \"\" #@param {type:\"string\"}\n",
        "api_url = f\"{nextcloud_url}/ocs/v2.php/apps/files_sharing/api/v1/shares\"\n",
        "payload = {\n",
        "    \"path\": remote_filename,\n",
        "    \"shareType\": 3,        # 3 = public link\n",
        "    \"permissions\": 31      # optional, full permissions (read/write/share)\n",
        "}\n",
        "\n",
        "headers = {\n",
        "    \"OCS-APIRequest\": \"true\"\n",
        "}\n",
        "response = requests.post(api_url, auth=(username, password), headers=headers, data=payload)\n",
        "if response.status_code == 200:\n",
        "    import xml.etree.ElementTree as ET\n",
        "    root = ET.fromstring(response.content)\n",
        "    ns = {\"ocs\": \"http://open-collaboration-services.org/ns\"}\n",
        "    url_elem = root.find(\".//url\")\n",
        "    if url_elem is not None:\n",
        "        share_link = url_elem.text\n",
        "        print(\"Share link:\", share_link+\"/download\")\n",
        "    else:\n",
        "        print(\"Could not find share URL in response\")\n",
        "else:\n",
        "    print(\"Error:\", response.status_code, response.text)"
      ],
      "metadata": {
        "cellView": "form",
        "id": "MSmhsS6PUxaG"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Cleanup/Delete old files\n",
        "import os\n",
        "mkv_files = glob.glob(\"/content/*.mkv\")\n",
        "sub_files = glob.glob(\"/content/*.vtt\") + glob.glob(\"/content/*.srt\")\n",
        "for f in mkv_files + sub_files:\n",
        "    os.remove(f)\n",
        "    print(f\"Deleted local file: {f}\")"
      ],
      "metadata": {
        "id": "xBprVc4OUCor"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage