1031 lines
85 KiB
HTML
1031 lines
85 KiB
HTML
<!DOCTYPE html>
|
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
|
|
|
|
<meta charset="utf-8">
|
|
<meta name="generator" content="quarto-1.5.46">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
|
|
|
|
|
|
<title>Miris Chrismas list</title>
|
|
<style>
|
|
code{white-space: pre-wrap;}
|
|
span.smallcaps{font-variant: small-caps;}
|
|
div.columns{display: flex; gap: min(4vw, 1.5em);}
|
|
div.column{flex: auto; overflow-x: auto;}
|
|
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
|
|
ul.task-list{list-style: none;}
|
|
ul.task-list li input[type="checkbox"] {
|
|
width: 0.8em;
|
|
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
|
|
vertical-align: middle;
|
|
}
|
|
/* CSS for syntax highlighting */
|
|
pre > code.sourceCode { white-space: pre; position: relative; }
|
|
pre > code.sourceCode > span { line-height: 1.25; }
|
|
pre > code.sourceCode > span:empty { height: 1.2em; }
|
|
.sourceCode { overflow: visible; }
|
|
code.sourceCode > span { color: inherit; text-decoration: inherit; }
|
|
div.sourceCode { margin: 1em 0; }
|
|
pre.sourceCode { margin: 0; }
|
|
@media screen {
|
|
div.sourceCode { overflow: auto; }
|
|
}
|
|
@media print {
|
|
pre > code.sourceCode { white-space: pre-wrap; }
|
|
pre > code.sourceCode > span { display: inline-block; text-indent: -5em; padding-left: 5em; }
|
|
}
|
|
pre.numberSource code
|
|
{ counter-reset: source-line 0; }
|
|
pre.numberSource code > span
|
|
{ position: relative; left: -4em; counter-increment: source-line; }
|
|
pre.numberSource code > span > a:first-child::before
|
|
{ content: counter(source-line);
|
|
position: relative; left: -1em; text-align: right; vertical-align: baseline;
|
|
border: none; display: inline-block;
|
|
-webkit-touch-callout: none; -webkit-user-select: none;
|
|
-khtml-user-select: none; -moz-user-select: none;
|
|
-ms-user-select: none; user-select: none;
|
|
padding: 0 4px; width: 4em;
|
|
}
|
|
pre.numberSource { margin-left: 3em; padding-left: 4px; }
|
|
div.sourceCode
|
|
{ }
|
|
@media screen {
|
|
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
|
|
}
|
|
</style>
|
|
|
|
|
|
<script src="audioconversion_files/libs/clipboard/clipboard.min.js"></script>
|
|
<script src="audioconversion_files/libs/quarto-html/quarto.js"></script>
|
|
<script src="audioconversion_files/libs/quarto-html/popper.min.js"></script>
|
|
<script src="audioconversion_files/libs/quarto-html/tippy.umd.min.js"></script>
|
|
<script src="audioconversion_files/libs/quarto-html/anchor.min.js"></script>
|
|
<link href="audioconversion_files/libs/quarto-html/tippy.css" rel="stylesheet">
|
|
<link href="audioconversion_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
|
|
<script src="audioconversion_files/libs/bootstrap/bootstrap.min.js"></script>
|
|
<link href="audioconversion_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
|
|
<link href="audioconversion_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
|
|
|
|
|
|
</head>
|
|
|
|
<body class="fullcontent">
|
|
|
|
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
|
|
|
|
<main class="content" id="quarto-document-content">
|
|
|
|
<header id="title-block-header" class="quarto-title-block default">
|
|
<div class="quarto-title">
|
|
<h1 class="title">Miris Chrismas list</h1>
|
|
</div>
|
|
|
|
|
|
|
|
<div class="quarto-title-meta">
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</header>
|
|
|
|
|
|
<div id="cell-1" class="cell" data-execution_count="2">
|
|
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> pathlib <span class="im">import</span> Path</span>
|
|
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>cd1<span class="op">=</span>Path(<span class="st">'/home/dl92/Music/cd#1'</span>)</span>
|
|
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>cd2<span class="op">=</span>Path(<span class="st">'/home/dl92/Music/cd#2'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|
</div>
|
|
<div id="cell-2" class="cell" data-execution_count="3">
|
|
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> pydub <span class="im">import</span> AudioSegment</span>
|
|
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> fname <span class="kw">in</span> cd1.glob(<span class="st">'**/*.wav'</span>):</span>
|
|
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a> song <span class="op">=</span> AudioSegment.frfrom_wav(fname)</span>
|
|
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a> <span class="co">#print(fname.stem)</span></span>
|
|
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a> fname_mp3<span class="op">=</span>fname.parent<span class="op">/</span><span class="st">'mp3'</span><span class="op">/</span><span class="ss">f'</span><span class="sc">{</span>fname<span class="sc">.</span>stem<span class="sc">}</span><span class="ss">.mp3'</span></span>
|
|
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(fname_mp3)</span>
|
|
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a> song.export(fname_mp3,<span class="bu">format</span><span class="op">=</span><span class="st">'mp3'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|
<div class="cell-output cell-output-error">
|
|
<div class="ansi-escaped-output">
|
|
<pre><span class="ansi-red-fg">---------------------------------------------------------------------------</span>
|
|
<span class="ansi-red-fg">ModuleNotFoundError</span> Traceback (most recent call last)
|
|
<span class="ansi-green-fg ansi-bold">/home/ys/Data/projects/code/Python/Scripts/audioconversion.ipynb Cell 2</span> line <span class="ansi-cyan-fg">1
|
|
</span><span class="ansi-green-fg">----> <a href='vscode-notebook-cell:/home/ys/Data/projects/code/Python/Scripts/audioconversion.ipynb#W1sZmlsZQ%3D%3D?line=0'>1</a></span> from pydub import AudioSegment
|
|
<span class="ansi-green-fg ansi-bold"> <a href='vscode-notebook-cell:/home/ys/Data/projects/code/Python/Scripts/audioconversion.ipynb#W1sZmlsZQ%3D%3D?line=2'>3</a></span> for fname in cd1.glob('**/*.wav'):
|
|
<span class="ansi-green-fg ansi-bold"> <a href='vscode-notebook-cell:/home/ys/Data/projects/code/Python/Scripts/audioconversion.ipynb#W1sZmlsZQ%3D%3D?line=3'>4</a></span> song = AudioSegment.frfrom_wav(fname)
|
|
|
|
<span class="ansi-red-fg">ModuleNotFoundError</span>: No module named 'pydub'</pre>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div id="cell-3" class="cell">
|
|
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pytube</span>
|
|
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> pytube <span class="im">import</span> YouTube</span>
|
|
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="co">#yt=YouTube('https://www.youtube.com/watch?v=M73x3O7dhmg')#philip glass</span></span>
|
|
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="co">#yt=YouTube('https://www.youtube.com/watch?v=K3RjISiW7gA')#Reinhardt - The Best of Jazz Guitar (The Greatest Jazz Masterpieces) [Standard Jazz Tracks].</span></span>
|
|
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a>yt<span class="op">=</span>YouTube(<span class="st">'https://www.youtube.com/watch?v=Ty4isRlrrbg'</span>)</span>
|
|
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a>yt<span class="op">=</span>YouTube(<span class="st">'https://www.youtube.com/watch?v=G6kAmdzfvC8'</span>)</span>
|
|
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a><span class="co">#yt.streaming_data</span></span>
|
|
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a>(<span class="bu">print</span>(yt.streams</span>
|
|
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a> <span class="co">#.filter(res='720p',mime_type='video/mp4')</span></span>
|
|
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a> .<span class="bu">filter</span>(progressive<span class="op">=</span><span class="va">True</span>,mime_type<span class="op">=</span><span class="st">'video/mp4'</span>)</span>
|
|
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a> )</span>
|
|
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb3-14"><a href="#cb3-14" aria-hidden="true" tabindex="-1"></a><span class="co">#.desc()</span></span>
|
|
<span id="cb3-15"><a href="#cb3-15" aria-hidden="true" tabindex="-1"></a>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|
<div class="cell-output cell-output-error">
|
|
<div class="ansi-escaped-output">
|
|
<pre><span class="ansi-red-fg ansi-bold">Running cells with 'General' requires the ipykernel package.
|
|
|
|
</span><span class="ansi-red-fg ansi-bold">Run the following command to install 'ipykernel' into the Python environment.
|
|
|
|
</span><span class="ansi-red-fg ansi-bold">Command: '/home/ys/Data/devtools/Envs/General/bin/python3.10 -m pip install ipykernel -U --force-reinstall'</span></pre>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div id="cell-4" class="cell" data-execution_count="1">
|
|
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> pytube <span class="im">import</span> YouTube</span>
|
|
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> moviepy.editor <span class="im">as</span> mp</span>
|
|
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> youtube_transcript_api <span class="im">import</span> YouTubeTranscriptApi</span>
|
|
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> remove_timestamps(transcript):</span>
|
|
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a> <span class="co">"""</span></span>
|
|
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a><span class="co"> Removes timestamps from a transcript string.</span></span>
|
|
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a><span class="co"> Args:</span></span>
|
|
<span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a><span class="co"> transcript: A string containing the transcript with timestamps.</span></span>
|
|
<span id="cb4-11"><a href="#cb4-11" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb4-12"><a href="#cb4-12" aria-hidden="true" tabindex="-1"></a><span class="co"> Returns:</span></span>
|
|
<span id="cb4-13"><a href="#cb4-13" aria-hidden="true" tabindex="-1"></a><span class="co"> A string containing the transcript without timestamps.</span></span>
|
|
<span id="cb4-14"><a href="#cb4-14" aria-hidden="true" tabindex="-1"></a><span class="co"> """</span></span>
|
|
<span id="cb4-15"><a href="#cb4-15" aria-hidden="true" tabindex="-1"></a> clean_transcript <span class="op">=</span> <span class="st">""</span></span>
|
|
<span id="cb4-16"><a href="#cb4-16" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> line <span class="kw">in</span> transcript.splitlines():</span>
|
|
<span id="cb4-17"><a href="#cb4-17" aria-hidden="true" tabindex="-1"></a> <span class="co"># Check if the line starts with a timestamp</span></span>
|
|
<span id="cb4-18"><a href="#cb4-18" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> line.startswith(<span class="st">"["</span>):</span>
|
|
<span id="cb4-19"><a href="#cb4-19" aria-hidden="true" tabindex="-1"></a> <span class="co"># Split the line to separate the timestamp and text</span></span>
|
|
<span id="cb4-20"><a href="#cb4-20" aria-hidden="true" tabindex="-1"></a> timestamp, text <span class="op">=</span> line.split(<span class="st">"] "</span>)</span>
|
|
<span id="cb4-21"><a href="#cb4-21" aria-hidden="true" tabindex="-1"></a> <span class="co"># Add only the text to the clean transcript</span></span>
|
|
<span id="cb4-22"><a href="#cb4-22" aria-hidden="true" tabindex="-1"></a> clean_transcript <span class="op">+=</span> text <span class="op">+</span> <span class="st">"</span><span class="ch">\n</span><span class="st">"</span></span>
|
|
<span id="cb4-23"><a href="#cb4-23" aria-hidden="true" tabindex="-1"></a> <span class="cf">else</span>:</span>
|
|
<span id="cb4-24"><a href="#cb4-24" aria-hidden="true" tabindex="-1"></a> <span class="co"># Add the entire line to the clean transcript</span></span>
|
|
<span id="cb4-25"><a href="#cb4-25" aria-hidden="true" tabindex="-1"></a> clean_transcript <span class="op">+=</span> line <span class="op">+</span> <span class="st">"</span><span class="ch">\n</span><span class="st">"</span></span>
|
|
<span id="cb4-26"><a href="#cb4-26" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> clean_transcript.strip()</span>
|
|
<span id="cb4-27"><a href="#cb4-27" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb4-28"><a href="#cb4-28" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb4-29"><a href="#cb4-29" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> convert_mp4_to_mp3(video_path):</span>
|
|
<span id="cb4-30"><a href="#cb4-30" aria-hidden="true" tabindex="-1"></a> <span class="co">"""</span></span>
|
|
<span id="cb4-31"><a href="#cb4-31" aria-hidden="true" tabindex="-1"></a><span class="co"> This function converts an MP4 video to an MP3 audio file.</span></span>
|
|
<span id="cb4-32"><a href="#cb4-32" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb4-33"><a href="#cb4-33" aria-hidden="true" tabindex="-1"></a><span class="co"> Args:</span></span>
|
|
<span id="cb4-34"><a href="#cb4-34" aria-hidden="true" tabindex="-1"></a><span class="co"> video_path: The path to the MP4 video file.</span></span>
|
|
<span id="cb4-35"><a href="#cb4-35" aria-hidden="true" tabindex="-1"></a><span class="co"> audio_path: The path where you want to save the extracted MP3 audio file.</span></span>
|
|
<span id="cb4-36"><a href="#cb4-36" aria-hidden="true" tabindex="-1"></a><span class="co"> """</span></span>
|
|
<span id="cb4-37"><a href="#cb4-37" aria-hidden="true" tabindex="-1"></a> <span class="cf">try</span>:</span>
|
|
<span id="cb4-38"><a href="#cb4-38" aria-hidden="true" tabindex="-1"></a> <span class="co"># Load the video file</span></span>
|
|
<span id="cb4-39"><a href="#cb4-39" aria-hidden="true" tabindex="-1"></a> video <span class="op">=</span> mp.VideoFileClip(video_path)</span>
|
|
<span id="cb4-40"><a href="#cb4-40" aria-hidden="true" tabindex="-1"></a> bb<span class="op">=</span>video_path.split(<span class="st">'.'</span>)[<span class="dv">0</span>]</span>
|
|
<span id="cb4-41"><a href="#cb4-41" aria-hidden="true" tabindex="-1"></a> audio_path<span class="op">=</span><span class="ss">f'</span><span class="sc">{</span>bb<span class="sc">}</span><span class="ss">.mp3'</span></span>
|
|
<span id="cb4-42"><a href="#cb4-42" aria-hidden="true" tabindex="-1"></a> <span class="co"># Extract the audio from the video and save it as an MP3 file</span></span>
|
|
<span id="cb4-43"><a href="#cb4-43" aria-hidden="true" tabindex="-1"></a> video.audio.write_audiofile(audio_path)</span>
|
|
<span id="cb4-44"><a href="#cb4-44" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb4-45"><a href="#cb4-45" aria-hidden="true" tabindex="-1"></a> <span class="co"># Print a success message</span></span>
|
|
<span id="cb4-46"><a href="#cb4-46" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"Audio extracted and saved as: </span><span class="sc">{</span>audio_path<span class="sc">}</span><span class="ss">"</span>)</span>
|
|
<span id="cb4-47"><a href="#cb4-47" aria-hidden="true" tabindex="-1"></a> <span class="cf">except</span> <span class="pp">Exception</span> <span class="im">as</span> e:</span>
|
|
<span id="cb4-48"><a href="#cb4-48" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"Error converting MP4 to MP3: </span><span class="sc">{</span>e<span class="sc">}</span><span class="ss">"</span>)</span>
|
|
<span id="cb4-49"><a href="#cb4-49" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb4-50"><a href="#cb4-50" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> download_mp4(video_url, output_path<span class="op">=</span><span class="st">"video.mp4"</span>, resolution<span class="op">=</span><span class="st">"720p"</span>):</span>
|
|
<span id="cb4-51"><a href="#cb4-51" aria-hidden="true" tabindex="-1"></a> <span class="co">"""</span></span>
|
|
<span id="cb4-52"><a href="#cb4-52" aria-hidden="true" tabindex="-1"></a><span class="co"> Downloads an MP4 video from YouTube.</span></span>
|
|
<span id="cb4-53"><a href="#cb4-53" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb4-54"><a href="#cb4-54" aria-hidden="true" tabindex="-1"></a><span class="co"> Args:</span></span>
|
|
<span id="cb4-55"><a href="#cb4-55" aria-hidden="true" tabindex="-1"></a><span class="co"> video_url: The URL of the YouTube video.</span></span>
|
|
<span id="cb4-56"><a href="#cb4-56" aria-hidden="true" tabindex="-1"></a><span class="co"> output_path: The path to save the downloaded video.</span></span>
|
|
<span id="cb4-57"><a href="#cb4-57" aria-hidden="true" tabindex="-1"></a><span class="co"> resolution: The resolution of the video to download (default: 720p).</span></span>
|
|
<span id="cb4-58"><a href="#cb4-58" aria-hidden="true" tabindex="-1"></a><span class="co"> """</span></span>
|
|
<span id="cb4-59"><a href="#cb4-59" aria-hidden="true" tabindex="-1"></a> <span class="cf">try</span>:</span>
|
|
<span id="cb4-60"><a href="#cb4-60" aria-hidden="true" tabindex="-1"></a> <span class="co"># Get the YouTube object</span></span>
|
|
<span id="cb4-61"><a href="#cb4-61" aria-hidden="true" tabindex="-1"></a> yt <span class="op">=</span> YouTube(video_url)</span>
|
|
<span id="cb4-62"><a href="#cb4-62" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb4-63"><a href="#cb4-63" aria-hidden="true" tabindex="-1"></a> <span class="co"># Get video title and language</span></span>
|
|
<span id="cb4-64"><a href="#cb4-64" aria-hidden="true" tabindex="-1"></a> title <span class="op">=</span> yt.title</span>
|
|
<span id="cb4-65"><a href="#cb4-65" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span> (title)</span>
|
|
<span id="cb4-66"><a href="#cb4-66" aria-hidden="true" tabindex="-1"></a> <span class="co">#lang = yt.captions.all().get('en').language_code</span></span>
|
|
<span id="cb4-67"><a href="#cb4-67" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb4-68"><a href="#cb4-68" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb4-69"><a href="#cb4-69" aria-hidden="true" tabindex="-1"></a> <span class="co"># Use youtube_transcript_api to retrieve transcript</span></span>
|
|
<span id="cb4-70"><a href="#cb4-70" aria-hidden="true" tabindex="-1"></a> transcript <span class="op">=</span> YouTubeTranscriptApi.get_transcript(yt.video_id)</span>
|
|
<span id="cb4-71"><a href="#cb4-71" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb4-72"><a href="#cb4-72" aria-hidden="true" tabindex="-1"></a> <span class="co"># Access transcript data</span></span>
|
|
<span id="cb4-73"><a href="#cb4-73" aria-hidden="true" tabindex="-1"></a> text <span class="op">=</span> transcript[<span class="dv">0</span>][<span class="st">'text'</span>]</span>
|
|
<span id="cb4-74"><a href="#cb4-74" aria-hidden="true" tabindex="-1"></a> start_seconds <span class="op">=</span> transcript[<span class="dv">0</span>][<span class="st">'start'</span>]</span>
|
|
<span id="cb4-75"><a href="#cb4-75" aria-hidden="true" tabindex="-1"></a> end_seconds <span class="op">=</span> start_seconds<span class="op">+</span>transcript[<span class="dv">0</span>][<span class="st">'duration'</span>]</span>
|
|
<span id="cb4-76"><a href="#cb4-76" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb4-77"><a href="#cb4-77" aria-hidden="true" tabindex="-1"></a> <span class="co"># Print transcript information</span></span>
|
|
<span id="cb4-78"><a href="#cb4-78" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"Video title: </span><span class="sc">{</span>title<span class="sc">}</span><span class="ss">"</span>)</span>
|
|
<span id="cb4-79"><a href="#cb4-79" aria-hidden="true" tabindex="-1"></a> </span>
|
|
<span id="cb4-80"><a href="#cb4-80" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb4-81"><a href="#cb4-81" aria-hidden="true" tabindex="-1"></a> <span class="co"># Loop through each transcript segment and print text with timestamps</span></span>
|
|
<span id="cb4-82"><a href="#cb4-82" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> segment <span class="kw">in</span> transcript:</span>
|
|
<span id="cb4-83"><a href="#cb4-83" aria-hidden="true" tabindex="-1"></a> start_seconds <span class="op">=</span> segment[<span class="st">'start'</span>]</span>
|
|
<span id="cb4-84"><a href="#cb4-84" aria-hidden="true" tabindex="-1"></a> end_seconds <span class="op">=</span> start_seconds<span class="op">+</span>segment[<span class="st">'duration'</span>]</span>
|
|
<span id="cb4-85"><a href="#cb4-85" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(segment[<span class="st">'text'</span>])</span>
|
|
<span id="cb4-86"><a href="#cb4-86" aria-hidden="true" tabindex="-1"></a> <span class="co">#print(f"[{segment['start']} - {end_seconds}] {segment['text']}")</span></span>
|
|
<span id="cb4-87"><a href="#cb4-87" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb4-88"><a href="#cb4-88" aria-hidden="true" tabindex="-1"></a> <span class="co"># Choose the video stream with the desired resolution</span></span>
|
|
<span id="cb4-89"><a href="#cb4-89" aria-hidden="true" tabindex="-1"></a> video_stream <span class="op">=</span> yt.streams.<span class="bu">filter</span>(progressive<span class="op">=</span><span class="va">True</span>, resolution<span class="op">=</span>resolution).first()</span>
|
|
<span id="cb4-90"><a href="#cb4-90" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb4-91"><a href="#cb4-91" aria-hidden="true" tabindex="-1"></a> <span class="co"># Download the video</span></span>
|
|
<span id="cb4-92"><a href="#cb4-92" aria-hidden="true" tabindex="-1"></a> video_stream.download(filename<span class="op">=</span>output_path)</span>
|
|
<span id="cb4-93"><a href="#cb4-93" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb4-94"><a href="#cb4-94" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"Video downloaded to: </span><span class="sc">{</span>output_path<span class="sc">}</span><span class="ss">"</span>)</span>
|
|
<span id="cb4-95"><a href="#cb4-95" aria-hidden="true" tabindex="-1"></a> <span class="cf">except</span> <span class="pp">Exception</span> <span class="im">as</span> e:</span>
|
|
<span id="cb4-96"><a href="#cb4-96" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"Error downloading video: </span><span class="sc">{</span>e<span class="sc">}</span><span class="ss">"</span>)</span>
|
|
<span id="cb4-97"><a href="#cb4-97" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb4-98"><a href="#cb4-98" aria-hidden="true" tabindex="-1"></a><span class="co"># Example usage</span></span>
|
|
<span id="cb4-99"><a href="#cb4-99" aria-hidden="true" tabindex="-1"></a><span class="co">#video_url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"</span></span>
|
|
<span id="cb4-100"><a href="#cb4-100" aria-hidden="true" tabindex="-1"></a><span class="co">#video_url ='https://www.youtube.com/watch?v=eNhJY-R3Gwg'</span></span>
|
|
<span id="cb4-101"><a href="#cb4-101" aria-hidden="true" tabindex="-1"></a><span class="co">#download_mp4(video_url)</span></span>
|
|
<span id="cb4-102"><a href="#cb4-102" aria-hidden="true" tabindex="-1"></a><span class="co">#convert_mp4_to_mp3('video.mp4')</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|
</div>
|
|
<div id="cell-6" class="cell" data-execution_count="1">
|
|
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> pathlib <span class="im">import</span> Path</span>
|
|
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> pytube <span class="im">import</span> YouTube</span>
|
|
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> moviepy.editor <span class="im">as</span> mp</span>
|
|
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> youtube_transcript_api <span class="im">import</span> YouTubeTranscriptApi</span>
|
|
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> pathvalidate <span class="im">import</span> sanitize_filepath</span>
|
|
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a><span class="co">#print(oppath/'wav')</span></span>
|
|
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> convert_mp4_to_mp3(<span class="bu">file</span>,oppath):</span>
|
|
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="cf">try</span>:</span>
|
|
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> <span class="co"># Load the video file</span></span>
|
|
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a> <span class="co">#video = mp.VideoFileClip(file)</span></span>
|
|
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a> audio<span class="op">=</span>mp.AudioFileClip(<span class="bu">str</span>(<span class="bu">file</span>))</span>
|
|
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a> stem<span class="op">=</span><span class="bu">str</span>(<span class="bu">file</span>.stem)<span class="co">#.replace.rstrip().lstrip()</span></span>
|
|
<span id="cb5-14"><a href="#cb5-14" aria-hidden="true" tabindex="-1"></a> audio_path<span class="op">=</span>sanitize_filepath(oppath<span class="op">/</span><span class="st">'mp3'</span><span class="op">/</span><span class="ss">f'</span><span class="sc">{</span>stem<span class="sc">}</span><span class="ss">.mp3'</span>)</span>
|
|
<span id="cb5-15"><a href="#cb5-15" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">'audio_path'</span>, stem, audio_path)</span>
|
|
<span id="cb5-16"><a href="#cb5-16" aria-hidden="true" tabindex="-1"></a> <span class="co"># Extract the audio from the video and save it as an MP3 file</span></span>
|
|
<span id="cb5-17"><a href="#cb5-17" aria-hidden="true" tabindex="-1"></a> <span class="co">#video.audio.write_audiofile(audio_path, codec='pcm_s16le')</span></span>
|
|
<span id="cb5-18"><a href="#cb5-18" aria-hidden="true" tabindex="-1"></a> audio.write_audiofile(<span class="bu">str</span>(audio_path),write_logfile<span class="op">=</span><span class="va">True</span>,verbose<span class="op">=</span><span class="va">True</span>)<span class="co">#, codec='pcm_s16le')</span></span>
|
|
<span id="cb5-19"><a href="#cb5-19" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb5-20"><a href="#cb5-20" aria-hidden="true" tabindex="-1"></a> <span class="co"># Print a success message</span></span>
|
|
<span id="cb5-21"><a href="#cb5-21" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"Audio extracted and saved as: </span><span class="sc">{</span>audio_path<span class="sc">}</span><span class="ss">"</span>)</span>
|
|
<span id="cb5-22"><a href="#cb5-22" aria-hidden="true" tabindex="-1"></a> <span class="cf">except</span> <span class="pp">Exception</span> <span class="im">as</span> e:</span>
|
|
<span id="cb5-23"><a href="#cb5-23" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"Error converting MP4 to MP3: </span><span class="sc">{</span>e<span class="sc">}</span><span class="ss">"</span>)</span>
|
|
<span id="cb5-24"><a href="#cb5-24" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb5-25"><a href="#cb5-25" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb5-26"><a href="#cb5-26" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> convert_mp4_to_wav(<span class="bu">file</span>,oppath):</span>
|
|
<span id="cb5-27"><a href="#cb5-27" aria-hidden="true" tabindex="-1"></a> <span class="cf">try</span>:</span>
|
|
<span id="cb5-28"><a href="#cb5-28" aria-hidden="true" tabindex="-1"></a> <span class="co"># Load the video file</span></span>
|
|
<span id="cb5-29"><a href="#cb5-29" aria-hidden="true" tabindex="-1"></a> <span class="co">#video = mp.VideoFileClip(file)</span></span>
|
|
<span id="cb5-30"><a href="#cb5-30" aria-hidden="true" tabindex="-1"></a> audio<span class="op">=</span>mp.AudioFileClip(<span class="bu">str</span>(<span class="bu">file</span>))</span>
|
|
<span id="cb5-31"><a href="#cb5-31" aria-hidden="true" tabindex="-1"></a> stem<span class="op">=</span><span class="bu">str</span>(<span class="bu">file</span>.stem)<span class="co">#.replace.rstrip().lstrip()</span></span>
|
|
<span id="cb5-32"><a href="#cb5-32" aria-hidden="true" tabindex="-1"></a> audio_path<span class="op">=</span>sanitize_filepath(oppath<span class="op">/</span><span class="st">'wav'</span><span class="op">/</span><span class="ss">f'</span><span class="sc">{</span>stem<span class="sc">}</span><span class="ss">.wav'</span>)</span>
|
|
<span id="cb5-33"><a href="#cb5-33" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">'audio_path'</span>, stem, audio_path)</span>
|
|
<span id="cb5-34"><a href="#cb5-34" aria-hidden="true" tabindex="-1"></a> <span class="co"># Extract the audio from the video and save it as an MP3 file</span></span>
|
|
<span id="cb5-35"><a href="#cb5-35" aria-hidden="true" tabindex="-1"></a> <span class="co">#video.audio.write_audiofile(audio_path, codec='pcm_s16le')</span></span>
|
|
<span id="cb5-36"><a href="#cb5-36" aria-hidden="true" tabindex="-1"></a> audio.write_audiofile(<span class="bu">str</span>(audio_path),codec<span class="op">=</span><span class="st">'pcm_s16le'</span>,write_logfile<span class="op">=</span><span class="va">True</span>,verbose<span class="op">=</span><span class="va">True</span>)<span class="co">#, codec='pcm_s16le')</span></span>
|
|
<span id="cb5-37"><a href="#cb5-37" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb5-38"><a href="#cb5-38" aria-hidden="true" tabindex="-1"></a> <span class="co"># Print a success message</span></span>
|
|
<span id="cb5-39"><a href="#cb5-39" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"Audio extracted and saved as: </span><span class="sc">{</span>audio_path<span class="sc">}</span><span class="ss">"</span>)</span>
|
|
<span id="cb5-40"><a href="#cb5-40" aria-hidden="true" tabindex="-1"></a> <span class="cf">except</span> <span class="pp">Exception</span> <span class="im">as</span> e:</span>
|
|
<span id="cb5-41"><a href="#cb5-41" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"Error converting MP4 to MP3: </span><span class="sc">{</span>e<span class="sc">}</span><span class="ss">"</span>)</span>
|
|
<span id="cb5-42"><a href="#cb5-42" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb5-43"><a href="#cb5-43" aria-hidden="true" tabindex="-1"></a> </span>
|
|
<span id="cb5-44"><a href="#cb5-44" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> download_mp4(video_url,oppath):</span>
|
|
<span id="cb5-45"><a href="#cb5-45" aria-hidden="true" tabindex="-1"></a> <span class="co"># Get the YouTube object</span></span>
|
|
<span id="cb5-46"><a href="#cb5-46" aria-hidden="true" tabindex="-1"></a> yt <span class="op">=</span> YouTube(video_url)</span>
|
|
<span id="cb5-47"><a href="#cb5-47" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb5-48"><a href="#cb5-48" aria-hidden="true" tabindex="-1"></a> <span class="co"># Get video title and language</span></span>
|
|
<span id="cb5-49"><a href="#cb5-49" aria-hidden="true" tabindex="-1"></a> title <span class="op">=</span> yt.title</span>
|
|
<span id="cb5-50"><a href="#cb5-50" aria-hidden="true" tabindex="-1"></a> <span class="co"># print (title)</span></span>
|
|
<span id="cb5-51"><a href="#cb5-51" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb5-52"><a href="#cb5-52" aria-hidden="true" tabindex="-1"></a> video_stream <span class="op">=</span> yt.streams.<span class="bu">filter</span>(adaptive<span class="op">=</span><span class="va">True</span>).order_by(<span class="st">'abr'</span>)</span>
|
|
<span id="cb5-53"><a href="#cb5-53" aria-hidden="true" tabindex="-1"></a> <span class="co"># print(video_stream)</span></span>
|
|
<span id="cb5-54"><a href="#cb5-54" aria-hidden="true" tabindex="-1"></a> fname<span class="op">=</span>sanitize_filepath(oppath<span class="op">/</span> <span class="ss">f'</span><span class="sc">{</span>title<span class="sc">}</span><span class="ss">.mp4'</span>)</span>
|
|
<span id="cb5-55"><a href="#cb5-55" aria-hidden="true" tabindex="-1"></a> video_stream.last().download(filename<span class="op">=</span>oppath<span class="op">/</span> <span class="ss">f'</span><span class="sc">{</span>title<span class="sc">}</span><span class="ss">.mp4'</span>)</span>
|
|
<span id="cb5-56"><a href="#cb5-56" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> fname </span>
|
|
<span id="cb5-57"><a href="#cb5-57" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb5-58"><a href="#cb5-58" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb5-59"><a href="#cb5-59" aria-hidden="true" tabindex="-1"></a> </span>
|
|
<span id="cb5-60"><a href="#cb5-60" aria-hidden="true" tabindex="-1"></a>filteroption<span class="op">=</span>{</span>
|
|
<span id="cb5-61"><a href="#cb5-61" aria-hidden="true" tabindex="-1"></a> <span class="st">'resolution'</span>:<span class="st">'720p'</span>,</span>
|
|
<span id="cb5-62"><a href="#cb5-62" aria-hidden="true" tabindex="-1"></a> <span class="st">'mp4'</span>:<span class="va">True</span>,</span>
|
|
<span id="cb5-63"><a href="#cb5-63" aria-hidden="true" tabindex="-1"></a> <span class="st">'audio'</span>:<span class="va">True</span>,</span>
|
|
<span id="cb5-64"><a href="#cb5-64" aria-hidden="true" tabindex="-1"></a> <span class="st">'adaptive'</span>:<span class="va">False</span>,</span>
|
|
<span id="cb5-65"><a href="#cb5-65" aria-hidden="true" tabindex="-1"></a>}</span>
|
|
<span id="cb5-66"><a href="#cb5-66" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb5-67"><a href="#cb5-67" aria-hidden="true" tabindex="-1"></a>songs<span class="op">=</span>[</span>
|
|
<span id="cb5-68"><a href="#cb5-68" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://youtu.be/_HleLk2vTHY?si=eJsAXvUEkvhMwvlm'</span>,</span>
|
|
<span id="cb5-69"><a href="#cb5-69" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://youtu.be/-hVB1Lv32dE?si=JAEptI4wSZ7sNHI4'</span>,</span>
|
|
<span id="cb5-70"><a href="#cb5-70" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://youtu.be/RJQks-JWOXE?si=NJUYWipKfunf7-KI'</span>,</span>
|
|
<span id="cb5-71"><a href="#cb5-71" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://youtu.be/YDxS20SsyHg?si=zDLn5_UlAMrU1vhA'</span>,</span>
|
|
<span id="cb5-72"><a href="#cb5-72" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://youtu.be/srY3Y6r1-VY?si=nAPubWmZWiq7hjq1'</span>,</span>
|
|
<span id="cb5-73"><a href="#cb5-73" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://youtu.be/BhUxb0henfw?si=kCR5dIuyxaMT2GD1'</span></span>
|
|
<span id="cb5-74"><a href="#cb5-74" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://youtu.be/EAmqmSkOE68?si=KfamPtR2c4maXpQP'</span>,</span>
|
|
<span id="cb5-75"><a href="#cb5-75" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://youtu.be/RK1V6T5YNpI?si=vuWocaC12es68raT'</span>,</span>
|
|
<span id="cb5-76"><a href="#cb5-76" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://youtu.be/o9sS9y0UWZY?si=N_MVwMewolkYLlhN'</span>,</span>
|
|
<span id="cb5-77"><a href="#cb5-77" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://youtu.be/1ewcPZ8-nX8?si=reKig_CIB7NBy-do'</span>,</span>
|
|
<span id="cb5-78"><a href="#cb5-78" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://youtu.be/EIadKxo1ZBs?si=BcMX01vnYAJkjJYb'</span>,</span>
|
|
<span id="cb5-79"><a href="#cb5-79" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://youtu.be/2zencBD3z68?si=dayfVeYawB-egqxP'</span>,</span>
|
|
<span id="cb5-80"><a href="#cb5-80" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://youtu.be/drFWZ1-NQmE?si=cCoh3Is_nObNvOK5'</span>,</span>
|
|
<span id="cb5-81"><a href="#cb5-81" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://youtu.be/NpIIwVj1ZFU?si=La7ump84_1O61sDT'</span>,</span>
|
|
<span id="cb5-82"><a href="#cb5-82" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://youtu.be/iSF26HsmeRI?si=RAl9v4-a7qAQKD39'</span></span>
|
|
<span id="cb5-83"><a href="#cb5-83" aria-hidden="true" tabindex="-1"></a>]</span>
|
|
<span id="cb5-84"><a href="#cb5-84" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb5-85"><a href="#cb5-85" aria-hidden="true" tabindex="-1"></a>songs_090324<span class="op">=</span>[</span>
|
|
<span id="cb5-86"><a href="#cb5-86" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=cce9JqwPFkY'</span>,</span>
|
|
<span id="cb5-87"><a href="#cb5-87" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=I9Wqm1W5-4s'</span>,</span>
|
|
<span id="cb5-88"><a href="#cb5-88" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=ZW2xa__89p0'</span>,</span>
|
|
<span id="cb5-89"><a href="#cb5-89" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=fFjUyhd0zz4'</span>,</span>
|
|
<span id="cb5-90"><a href="#cb5-90" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=QXzC2eiHBG8'</span>,</span>
|
|
<span id="cb5-91"><a href="#cb5-91" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=L6fmdfQK3es'</span>,</span>
|
|
<span id="cb5-92"><a href="#cb5-92" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=9auVFKjZS7c'</span>,</span>
|
|
<span id="cb5-93"><a href="#cb5-93" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=PJo8F-L0HMs'</span>,</span>
|
|
<span id="cb5-94"><a href="#cb5-94" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=I6nqiTIKdL4'</span>,</span>
|
|
<span id="cb5-95"><a href="#cb5-95" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=TADxdahPBUA'</span>,</span>
|
|
<span id="cb5-96"><a href="#cb5-96" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=lrIKt5uDWZo'</span></span>
|
|
<span id="cb5-97"><a href="#cb5-97" aria-hidden="true" tabindex="-1"></a>]</span>
|
|
<span id="cb5-98"><a href="#cb5-98" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb5-99"><a href="#cb5-99" aria-hidden="true" tabindex="-1"></a>songs_250324<span class="op">=</span>[</span>
|
|
<span id="cb5-100"><a href="#cb5-100" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=UYCg6NBiXMI'</span>,</span>
|
|
<span id="cb5-101"><a href="#cb5-101" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=p9XrgGAafhc'</span>,</span>
|
|
<span id="cb5-102"><a href="#cb5-102" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=x5Oag4hISgU'</span>,</span>
|
|
<span id="cb5-103"><a href="#cb5-103" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=tHyHOUShOg8&t=16s'</span>,</span>
|
|
<span id="cb5-104"><a href="#cb5-104" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=S-u6qdeaPoE&t=229s'</span>,</span>
|
|
<span id="cb5-105"><a href="#cb5-105" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=9_qij2b9-Jg'</span>,</span>
|
|
<span id="cb5-106"><a href="#cb5-106" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=I9Wqm1W5-4s'</span>,</span>
|
|
<span id="cb5-107"><a href="#cb5-107" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=0SVaz8VWE84'</span>,</span>
|
|
<span id="cb5-108"><a href="#cb5-108" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=s7-GTShjcqY'</span></span>
|
|
<span id="cb5-109"><a href="#cb5-109" aria-hidden="true" tabindex="-1"></a>]</span>
|
|
<span id="cb5-110"><a href="#cb5-110" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb5-111"><a href="#cb5-111" aria-hidden="true" tabindex="-1"></a>songs_180424<span class="op">=</span>[</span>
|
|
<span id="cb5-112"><a href="#cb5-112" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=5ng1ypdkRIc'</span>,</span>
|
|
<span id="cb5-113"><a href="#cb5-113" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=TgBww5kmgmg'</span>,</span>
|
|
<span id="cb5-114"><a href="#cb5-114" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=t9wCMPCgeCk'</span>,</span>
|
|
<span id="cb5-115"><a href="#cb5-115" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=rpP2JjFKthU'</span>,</span>
|
|
<span id="cb5-116"><a href="#cb5-116" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=-H4umqfVoHk'</span></span>
|
|
<span id="cb5-117"><a href="#cb5-117" aria-hidden="true" tabindex="-1"></a>]</span>
|
|
<span id="cb5-118"><a href="#cb5-118" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb5-119"><a href="#cb5-119" aria-hidden="true" tabindex="-1"></a>playlist<span class="op">=</span>[</span>
|
|
<span id="cb5-120"><a href="#cb5-120" aria-hidden="true" tabindex="-1"></a> <span class="st">'https://www.youtube.com/watch?v=rknrt3tykH8&list=PL89GLpYyckZgglcqylKKyEdoUZ17drYAf&index=2'</span></span>
|
|
<span id="cb5-121"><a href="#cb5-121" aria-hidden="true" tabindex="-1"></a> </span>
|
|
<span id="cb5-122"><a href="#cb5-122" aria-hidden="true" tabindex="-1"></a>]</span>
|
|
<span id="cb5-123"><a href="#cb5-123" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> download_songlist(songs):</span>
|
|
<span id="cb5-124"><a href="#cb5-124" aria-hidden="true" tabindex="-1"></a> oppath<span class="op">=</span>Path(<span class="st">'/home/ys/Music/Miri/5_playlist'</span>)</span>
|
|
<span id="cb5-125"><a href="#cb5-125" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> song <span class="kw">in</span> songs:</span>
|
|
<span id="cb5-126"><a href="#cb5-126" aria-hidden="true" tabindex="-1"></a> <span class="cf">try</span>:</span>
|
|
<span id="cb5-127"><a href="#cb5-127" aria-hidden="true" tabindex="-1"></a> <span class="bu">file</span><span class="op">=</span>download_mp4(song,oppath)</span>
|
|
<span id="cb5-128"><a href="#cb5-128" aria-hidden="true" tabindex="-1"></a> convert_mp4_to_mp3(<span class="bu">file</span>,oppath)</span>
|
|
<span id="cb5-129"><a href="#cb5-129" aria-hidden="true" tabindex="-1"></a> <span class="cf">except</span> <span class="pp">Exception</span> <span class="im">as</span> e:</span>
|
|
<span id="cb5-130"><a href="#cb5-130" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"Error converting MP4 to MP3: </span><span class="sc">{</span>e<span class="sc">}</span><span class="ss">"</span>)</span>
|
|
<span id="cb5-131"><a href="#cb5-131" aria-hidden="true" tabindex="-1"></a> <span class="cf">continue</span></span>
|
|
<span id="cb5-132"><a href="#cb5-132" aria-hidden="true" tabindex="-1"></a></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|
</div>
|
|
<div id="cell-7" class="cell" data-execution_count="2">
|
|
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>download_songlist(songs_250324)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|
<div class="cell-output cell-output-stdout">
|
|
<pre><code>audio_path Shawn Mendes - Stitches (Lyrics) /home/ys/Music/Miri/5_playlist/mp3/Shawn Mendes - Stitches (Lyrics).mp3
|
|
MoviePy - Writing audio in /home/ys/Music/Miri/5_playlist/mp3/Shawn Mendes - Stitches (Lyrics).mp3</code></pre>
|
|
</div>
|
|
<div class="cell-output cell-output-stderr">
|
|
<pre><code> </code></pre>
|
|
</div>
|
|
<div class="cell-output cell-output-stdout">
|
|
<pre><code>MoviePy - Done.
|
|
Audio extracted and saved as: /home/ys/Music/Miri/5_playlist/mp3/Shawn Mendes - Stitches (Lyrics).mp3
|
|
audio_path DARKSIDE /home/ys/Music/Miri/5_playlist/mp3/DARKSIDE.mp3
|
|
MoviePy - Writing audio in /home/ys/Music/Miri/5_playlist/mp3/DARKSIDE.mp3</code></pre>
|
|
</div>
|
|
<div class="cell-output cell-output-stderr">
|
|
<pre><code> </code></pre>
|
|
</div>
|
|
<div class="cell-output cell-output-stdout">
|
|
<pre><code>MoviePy - Done.
|
|
Audio extracted and saved as: /home/ys/Music/Miri/5_playlist/mp3/DARKSIDE.mp3
|
|
audio_path Rasputin /home/ys/Music/Miri/5_playlist/mp3/Rasputin.mp3
|
|
MoviePy - Writing audio in /home/ys/Music/Miri/5_playlist/mp3/Rasputin.mp3</code></pre>
|
|
</div>
|
|
<div class="cell-output cell-output-stderr">
|
|
<pre><code> </code></pre>
|
|
</div>
|
|
<div class="cell-output cell-output-stdout">
|
|
<pre><code>MoviePy - Done.
|
|
Audio extracted and saved as: /home/ys/Music/Miri/5_playlist/mp3/Rasputin.mp3
|
|
audio_path Boney M - Daddy Cool (Original video) 1976 /home/ys/Music/Miri/5_playlist/mp3/Boney M - Daddy Cool (Original video) 1976.mp3
|
|
MoviePy - Writing audio in /home/ys/Music/Miri/5_playlist/mp3/Boney M - Daddy Cool (Original video) 1976.mp3</code></pre>
|
|
</div>
|
|
<div class="cell-output cell-output-stderr">
|
|
<pre><code> </code></pre>
|
|
</div>
|
|
<div class="cell-output cell-output-stdout">
|
|
<pre><code>MoviePy - Done.
|
|
Audio extracted and saved as: /home/ys/Music/Miri/5_playlist/mp3/Boney M - Daddy Cool (Original video) 1976.mp3
|
|
audio_path Boney M. - Sunny (Official Video) [HD 1080p] /home/ys/Music/Miri/5_playlist/mp3/Boney M. - Sunny (Official Video) [HD 1080p].mp3
|
|
MoviePy - Writing audio in /home/ys/Music/Miri/5_playlist/mp3/Boney M. - Sunny (Official Video) [HD 1080p].mp3</code></pre>
|
|
</div>
|
|
<div class="cell-output cell-output-stderr">
|
|
<pre><code> </code></pre>
|
|
</div>
|
|
<div class="cell-output cell-output-stdout">
|
|
<pre><code>MoviePy - Done.
|
|
Audio extracted and saved as: /home/ys/Music/Miri/5_playlist/mp3/Boney M. - Sunny (Official Video) [HD 1080p].mp3
|
|
Error converting MP4 to MP3: MoviePy error: the file /home/ys/Music/Miri/5_playlist/Charles & Eddie - Would I lie to you baby (Lyrics) BUGG Lyrics.mp4 could not be found!
|
|
Please check that you entered the correct path.
|
|
audio_path You /home/ys/Music/Miri/5_playlist/mp3/You.mp3
|
|
MoviePy - Writing audio in /home/ys/Music/Miri/5_playlist/mp3/You.mp3</code></pre>
|
|
</div>
|
|
<div class="cell-output cell-output-stderr">
|
|
<pre><code> </code></pre>
|
|
</div>
|
|
<div class="cell-output cell-output-stdout">
|
|
<pre><code>MoviePy - Done.
|
|
Audio extracted and saved as: /home/ys/Music/Miri/5_playlist/mp3/You.mp3
|
|
audio_path People You Know (Official Lyrics) /home/ys/Music/Miri/5_playlist/mp3/People You Know (Official Lyrics).mp3
|
|
MoviePy - Writing audio in /home/ys/Music/Miri/5_playlist/mp3/People You Know (Official Lyrics).mp3</code></pre>
|
|
</div>
|
|
<div class="cell-output cell-output-stderr">
|
|
<pre><code> </code></pre>
|
|
</div>
|
|
<div class="cell-output cell-output-stdout">
|
|
<pre><code>MoviePy - Done.
|
|
Audio extracted and saved as: /home/ys/Music/Miri/5_playlist/mp3/People You Know (Official Lyrics).mp3
|
|
audio_path DARKSIDE /home/ys/Music/Miri/5_playlist/mp3/DARKSIDE.mp3
|
|
MoviePy - Writing audio in /home/ys/Music/Miri/5_playlist/mp3/DARKSIDE.mp3</code></pre>
|
|
</div>
|
|
<div class="cell-output cell-output-stderr">
|
|
<pre><code> </code></pre>
|
|
</div>
|
|
<div class="cell-output cell-output-stdout">
|
|
<pre><code>MoviePy - Done.
|
|
Audio extracted and saved as: /home/ys/Music/Miri/5_playlist/mp3/DARKSIDE.mp3</code></pre>
|
|
</div>
|
|
<div class="cell-output cell-output-stderr">
|
|
<pre><code></code></pre>
|
|
</div>
|
|
</div>
|
|
<p>https://www.fatpick.com/blog?in=music-theory</p>
|
|
<div class="quarto-figure quarto-figure-center">
|
|
<figure class="figure">
|
|
<p><img src="audioconversion_files/figure-html/cell-8-1-image.png" class="img-fluid figure-img"></p>
|
|
<figcaption>image.png</figcaption>
|
|
</figure>
|
|
</div>
|
|
<div id="cell-9" class="cell" data-execution_count="12">
|
|
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a><span class="co">#for i in range(40,88,1):</span></span>
|
|
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a><span class="co"># guitar.play_note(i,1,0.2)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|
<div class="cell-output cell-output-stdout">
|
|
<pre><code>Using preset Guitar Nylon X for guitar nylon</code></pre>
|
|
</div>
|
|
</div>
|
|
<div id="cell-10" class="cell" data-execution_count="3">
|
|
<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a>NOTES_FLAT <span class="op">=</span> [<span class="st">'C'</span>, <span class="st">'Db'</span>, <span class="st">'D'</span>, <span class="st">'Eb'</span>, <span class="st">'E'</span>, <span class="st">'F'</span>, <span class="st">'Gb'</span>, <span class="st">'G'</span>, <span class="st">'Ab'</span>, <span class="st">'A'</span>, <span class="st">'Bb'</span>, <span class="st">'B'</span>]</span>
|
|
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a>NOTES_SHARP <span class="op">=</span> [<span class="st">'C'</span>, <span class="st">'C#'</span>, <span class="st">'D'</span>, <span class="st">'D#'</span>, <span class="st">'E'</span>, <span class="st">'F'</span>, <span class="st">'F#'</span>, <span class="st">'G'</span>, <span class="st">'G#'</span>, <span class="st">'A'</span>, <span class="st">'A#'</span>, <span class="st">'B'</span>]</span>
|
|
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a>NOTES_FLAT<span class="op">=</span>[<span class="bu">str</span>.lower(_) <span class="cf">for</span> _ <span class="kw">in</span> NOTES_FLAT]</span>
|
|
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a>NOTES_SHARP<span class="op">=</span>[<span class="bu">str</span>.lower(_) <span class="cf">for</span> _ <span class="kw">in</span> NOTES_SHARP]</span>
|
|
<span id="cb27-5"><a href="#cb27-5" aria-hidden="true" tabindex="-1"></a><span class="co">#def NoteToMidi_guitar(string, fret):</span></span>
|
|
<span id="cb27-6"><a href="#cb27-6" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pandas <span class="im">as</span> pd</span>
|
|
<span id="cb27-7"><a href="#cb27-7" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> numpy <span class="im">as</span> np</span>
|
|
<span id="cb27-8"><a href="#cb27-8" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> itertools <span class="im">import</span> product</span>
|
|
<span id="cb27-9"><a href="#cb27-9" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> NoteToMidi(KeyOctave): </span>
|
|
<span id="cb27-10"><a href="#cb27-10" aria-hidden="true" tabindex="-1"></a> <span class="co"># KeyOctave is formatted like 'C#3'</span></span>
|
|
<span id="cb27-11"><a href="#cb27-11" aria-hidden="true" tabindex="-1"></a> key <span class="op">=</span> KeyOctave[:<span class="op">-</span><span class="dv">1</span>] <span class="co"># eg C, Db</span></span>
|
|
<span id="cb27-12"><a href="#cb27-12" aria-hidden="true" tabindex="-1"></a> octave <span class="op">=</span> KeyOctave[<span class="op">-</span><span class="dv">1</span>] <span class="co"># eg 3, 4</span></span>
|
|
<span id="cb27-13"><a href="#cb27-13" aria-hidden="true" tabindex="-1"></a> answer <span class="op">=</span> <span class="op">-</span><span class="dv">1</span></span>
|
|
<span id="cb27-14"><a href="#cb27-14" aria-hidden="true" tabindex="-1"></a> </span>
|
|
<span id="cb27-15"><a href="#cb27-15" aria-hidden="true" tabindex="-1"></a> <span class="cf">try</span>:</span>
|
|
<span id="cb27-16"><a href="#cb27-16" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> <span class="st">'b'</span> <span class="kw">in</span> key:</span>
|
|
<span id="cb27-17"><a href="#cb27-17" aria-hidden="true" tabindex="-1"></a> pos <span class="op">=</span> NOTES_FLAT.index(key)</span>
|
|
<span id="cb27-18"><a href="#cb27-18" aria-hidden="true" tabindex="-1"></a> <span class="cf">else</span>:</span>
|
|
<span id="cb27-19"><a href="#cb27-19" aria-hidden="true" tabindex="-1"></a> pos <span class="op">=</span> NOTES_SHARP.index(key)</span>
|
|
<span id="cb27-20"><a href="#cb27-20" aria-hidden="true" tabindex="-1"></a> <span class="cf">except</span>:</span>
|
|
<span id="cb27-21"><a href="#cb27-21" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">'The key is not valid'</span>, key)</span>
|
|
<span id="cb27-22"><a href="#cb27-22" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> answer</span>
|
|
<span id="cb27-23"><a href="#cb27-23" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb27-24"><a href="#cb27-24" aria-hidden="true" tabindex="-1"></a> answer <span class="op">+=</span> pos <span class="op">+</span> <span class="dv">12</span> <span class="op">*</span> (<span class="bu">int</span>(octave) <span class="op">+</span> <span class="dv">1</span>) <span class="op">+</span> <span class="dv">1</span></span>
|
|
<span id="cb27-25"><a href="#cb27-25" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> answer</span>
|
|
<span id="cb27-26"><a href="#cb27-26" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb27-27"><a href="#cb27-27" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> guitarToMidi(string,fret):</span>
|
|
<span id="cb27-28"><a href="#cb27-28" aria-hidden="true" tabindex="-1"></a> openstr<span class="op">=</span>{}</span>
|
|
<span id="cb27-29"><a href="#cb27-29" aria-hidden="true" tabindex="-1"></a> openstr[<span class="dv">6</span>]<span class="op">=</span><span class="dv">40</span></span>
|
|
<span id="cb27-30"><a href="#cb27-30" aria-hidden="true" tabindex="-1"></a> openstr[<span class="dv">5</span>]<span class="op">=</span><span class="dv">45</span></span>
|
|
<span id="cb27-31"><a href="#cb27-31" aria-hidden="true" tabindex="-1"></a> openstr[<span class="dv">4</span>]<span class="op">=</span><span class="dv">50</span></span>
|
|
<span id="cb27-32"><a href="#cb27-32" aria-hidden="true" tabindex="-1"></a> openstr[<span class="dv">3</span>]<span class="op">=</span><span class="dv">55</span></span>
|
|
<span id="cb27-33"><a href="#cb27-33" aria-hidden="true" tabindex="-1"></a> openstr[<span class="dv">2</span>]<span class="op">=</span><span class="dv">59</span></span>
|
|
<span id="cb27-34"><a href="#cb27-34" aria-hidden="true" tabindex="-1"></a> openstr[<span class="dv">1</span>]<span class="op">=</span><span class="dv">64</span></span>
|
|
<span id="cb27-35"><a href="#cb27-35" aria-hidden="true" tabindex="-1"></a> midi<span class="op">=</span>openstr[string]<span class="op">+</span>fret</span>
|
|
<span id="cb27-36"><a href="#cb27-36" aria-hidden="true" tabindex="-1"></a> notes<span class="op">=</span>miditoGuitarNote_sharps.loc[midi,<span class="dv">0</span>]</span>
|
|
<span id="cb27-37"><a href="#cb27-37" aria-hidden="true" tabindex="-1"></a> notef<span class="op">=</span>miditoGuitarNote_flats.loc[midi,<span class="dv">0</span>]</span>
|
|
<span id="cb27-38"><a href="#cb27-38" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> midi,notes,notef</span>
|
|
<span id="cb27-39"><a href="#cb27-39" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb27-40"><a href="#cb27-40" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb27-41"><a href="#cb27-41" aria-hidden="true" tabindex="-1"></a>miditoGuitarNote_sharps<span class="op">=</span>(pd.concat([pd.Series({NoteToMidi(<span class="ss">f'</span><span class="sc">{</span>k<span class="sc">}{</span>o<span class="sc">}</span><span class="ss">'</span>):<span class="ss">f'</span><span class="sc">{</span>k<span class="sc">}{</span>o<span class="sc">}</span><span class="ss">'</span> <span class="cf">for</span> o,k <span class="kw">in</span> product(<span class="bu">range</span>(<span class="dv">0</span>,<span class="dv">7</span>,<span class="dv">1</span>),NOTES_SHARP)}),</span>
|
|
<span id="cb27-42"><a href="#cb27-42" aria-hidden="true" tabindex="-1"></a> ], axis<span class="op">=</span><span class="dv">1</span></span>
|
|
<span id="cb27-43"><a href="#cb27-43" aria-hidden="true" tabindex="-1"></a> )</span>
|
|
<span id="cb27-44"><a href="#cb27-44" aria-hidden="true" tabindex="-1"></a> .assign(E<span class="op">=</span><span class="kw">lambda</span> df: np.where((df.index<span class="op">>=</span><span class="dv">40</span>) <span class="op">&</span> (df.index<span class="op"><</span><span class="dv">59</span>),df[<span class="dv">0</span>],<span class="dv">0</span>),</span>
|
|
<span id="cb27-45"><a href="#cb27-45" aria-hidden="true" tabindex="-1"></a> A<span class="op">=</span><span class="kw">lambda</span> df: np.where((df.index<span class="op">>=</span><span class="dv">45</span>) <span class="op">&</span> (df.index<span class="op"><</span><span class="dv">64</span>),df[<span class="dv">0</span>],<span class="dv">0</span>),</span>
|
|
<span id="cb27-46"><a href="#cb27-46" aria-hidden="true" tabindex="-1"></a> D<span class="op">=</span><span class="kw">lambda</span> df: np.where((df.index<span class="op">>=</span><span class="dv">50</span>) <span class="op">&</span> (df.index<span class="op"><</span><span class="dv">69</span>),df[<span class="dv">0</span>],<span class="dv">0</span>),</span>
|
|
<span id="cb27-47"><a href="#cb27-47" aria-hidden="true" tabindex="-1"></a> G<span class="op">=</span><span class="kw">lambda</span> df: np.where((df.index<span class="op">>=</span><span class="dv">55</span>) <span class="op">&</span> (df.index<span class="op"><</span><span class="dv">74</span>),df[<span class="dv">0</span>],<span class="dv">0</span>),</span>
|
|
<span id="cb27-48"><a href="#cb27-48" aria-hidden="true" tabindex="-1"></a> B<span class="op">=</span><span class="kw">lambda</span> df: np.where((df.index<span class="op">>=</span><span class="dv">59</span>) <span class="op">&</span> (df.index<span class="op"><</span><span class="dv">78</span>),df[<span class="dv">0</span>],<span class="dv">0</span>),</span>
|
|
<span id="cb27-49"><a href="#cb27-49" aria-hidden="true" tabindex="-1"></a> e<span class="op">=</span><span class="kw">lambda</span> df: np.where((df.index<span class="op">>=</span><span class="dv">64</span>) <span class="op">&</span> (df.index<span class="op"><</span><span class="dv">83</span>),df[<span class="dv">0</span>],<span class="dv">0</span>),</span>
|
|
<span id="cb27-50"><a href="#cb27-50" aria-hidden="true" tabindex="-1"></a> )</span>
|
|
<span id="cb27-51"><a href="#cb27-51" aria-hidden="true" tabindex="-1"></a>)</span>
|
|
<span id="cb27-52"><a href="#cb27-52" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb27-53"><a href="#cb27-53" aria-hidden="true" tabindex="-1"></a>miditoGuitarNote_flats<span class="op">=</span>(pd.concat([pd.Series({NoteToMidi(<span class="ss">f'</span><span class="sc">{</span>k<span class="sc">}{</span>o<span class="sc">}</span><span class="ss">'</span>):<span class="ss">f'</span><span class="sc">{</span>k<span class="sc">}{</span>o<span class="sc">}</span><span class="ss">'</span> <span class="cf">for</span> o,k <span class="kw">in</span> product(<span class="bu">range</span>(<span class="dv">0</span>,<span class="dv">7</span>,<span class="dv">1</span>),NOTES_FLAT)}),</span>
|
|
<span id="cb27-54"><a href="#cb27-54" aria-hidden="true" tabindex="-1"></a> ], axis<span class="op">=</span><span class="dv">1</span></span>
|
|
<span id="cb27-55"><a href="#cb27-55" aria-hidden="true" tabindex="-1"></a> )</span>
|
|
<span id="cb27-56"><a href="#cb27-56" aria-hidden="true" tabindex="-1"></a> .assign(E<span class="op">=</span><span class="kw">lambda</span> df: np.where((df.index<span class="op">>=</span><span class="dv">40</span>) <span class="op">&</span> (df.index<span class="op"><</span><span class="dv">59</span>),df[<span class="dv">0</span>],<span class="dv">0</span>),</span>
|
|
<span id="cb27-57"><a href="#cb27-57" aria-hidden="true" tabindex="-1"></a> A<span class="op">=</span><span class="kw">lambda</span> df: np.where((df.index<span class="op">>=</span><span class="dv">45</span>) <span class="op">&</span> (df.index<span class="op"><</span><span class="dv">64</span>),df[<span class="dv">0</span>],<span class="dv">0</span>),</span>
|
|
<span id="cb27-58"><a href="#cb27-58" aria-hidden="true" tabindex="-1"></a> D<span class="op">=</span><span class="kw">lambda</span> df: np.where((df.index<span class="op">>=</span><span class="dv">50</span>) <span class="op">&</span> (df.index<span class="op"><</span><span class="dv">69</span>),df[<span class="dv">0</span>],<span class="dv">0</span>),</span>
|
|
<span id="cb27-59"><a href="#cb27-59" aria-hidden="true" tabindex="-1"></a> G<span class="op">=</span><span class="kw">lambda</span> df: np.where((df.index<span class="op">>=</span><span class="dv">55</span>) <span class="op">&</span> (df.index<span class="op"><</span><span class="dv">74</span>),df[<span class="dv">0</span>],<span class="dv">0</span>),</span>
|
|
<span id="cb27-60"><a href="#cb27-60" aria-hidden="true" tabindex="-1"></a> B<span class="op">=</span><span class="kw">lambda</span> df: np.where((df.index<span class="op">>=</span><span class="dv">59</span>) <span class="op">&</span> (df.index<span class="op"><</span><span class="dv">78</span>),df[<span class="dv">0</span>],<span class="dv">0</span>),</span>
|
|
<span id="cb27-61"><a href="#cb27-61" aria-hidden="true" tabindex="-1"></a> e<span class="op">=</span><span class="kw">lambda</span> df: np.where((df.index<span class="op">>=</span><span class="dv">64</span>) <span class="op">&</span> (df.index<span class="op"><</span><span class="dv">83</span>),df[<span class="dv">0</span>],<span class="dv">0</span>),</span>
|
|
<span id="cb27-62"><a href="#cb27-62" aria-hidden="true" tabindex="-1"></a> )</span>
|
|
<span id="cb27-63"><a href="#cb27-63" aria-hidden="true" tabindex="-1"></a>)</span>
|
|
<span id="cb27-64"><a href="#cb27-64" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb27-65"><a href="#cb27-65" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb27-66"><a href="#cb27-66" aria-hidden="true" tabindex="-1"></a></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|
</div>
|
|
<div id="cell-11" class="cell" data-execution_count="48">
|
|
<div class="sourceCode cell-code" id="cb28"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a><span class="co">#guitarToMidi(6,2)[0]</span></span>
|
|
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb28-5"><a href="#cb28-5" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> scamp <span class="im">import</span> <span class="op">*</span></span>
|
|
<span id="cb28-6"><a href="#cb28-6" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb28-7"><a href="#cb28-7" aria-hidden="true" tabindex="-1"></a>s<span class="op">=</span> Session() <span class="co"># (temp=100,)</span></span>
|
|
<span id="cb28-8"><a href="#cb28-8" aria-hidden="true" tabindex="-1"></a><span class="co">#s.print_default_soundfont_presets()</span></span>
|
|
<span id="cb28-9"><a href="#cb28-9" aria-hidden="true" tabindex="-1"></a>guitar<span class="op">=</span>s.new_part(<span class="st">"guitar nylon"</span>)</span>
|
|
<span id="cb28-10"><a href="#cb28-10" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb28-11"><a href="#cb28-11" aria-hidden="true" tabindex="-1"></a><span class="co">#https://www.fatpick.com/blog/note-table</span></span>
|
|
<span id="cb28-12"><a href="#cb28-12" aria-hidden="true" tabindex="-1"></a><span class="co">#http://scamp.marcevanstein.com/narrative/tutorial_videos.html</span></span>
|
|
<span id="cb28-13"><a href="#cb28-13" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb28-14"><a href="#cb28-14" aria-hidden="true" tabindex="-1"></a><span class="co">#</span></span>
|
|
<span id="cb28-15"><a href="#cb28-15" aria-hidden="true" tabindex="-1"></a>count<span class="op">=</span><span class="dv">0</span></span>
|
|
<span id="cb28-16"><a href="#cb28-16" aria-hidden="true" tabindex="-1"></a>vol<span class="op">=</span><span class="dv">0</span></span>
|
|
<span id="cb28-17"><a href="#cb28-17" aria-hidden="true" tabindex="-1"></a><span class="co">#guitar.play_note(guitarToMidi(6,0)[0],1,5,blocking=False)</span></span>
|
|
<span id="cb28-18"><a href="#cb28-18" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> string <span class="kw">in</span> <span class="bu">reversed</span>(<span class="bu">range</span>(<span class="dv">1</span>,<span class="dv">7</span>,<span class="dv">1</span>)):</span>
|
|
<span id="cb28-19"><a href="#cb28-19" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> fret <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">0</span>,<span class="dv">5</span>):</span>
|
|
<span id="cb28-20"><a href="#cb28-20" aria-hidden="true" tabindex="-1"></a> </span>
|
|
<span id="cb28-21"><a href="#cb28-21" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (count<span class="op">%</span><span class="dv">3</span><span class="op">==</span><span class="dv">0</span>):</span>
|
|
<span id="cb28-22"><a href="#cb28-22" aria-hidden="true" tabindex="-1"></a> vol<span class="op">=</span><span class="dv">1</span></span>
|
|
<span id="cb28-23"><a href="#cb28-23" aria-hidden="true" tabindex="-1"></a> <span class="cf">else</span>:</span>
|
|
<span id="cb28-24"><a href="#cb28-24" aria-hidden="true" tabindex="-1"></a> vol<span class="op">=</span><span class="fl">0.7</span></span>
|
|
<span id="cb28-25"><a href="#cb28-25" aria-hidden="true" tabindex="-1"></a> count<span class="op">+=</span><span class="dv">1</span></span>
|
|
<span id="cb28-26"><a href="#cb28-26" aria-hidden="true" tabindex="-1"></a> </span>
|
|
<span id="cb28-27"><a href="#cb28-27" aria-hidden="true" tabindex="-1"></a> guitar.play_note(guitarToMidi(string,fret)[<span class="dv">0</span>],vol,<span class="fl">0.1</span>)</span>
|
|
<span id="cb28-28"><a href="#cb28-28" aria-hidden="true" tabindex="-1"></a><span class="co">#guitar.play_note(40,1,2)</span></span>
|
|
<span id="cb28-29"><a href="#cb28-29" aria-hidden="true" tabindex="-1"></a><span class="co">#guitar.play_note(83,1,2)</span></span>
|
|
<span id="cb28-30"><a href="#cb28-30" aria-hidden="true" tabindex="-1"></a><span class="co">#guitar.play_chord([70,73,83],1,2)</span></span>
|
|
<span id="cb28-31"><a href="#cb28-31" aria-hidden="true" tabindex="-1"></a><span class="co">#guitar.play_note(52,1,5)</span></span>
|
|
<span id="cb28-32"><a href="#cb28-32" aria-hidden="true" tabindex="-1"></a>s.beat_length</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|
<div class="cell-output cell-output-stdout">
|
|
<pre><code>Using preset Guitar Nylon X for guitar nylon</code></pre>
|
|
</div>
|
|
<div class="cell-output cell-output-display" data-execution_count="48">
|
|
<pre><code>1.0</code></pre>
|
|
</div>
|
|
</div>
|
|
<div id="cell-12" class="cell" data-execution_count="34">
|
|
<div class="sourceCode cell-code" id="cb31"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> abjad</span>
|
|
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a>string <span class="op">=</span> <span class="st">"c'16 f' g' a' d' g' a' b' e' a' b' c'' f' b' c'' d''16"</span></span>
|
|
<span id="cb31-3"><a href="#cb31-3" aria-hidden="true" tabindex="-1"></a>voice_1 <span class="op">=</span> abjad.Voice(string, name<span class="op">=</span><span class="st">"Voice_1"</span>)</span>
|
|
<span id="cb31-4"><a href="#cb31-4" aria-hidden="true" tabindex="-1"></a>staff_1 <span class="op">=</span> abjad.Staff([voice_1], name<span class="op">=</span><span class="st">"Staff_1"</span>)</span>
|
|
<span id="cb31-5"><a href="#cb31-5" aria-hidden="true" tabindex="-1"></a>abjad.show(staff_1)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|
</div>
|
|
|
|
</main>
|
|
<!-- /main column -->
|
|
<script id="quarto-html-after-body" type="application/javascript">
|
|
window.document.addEventListener("DOMContentLoaded", function (event) {
|
|
const toggleBodyColorMode = (bsSheetEl) => {
|
|
const mode = bsSheetEl.getAttribute("data-mode");
|
|
const bodyEl = window.document.querySelector("body");
|
|
if (mode === "dark") {
|
|
bodyEl.classList.add("quarto-dark");
|
|
bodyEl.classList.remove("quarto-light");
|
|
} else {
|
|
bodyEl.classList.add("quarto-light");
|
|
bodyEl.classList.remove("quarto-dark");
|
|
}
|
|
}
|
|
const toggleBodyColorPrimary = () => {
|
|
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
|
|
if (bsSheetEl) {
|
|
toggleBodyColorMode(bsSheetEl);
|
|
}
|
|
}
|
|
toggleBodyColorPrimary();
|
|
const icon = "";
|
|
const anchorJS = new window.AnchorJS();
|
|
anchorJS.options = {
|
|
placement: 'right',
|
|
icon: icon
|
|
};
|
|
anchorJS.add('.anchored');
|
|
const isCodeAnnotation = (el) => {
|
|
for (const clz of el.classList) {
|
|
if (clz.startsWith('code-annotation-')) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
const onCopySuccess = function(e) {
|
|
// button target
|
|
const button = e.trigger;
|
|
// don't keep focus
|
|
button.blur();
|
|
// flash "checked"
|
|
button.classList.add('code-copy-button-checked');
|
|
var currentTitle = button.getAttribute("title");
|
|
button.setAttribute("title", "Copied!");
|
|
let tooltip;
|
|
if (window.bootstrap) {
|
|
button.setAttribute("data-bs-toggle", "tooltip");
|
|
button.setAttribute("data-bs-placement", "left");
|
|
button.setAttribute("data-bs-title", "Copied!");
|
|
tooltip = new bootstrap.Tooltip(button,
|
|
{ trigger: "manual",
|
|
customClass: "code-copy-button-tooltip",
|
|
offset: [0, -8]});
|
|
tooltip.show();
|
|
}
|
|
setTimeout(function() {
|
|
if (tooltip) {
|
|
tooltip.hide();
|
|
button.removeAttribute("data-bs-title");
|
|
button.removeAttribute("data-bs-toggle");
|
|
button.removeAttribute("data-bs-placement");
|
|
}
|
|
button.setAttribute("title", currentTitle);
|
|
button.classList.remove('code-copy-button-checked');
|
|
}, 1000);
|
|
// clear code selection
|
|
e.clearSelection();
|
|
}
|
|
const getTextToCopy = function(trigger) {
|
|
const codeEl = trigger.previousElementSibling.cloneNode(true);
|
|
for (const childEl of codeEl.children) {
|
|
if (isCodeAnnotation(childEl)) {
|
|
childEl.remove();
|
|
}
|
|
}
|
|
return codeEl.innerText;
|
|
}
|
|
const clipboard = new window.ClipboardJS('.code-copy-button:not([data-in-quarto-modal])', {
|
|
text: getTextToCopy
|
|
});
|
|
clipboard.on('success', onCopySuccess);
|
|
if (window.document.getElementById('quarto-embedded-source-code-modal')) {
|
|
// For code content inside modals, clipBoardJS needs to be initialized with a container option
|
|
// TODO: Check when it could be a function (https://github.com/zenorocha/clipboard.js/issues/860)
|
|
const clipboardModal = new window.ClipboardJS('.code-copy-button[data-in-quarto-modal]', {
|
|
text: getTextToCopy,
|
|
container: window.document.getElementById('quarto-embedded-source-code-modal')
|
|
});
|
|
clipboardModal.on('success', onCopySuccess);
|
|
}
|
|
var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//);
|
|
var mailtoRegex = new RegExp(/^mailto:/);
|
|
var filterRegex = new RegExp('/' + window.location.host + '/');
|
|
var isInternal = (href) => {
|
|
return filterRegex.test(href) || localhostRegex.test(href) || mailtoRegex.test(href);
|
|
}
|
|
// Inspect non-navigation links and adorn them if external
|
|
var links = window.document.querySelectorAll('a[href]:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external):not([aria-hidden]):not(.dropdown-item):not(.quarto-navigation-tool):not(.about-link)');
|
|
for (var i=0; i<links.length; i++) {
|
|
const link = links[i];
|
|
if (!isInternal(link.href)) {
|
|
// undo the damage that might have been done by quarto-nav.js in the case of
|
|
// links that we want to consider external
|
|
if (link.dataset.originalHref !== undefined) {
|
|
link.href = link.dataset.originalHref;
|
|
}
|
|
}
|
|
}
|
|
function tippyHover(el, contentFn, onTriggerFn, onUntriggerFn) {
|
|
const config = {
|
|
allowHTML: true,
|
|
maxWidth: 500,
|
|
delay: 100,
|
|
arrow: false,
|
|
appendTo: function(el) {
|
|
return el.parentElement;
|
|
},
|
|
interactive: true,
|
|
interactiveBorder: 10,
|
|
theme: 'quarto',
|
|
placement: 'bottom-start',
|
|
};
|
|
if (contentFn) {
|
|
config.content = contentFn;
|
|
}
|
|
if (onTriggerFn) {
|
|
config.onTrigger = onTriggerFn;
|
|
}
|
|
if (onUntriggerFn) {
|
|
config.onUntrigger = onUntriggerFn;
|
|
}
|
|
window.tippy(el, config);
|
|
}
|
|
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
|
|
for (var i=0; i<noterefs.length; i++) {
|
|
const ref = noterefs[i];
|
|
tippyHover(ref, function() {
|
|
// use id or data attribute instead here
|
|
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
|
|
try { href = new URL(href).hash; } catch {}
|
|
const id = href.replace(/^#\/?/, "");
|
|
const note = window.document.getElementById(id);
|
|
if (note) {
|
|
return note.innerHTML;
|
|
} else {
|
|
return "";
|
|
}
|
|
});
|
|
}
|
|
const xrefs = window.document.querySelectorAll('a.quarto-xref');
|
|
const processXRef = (id, note) => {
|
|
// Strip column container classes
|
|
const stripColumnClz = (el) => {
|
|
el.classList.remove("page-full", "page-columns");
|
|
if (el.children) {
|
|
for (const child of el.children) {
|
|
stripColumnClz(child);
|
|
}
|
|
}
|
|
}
|
|
stripColumnClz(note)
|
|
if (id === null || id.startsWith('sec-')) {
|
|
// Special case sections, only their first couple elements
|
|
const container = document.createElement("div");
|
|
if (note.children && note.children.length > 2) {
|
|
container.appendChild(note.children[0].cloneNode(true));
|
|
for (let i = 1; i < note.children.length; i++) {
|
|
const child = note.children[i];
|
|
if (child.tagName === "P" && child.innerText === "") {
|
|
continue;
|
|
} else {
|
|
container.appendChild(child.cloneNode(true));
|
|
break;
|
|
}
|
|
}
|
|
if (window.Quarto?.typesetMath) {
|
|
window.Quarto.typesetMath(container);
|
|
}
|
|
return container.innerHTML
|
|
} else {
|
|
if (window.Quarto?.typesetMath) {
|
|
window.Quarto.typesetMath(note);
|
|
}
|
|
return note.innerHTML;
|
|
}
|
|
} else {
|
|
// Remove any anchor links if they are present
|
|
const anchorLink = note.querySelector('a.anchorjs-link');
|
|
if (anchorLink) {
|
|
anchorLink.remove();
|
|
}
|
|
if (window.Quarto?.typesetMath) {
|
|
window.Quarto.typesetMath(note);
|
|
}
|
|
// TODO in 1.5, we should make sure this works without a callout special case
|
|
if (note.classList.contains("callout")) {
|
|
return note.outerHTML;
|
|
} else {
|
|
return note.innerHTML;
|
|
}
|
|
}
|
|
}
|
|
for (var i=0; i<xrefs.length; i++) {
|
|
const xref = xrefs[i];
|
|
tippyHover(xref, undefined, function(instance) {
|
|
instance.disable();
|
|
let url = xref.getAttribute('href');
|
|
let hash = undefined;
|
|
if (url.startsWith('#')) {
|
|
hash = url;
|
|
} else {
|
|
try { hash = new URL(url).hash; } catch {}
|
|
}
|
|
if (hash) {
|
|
const id = hash.replace(/^#\/?/, "");
|
|
const note = window.document.getElementById(id);
|
|
if (note !== null) {
|
|
try {
|
|
const html = processXRef(id, note.cloneNode(true));
|
|
instance.setContent(html);
|
|
} finally {
|
|
instance.enable();
|
|
instance.show();
|
|
}
|
|
} else {
|
|
// See if we can fetch this
|
|
fetch(url.split('#')[0])
|
|
.then(res => res.text())
|
|
.then(html => {
|
|
const parser = new DOMParser();
|
|
const htmlDoc = parser.parseFromString(html, "text/html");
|
|
const note = htmlDoc.getElementById(id);
|
|
if (note !== null) {
|
|
const html = processXRef(id, note);
|
|
instance.setContent(html);
|
|
}
|
|
}).finally(() => {
|
|
instance.enable();
|
|
instance.show();
|
|
});
|
|
}
|
|
} else {
|
|
// See if we can fetch a full url (with no hash to target)
|
|
// This is a special case and we should probably do some content thinning / targeting
|
|
fetch(url)
|
|
.then(res => res.text())
|
|
.then(html => {
|
|
const parser = new DOMParser();
|
|
const htmlDoc = parser.parseFromString(html, "text/html");
|
|
const note = htmlDoc.querySelector('main.content');
|
|
if (note !== null) {
|
|
// This should only happen for chapter cross references
|
|
// (since there is no id in the URL)
|
|
// remove the first header
|
|
if (note.children.length > 0 && note.children[0].tagName === "HEADER") {
|
|
note.children[0].remove();
|
|
}
|
|
const html = processXRef(null, note);
|
|
instance.setContent(html);
|
|
}
|
|
}).finally(() => {
|
|
instance.enable();
|
|
instance.show();
|
|
});
|
|
}
|
|
}, function(instance) {
|
|
});
|
|
}
|
|
let selectedAnnoteEl;
|
|
const selectorForAnnotation = ( cell, annotation) => {
|
|
let cellAttr = 'data-code-cell="' + cell + '"';
|
|
let lineAttr = 'data-code-annotation="' + annotation + '"';
|
|
const selector = 'span[' + cellAttr + '][' + lineAttr + ']';
|
|
return selector;
|
|
}
|
|
const selectCodeLines = (annoteEl) => {
|
|
const doc = window.document;
|
|
const targetCell = annoteEl.getAttribute("data-target-cell");
|
|
const targetAnnotation = annoteEl.getAttribute("data-target-annotation");
|
|
const annoteSpan = window.document.querySelector(selectorForAnnotation(targetCell, targetAnnotation));
|
|
const lines = annoteSpan.getAttribute("data-code-lines").split(",");
|
|
const lineIds = lines.map((line) => {
|
|
return targetCell + "-" + line;
|
|
})
|
|
let top = null;
|
|
let height = null;
|
|
let parent = null;
|
|
if (lineIds.length > 0) {
|
|
//compute the position of the single el (top and bottom and make a div)
|
|
const el = window.document.getElementById(lineIds[0]);
|
|
top = el.offsetTop;
|
|
height = el.offsetHeight;
|
|
parent = el.parentElement.parentElement;
|
|
if (lineIds.length > 1) {
|
|
const lastEl = window.document.getElementById(lineIds[lineIds.length - 1]);
|
|
const bottom = lastEl.offsetTop + lastEl.offsetHeight;
|
|
height = bottom - top;
|
|
}
|
|
if (top !== null && height !== null && parent !== null) {
|
|
// cook up a div (if necessary) and position it
|
|
let div = window.document.getElementById("code-annotation-line-highlight");
|
|
if (div === null) {
|
|
div = window.document.createElement("div");
|
|
div.setAttribute("id", "code-annotation-line-highlight");
|
|
div.style.position = 'absolute';
|
|
parent.appendChild(div);
|
|
}
|
|
div.style.top = top - 2 + "px";
|
|
div.style.height = height + 4 + "px";
|
|
div.style.left = 0;
|
|
let gutterDiv = window.document.getElementById("code-annotation-line-highlight-gutter");
|
|
if (gutterDiv === null) {
|
|
gutterDiv = window.document.createElement("div");
|
|
gutterDiv.setAttribute("id", "code-annotation-line-highlight-gutter");
|
|
gutterDiv.style.position = 'absolute';
|
|
const codeCell = window.document.getElementById(targetCell);
|
|
const gutter = codeCell.querySelector('.code-annotation-gutter');
|
|
gutter.appendChild(gutterDiv);
|
|
}
|
|
gutterDiv.style.top = top - 2 + "px";
|
|
gutterDiv.style.height = height + 4 + "px";
|
|
}
|
|
selectedAnnoteEl = annoteEl;
|
|
}
|
|
};
|
|
const unselectCodeLines = () => {
|
|
const elementsIds = ["code-annotation-line-highlight", "code-annotation-line-highlight-gutter"];
|
|
elementsIds.forEach((elId) => {
|
|
const div = window.document.getElementById(elId);
|
|
if (div) {
|
|
div.remove();
|
|
}
|
|
});
|
|
selectedAnnoteEl = undefined;
|
|
};
|
|
// Handle positioning of the toggle
|
|
window.addEventListener(
|
|
"resize",
|
|
throttle(() => {
|
|
elRect = undefined;
|
|
if (selectedAnnoteEl) {
|
|
selectCodeLines(selectedAnnoteEl);
|
|
}
|
|
}, 10)
|
|
);
|
|
function throttle(fn, ms) {
|
|
let throttle = false;
|
|
let timer;
|
|
return (...args) => {
|
|
if(!throttle) { // first call gets through
|
|
fn.apply(this, args);
|
|
throttle = true;
|
|
} else { // all the others get throttled
|
|
if(timer) clearTimeout(timer); // cancel #2
|
|
timer = setTimeout(() => {
|
|
fn.apply(this, args);
|
|
timer = throttle = false;
|
|
}, ms);
|
|
}
|
|
};
|
|
}
|
|
// Attach click handler to the DT
|
|
const annoteDls = window.document.querySelectorAll('dt[data-target-cell]');
|
|
for (const annoteDlNode of annoteDls) {
|
|
annoteDlNode.addEventListener('click', (event) => {
|
|
const clickedEl = event.target;
|
|
if (clickedEl !== selectedAnnoteEl) {
|
|
unselectCodeLines();
|
|
const activeEl = window.document.querySelector('dt[data-target-cell].code-annotation-active');
|
|
if (activeEl) {
|
|
activeEl.classList.remove('code-annotation-active');
|
|
}
|
|
selectCodeLines(clickedEl);
|
|
clickedEl.classList.add('code-annotation-active');
|
|
} else {
|
|
// Unselect the line
|
|
unselectCodeLines();
|
|
clickedEl.classList.remove('code-annotation-active');
|
|
}
|
|
});
|
|
}
|
|
const findCites = (el) => {
|
|
const parentEl = el.parentElement;
|
|
if (parentEl) {
|
|
const cites = parentEl.dataset.cites;
|
|
if (cites) {
|
|
return {
|
|
el,
|
|
cites: cites.split(' ')
|
|
};
|
|
} else {
|
|
return findCites(el.parentElement)
|
|
}
|
|
} else {
|
|
return undefined;
|
|
}
|
|
};
|
|
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
|
|
for (var i=0; i<bibliorefs.length; i++) {
|
|
const ref = bibliorefs[i];
|
|
const citeInfo = findCites(ref);
|
|
if (citeInfo) {
|
|
tippyHover(citeInfo.el, function() {
|
|
var popup = window.document.createElement('div');
|
|
citeInfo.cites.forEach(function(cite) {
|
|
var citeDiv = window.document.createElement('div');
|
|
citeDiv.classList.add('hanging-indent');
|
|
citeDiv.classList.add('csl-entry');
|
|
var biblioDiv = window.document.getElementById('ref-' + cite);
|
|
if (biblioDiv) {
|
|
citeDiv.innerHTML = biblioDiv.innerHTML;
|
|
}
|
|
popup.appendChild(citeDiv);
|
|
});
|
|
return popup.innerHTML;
|
|
});
|
|
}
|
|
}
|
|
});
|
|
</script>
|
|
</div> <!-- /content -->
|
|
|
|
|
|
|
|
|
|
</body></html> |