Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MuhammetOzturk/e638a053b574491b4b547ab7b723ca7e to your computer and use it in GitHub Desktop.
Save MuhammetOzturk/e638a053b574491b4b547ab7b723ca7e to your computer and use it in GitHub Desktop.
Bosluk Tusuna Basarak Kameradan Kayit Alma.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyMqAt0c7xuwZ08HIzs0QK3Q",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/MuhammetOzturk/e638a053b574491b4b547ab7b723ca7e/bosluk-tusuna-basarak-kameradan-kayit-alma.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "XqH9pMTMEhBP"
},
"outputs": [],
"source": [
"from IPython.display import Javascript, display\n",
"from google.colab import output\n",
"import base64\n",
"import uuid\n",
"\n",
"video_file_path = f\"/content/webcam_record_{uuid.uuid4().hex}.webm\"\n",
"\n",
"def save_video(b64_video):\n",
" data = base64.b64decode(b64_video.split(',')[1])\n",
" with open(video_file_path, 'wb') as f:\n",
" f.write(data)\n",
" print(f\"📁 Video kaydedildi: {video_file_path}\")\n",
"\n",
"output.register_callback(\"notebook.save_video\", save_video)\n",
"\n",
"display(Javascript(\"\"\"\n",
"let div = document.createElement('div');\n",
"let video = document.createElement('video');\n",
"let btn = document.createElement('button');\n",
"let stream;\n",
"let mediaRecorder;\n",
"let recordedChunks = [];\n",
"\n",
"video.style.border = '2px solid black';\n",
"video.style.width = '480px';\n",
"video.style.height = '360px';\n",
"video.setAttribute('autoplay', '');\n",
"video.setAttribute('muted', '');\n",
"video.setAttribute('playsinline', '');\n",
"\n",
"btn.innerText = \"Hazır\";\n",
"btn.style.marginTop = \"10px\";\n",
"\n",
"div.appendChild(video);\n",
"div.appendChild(btn);\n",
"document.body.appendChild(div);\n",
"\n",
"btn.onclick = async () => {\n",
" if (!stream) {\n",
" stream = await navigator.mediaDevices.getUserMedia({ video: true });\n",
" video.srcObject = stream;\n",
" }\n",
" alert(\"Boşluk tuşuna basarak kaydı başlatıp durdurabilirsiniz.\");\n",
"};\n",
"\n",
"document.addEventListener('keydown', async (e) => {\n",
" if (e.code === 'Space') {\n",
" if (!mediaRecorder || mediaRecorder.state === 'inactive') {\n",
" recordedChunks = [];\n",
" mediaRecorder = new MediaRecorder(stream, { mimeType: 'video/webm' });\n",
" mediaRecorder.ondataavailable = function(e) {\n",
" if (e.data.size > 0) {\n",
" recordedChunks.push(e.data);\n",
" }\n",
" };\n",
" mediaRecorder.onstop = function() {\n",
" let blob = new Blob(recordedChunks, { type: 'video/webm' });\n",
" let reader = new FileReader();\n",
" reader.onloadend = function() {\n",
" google.colab.kernel.invokeFunction('notebook.save_video', [reader.result], {});\n",
" };\n",
" reader.readAsDataURL(blob);\n",
" };\n",
" mediaRecorder.start();\n",
" console.log(\"🔴 Kayıt başladı\");\n",
" } else if (mediaRecorder.state === 'recording') {\n",
" mediaRecorder.stop();\n",
" console.log(\"⏹️ Kayıt durduruldu\");\n",
" }\n",
" }\n",
"});\n",
"\"\"\"))\n"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment