Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save anujonthemove/6a17f29e0ae7f84ea750f393e575a453 to your computer and use it in GitHub Desktop.
Save anujonthemove/6a17f29e0ae7f84ea750f393e575a453 to your computer and use it in GitHub Desktop.
Convert to .mp4 from '.mkv', '.avi', '.asf', '.dav', '.mov' using ffmpeg, python subprocess
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "Code taken from: https://askubuntu.com/questions/396883/how-to-simply-convert-video-files-i-e-mkv-to-mp4"
},
{
"metadata": {
"trusted": false
},
"id": "c2fb2992",
"cell_type": "code",
"source": "import os\nimport pandas as pd\nimport subprocess",
"execution_count": 16,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "44791fa5",
"cell_type": "code",
"source": "# def gather_video_stats(base_path):\n \n# stats = []\n# allowed_file_extensions = ['mp4', 'avi', 'mkv', 'asf', 'mov', 'dav']\n \n# for root, dirs, files in os.walk(base_path):\n# for file in files:\n# abs_file_path = os.path.join(root, file)\n# extn = abs_file_path.split(os.path.sep)[-1].split(\".\")[-1] \n# if extn in allowed_file_extensions:\n# stats.append(get_video_stats(abs_file_path)+[extn])\n \n# stats_df = pd.DataFrame(stats, columns=['camera_name', 'date', 'video_name', 'width', 'height', 'fps', 'duration', 'extension'])\n# return stats_df",
"execution_count": 17,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "8a522eb9",
"cell_type": "code",
"source": "base_path = '/home/acer/sample_videos'",
"execution_count": 18,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "632bee88",
"cell_type": "code",
"source": "dest_path = '/home/acer/test_out/'",
"execution_count": 19,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "4bffdf91",
"cell_type": "code",
"source": "allowed_extns = ['.mkv', '.avi', '.asf', '.dav', '.mov']",
"execution_count": 20,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "a543f6ab",
"cell_type": "code",
"source": "for i in os.listdir(base_path):\n abs_file_path = os.path.join(base_path, i)\n # filename, extn = os.path.splitext(abs_file_path)\n filename, extn = os.path.splitext(os.path.basename(abs_file_path))\n \n if extn in allowed_extns:\n \n abs_output_file_path = os.path.join(dest_path, filename+'.mp4')\n cmd = [\"ffmpeg\", \"-i\", abs_file_path, \"-codec\", \"copy\", abs_output_file_path]\n print(subprocess.check_output(cmd).decode(\"utf-8\").strip())\n ",
"execution_count": 21,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "\n\n\n\n"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "9fe5be0c",
"cell_type": "code",
"source": "os.path.split(abs_file_path)",
"execution_count": 7,
"outputs": [
{
"data": {
"text/plain": "('/home/acer/sample_videos', '4.mkv')"
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "11e8f406",
"cell_type": "code",
"source": "abs_file_path.split(os.path.sep)",
"execution_count": 9,
"outputs": [
{
"data": {
"text/plain": "['', 'home', 'acer', 'sample_videos', '4.mkv']"
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "4e6216ef",
"cell_type": "code",
"source": "os.path.splitext(abs_file_path)",
"execution_count": 10,
"outputs": [
{
"data": {
"text/plain": "('/home/acer/sample_videos/4', '.mkv')"
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "bc871af5",
"cell_type": "code",
"source": "cmd",
"execution_count": 9,
"outputs": [
{
"data": {
"text/plain": "['ffmpeg',\n '-i',\n '/home/acer/sample_videos/NVR_ch1_main_20210216165000_20210216170000.dav',\n '-codec',\n 'copy',\n '/home/acer/sample_videos/NVR_ch1_main_20210216165000_20210216170000.mp4']"
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "92071483",
"cell_type": "code",
"source": "abs_output_file_path",
"execution_count": 10,
"outputs": [
{
"data": {
"text/plain": "'/home/acer/sample_videos/NVR_ch1_main_20210216165000_20210216170000.mp4'"
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "5f86392e",
"cell_type": "code",
"source": "filename",
"execution_count": 11,
"outputs": [
{
"data": {
"text/plain": "'/home/acer/sample_videos/NVR_ch1_main_20210216165000_20210216170000'"
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "b97a2bf6",
"cell_type": "code",
"source": "os.path.split(abs_file_path)",
"execution_count": 13,
"outputs": [
{
"data": {
"text/plain": "('/home/acer/sample_videos', 'NVR_ch1_main_20210216165000_20210216170000.dav')"
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "9967e11b",
"cell_type": "code",
"source": "filename, extn = os.path.splitext(os.path.basename(abs_file_path))",
"execution_count": 14,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "ccfdf13f",
"cell_type": "code",
"source": "filename",
"execution_count": 15,
"outputs": [
{
"data": {
"text/plain": "'NVR_ch1_main_20210216165000_20210216170000'"
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "3df203eb",
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"id": "120061c9",
"cell_type": "markdown",
"source": "# References\n1. the best \"If you only want to convert MKV to MP4 then you will save quality and a lot of time by just changing the containers.\": https://askubuntu.com/questions/396883/how-to-simply-convert-video-files-i-e-mkv-to-mp4\n\nUnread:\n1. https://stackoverflow.com/questions/64519818/converting-mkv-files-to-mp4-with-ffmpeg-python\n2. https://stackoverflow.com/questions/32714579/ffmpeg-converts-anything-to-mp4\n3. https://gist.github.com/elrond1999/3019676\n4. https://linuxhint.com/convert_videos_using_ffmpeg_ubuntu/\n5. Good tutorial: https://opensource.com/article/17/6/ffmpeg-convert-media-file-formats\n6. https://stackoverflow.com/questions/32827539/ffmpeg-conversion-dav-to-any-video-files"
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.9",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"varInspector": {
"window_display": false,
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"library": "var_list.py",
"delete_cmd_prefix": "del ",
"delete_cmd_postfix": "",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"library": "var_list.r",
"delete_cmd_prefix": "rm(",
"delete_cmd_postfix": ") ",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
]
},
"gist": {
"id": "",
"data": {
"description": "Convert to .mp4 from '.mkv', '.avi', '.asf', '.dav', '.mov' using ffmpeg, python subprocess",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment