Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added -r recursive flag functionality #7

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ it will capture screenshots of unique frames and save it output folder...once sc

# Example
There are two sample video avilable in "./input", you can test the code using these input by running
<li>python video2pdfslides.py "./input/Test Video 1.mp4" (4 unique slide)
<li>python video2pdfslides.py "./input/Test Video 2.mp4" (19 unique slide)
<li>python video2pdfslides.py "./input/Test Video 1.mp4" (4 unique slide)</li>
<li>python video2pdfslides.py "./input/Test Video 2.mp4" (19 unique slide)</li>

## Running the code for on a folder of presentations.
<li>python video2pdfslides.py -r ./folder_path</li>

# More
The default parameters works for a typical video presentation. But if the video presentation has lots of animations, the default parametrs won't give a good results, you may notice duplicate/missing slides. Don't worry, you can make it work for any video presentation, even the ones with animations, you just need to fine tune and figure out the right set of parametrs, The 3 most important parameters that I would recommend to get play around is "MIN_PERCENT", "MAX_PERCENT", "FGBG_HISTORY". The description of these variables can be found in code comments.
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
imutils==0.5.4
opencv_python==4.5.2.52
opencv_python
img2pdf==0.4.1
62 changes: 37 additions & 25 deletions video2pdfslides.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ def detect_unique_screenshots(video_path, output_folder_screenshot_path):
# varThreshold = Threshold on the squared Mahalanobis distance between the pixel and the model to decide whether a pixel is well described by the background model. This parameter does not affect the background update.
# detectShadows = If true, the algorithm will detect shadows and mark them. It decreases the speed a bit, so if you do not need this feature, set the parameter to false.

# Initialize fgbg a Background object with Parameters
# ... (rest of the function remains unchanged)
fgbg = cv2.createBackgroundSubtractorMOG2(history=FGBG_HISTORY, varThreshold=VAR_THRESHOLD,detectShadows=DETECT_SHADOWS)


Expand Down Expand Up @@ -110,7 +112,13 @@ def detect_unique_screenshots(video_path, output_folder_screenshot_path):

def initialize_output_folder(video_path):
'''Clean the output folder if already exists'''
output_folder_screenshot_path = f"{OUTPUT_SLIDES_DIR}/{video_path.rsplit('/')[-1].split('.')[0]}"
if os.path.isdir(video_path):
# Assuming the directory name is the base name for the output folder
base_name = os.path.basename(video_path)
else:
base_name = os.path.splitext(os.path.basename(video_path))[0]

output_folder_screenshot_path = f"{OUTPUT_SLIDES_DIR}/{base_name}"

if os.path.exists(output_folder_screenshot_path):
shutil.rmtree(output_folder_screenshot_path)
Expand All @@ -119,42 +127,46 @@ def initialize_output_folder(video_path):
print('initialized output folder', output_folder_screenshot_path)
return output_folder_screenshot_path


def convert_screenshots_to_pdf(output_folder_screenshot_path):
output_pdf_path = f"{OUTPUT_SLIDES_DIR}/{video_path.rsplit('/')[-1].split('.')[0]}" + '.pdf'
# Extract the base name from the output folder path
base_name = os.path.basename(output_folder_screenshot_path)
output_pdf_path = f"{OUTPUT_SLIDES_DIR}/{base_name}.pdf"
print('output_folder_screenshot_path', output_folder_screenshot_path)
print('output_pdf_path', output_pdf_path)
print('converting images to pdf..')
with open(output_pdf_path, "wb") as f:
f.write(img2pdf.convert(sorted(glob.glob(f"{output_folder_screenshot_path}/*.png"))))
print('Pdf Created!')
print('pdf saved at', output_pdf_path)
print('Pdf Created!')
print('pdf saved at', output_pdf_path)


if __name__ == "__main__":

# video_path = "./input/Test Video 2.mp4"
# choice = 'y'
# output_folder_screenshot_path = initialize_output_folder(video_path)


parser = argparse.ArgumentParser("video_path")
parser.add_argument("video_path", help="path of video to be converted to pdf slides", type=str)
parser.add_argument("-r", "--recursive", action='store_true', help="process all .mp4 files within the specified folder recursively")
args = parser.parse_args()
video_path = args.video_path

print('video_path', video_path)
output_folder_screenshot_path = initialize_output_folder(video_path)
detect_unique_screenshots(video_path, output_folder_screenshot_path)

print('Please Manually verify screenshots and delete duplicates')
while True:
choice = input("Press y to continue and n to terminate")
choice = choice.lower().strip()
if choice in ['y', 'n']:
break
else:
print('please enter a valid choice')

if choice == 'y':
convert_screenshots_to_pdf(output_folder_screenshot_path)
recursive = args.recursive

if recursive:
# Process all .mp4 files within the specified folder recursively
for root, dirs, files in os.walk(video_path):
for file in files:
if file.endswith(".mp4"):
video_file_path = os.path.join(root, file)
# Check if a PDF file with the same base name already exists
base_name = os.path.splitext(os.path.basename(video_file_path))[0]
output_pdf_path = f"{OUTPUT_SLIDES_DIR}/{base_name}.pdf"
if os.path.exists(output_pdf_path):
print(f'PDF already exists for {base_name}, skipping screenshot detection.')
continue
output_folder_screenshot_path = initialize_output_folder(video_file_path)
detect_unique_screenshots(video_file_path, output_folder_screenshot_path)
convert_screenshots_to_pdf(output_folder_screenshot_path)
else:
# Process a single video file
output_folder_screenshot_path = initialize_output_folder(video_path)
detect_unique_screenshots(video_path, output_folder_screenshot_path)
convert_screenshots_to_pdf(output_folder_screenshot_path)