How Python helped me in releasing a 20 Hours long Power BI Video

Combining 170+ Videos into One Long Tutorial — Solving the Chapter Timing Challenge using Python

Amit Chandak
4 min readJan 13, 2025

Over the past year, I’ve created over 170 Power BI videos. At the start of January 2025, I decided to combine them into a single, massive 20-hours video. I used Adobe Premiere Pro to merge all the clips, and then uploaded the final video to YouTube. However, this led to an unexpected challenge: I wanted to create timestamps (or chapters) to help viewers navigate through this marathon tutorial.

Manually noting down the exact start times of each of the videos and then putting them together was error-prone and tedious. That’s when I turned to Python for help. There was an idea to write (or find) a simple Python script that could list all the files in a folder and calculate each clip’s duration. Ideally, it would generate a spreadsheet with the file names and their lengths.

Initially, I tried a library import that didn’t quite work for my needs, so I searched on Stack Overflow and found a working import statement. Once I got the required libraries installed, I successfully exported the timestamps into Excel. After some quick cleanup in Excel — such as creating the timestamp format YouTube expects — I was ready to paste them into the video description.

One last twist: YouTube only allows 100 chapters per video. Since my project far exceeded that limit, I had to merge some topics and group a few timestamps. This Python-powered approach saved me a massive amount of time and frustration — proof that coding can simply be some tedious day-to-day task.

"""
This script calculates the duration (in seconds) of all .mp4 files in a given folder
and exports the results (file names and durations) to an Excel file.

Dependencies:
- MoviePy (pip install moviepy)
- pandas (pip install pandas)
- openpyxl (pip install openpyxl), required by pandas for Excel output

Usage:
1. Update the folder_path variable to point to the directory with your .mp4 files.
2. Update the output_excel variable to the desired output Excel filename.
3. Run the script.

Example:
folder_path = "e:/Videos"
output_excel = "video_durations.xlsx"
get_mp4_durations(folder_path, output_excel)
"""
import os
from moviepy.video.io.VideoFileClip import VideoFileClip
import pandas as pd

def get_mp4_durations(folder_path, output_excel):
"""
Scans the specified folder for all .mp4 files, calculates their duration,
and writes the results to an Excel file.

:param folder_path: The path to the folder containing .mp4 files.
:type folder_path: str
:param output_excel: The name/path of the Excel file to be created or overwritten.
:type output_excel: str

:return: None
"""
# List to store file name and duration
file_data = []

# List all files in the folder non-recursively
files = os.listdir(folder_path)

for file_name in files:
# Build full file path
file_path = os.path.join(folder_path, file_name)

# Check if it's a file and has .mp4 extension
if os.path.isfile(file_path) and file_name.lower().endswith('.mp4'):
try:
# Get video duration using VideoFileClip
with VideoFileClip(file_path) as video:
duration = video.duration # Duration in seconds
# Append file name and duration to list
file_data.append({"File Name": file_name, "Duration (s)": duration})
except Exception as e:
print(f"Error processing file {file_name}: {e}")

# Create a DataFrame from the list
df = pd.DataFrame(file_data)

# Write the DataFrame to an Excel file
df.to_excel(output_excel, index=False)

print(f"Data has been written to {output_excel}")

# Replace 'your_folder_path' with the path to the folder containing the files
# Replace 'output_file.xlsx' with the desired output file name
folder_path = "e:\Videos"
output_excel = "video_durations.xlsx"
get_mp4_durations(folder_path, output_excel)

How It Works

  1. Folder Enumeration: The script uses os.listdir(folder_path) to list all items in the specified directory.
  2. File Filtering: It checks each item to ensure it’s an actual file (not a subfolder) and that its filename ends with .mp4.
  3. Duration Calculation: For each valid file, it opens the video using VideoFileClip and retrieves the duration in seconds (video.duration).
  4. Data Storage: The file name and duration are stored in a list of dictionaries.
  5. Excel Writing: The script converts that list into a pandas DataFrame and writes it to an Excel file (to_excel).

Make sure you have the required libraries installed:

pip install moviepy pandas openpyxl

I hope you will like my 20-hour-long video as well as this code.

My Medium blogs can be found here if you are interested

Click here to access all my blogs and videos in a jiffy via an exclusive glossary using Power BI. Please like, share, and comment on these blogs. I would appreciate your suggestions for improvement, challenges, and suggestions for topics so that I can explore these in more depth.

In addition, I have over 750 videos on my YouTube channel that are related to Power BI, Tableau, and Incorta. With the help of these videos, you will learn hundreds of DAX and Power Query functions, in addition to hundreds of Power BI, use cases that will assist you in becoming an expert in Power BI. Make sure you subscribe, like, and share it with your friends.

Master Power BI
Expertise in Power BI
Power BI For Tableau User
DAX for SQL Users
Learn SQL

--

--

Amit Chandak
Amit Chandak

Written by Amit Chandak

Amit has 20+ years of experience in Business Intelligence, Analytics, Data Science. He is Chief Analytics Officer at Kanerika & Power BI community Super User

No responses yet