-3

I have this code that shows the duration of videos in path.

import os 
import cv2 
import datetime

path = 'H:\path\course'

array = []

for directory, subdirectory, files in os.walk(path):
    for file in files:
        merge = os.path.join(directory,file)
        if file.lower().endswith('.mov') or file.lower().endswith('.flv') or file.lower().endswith('.mp4'):
            data = cv2.VideoCapture(merge)
            frames = data.get(cv2.CAP_PROP_FRAME_COUNT) 
            fps = int(data.get(cv2.CAP_PROP_FPS)) 
            seconds = int(frames / fps) 
            video_time = str(datetime.timedelta(seconds=seconds)) 
            # print("duration in seconds:", seconds) 
            # print("video time:", video_time) 
            # print(merge,' == ', video_time)
            array += str(video_time)

print('Total: ', sum(array))

I tried putting it on a list to do the sum of all the hours at once and it didn't work.

It came out like this:

Total:  ['0', ':', '0', '1', ':', '0', '0', '0', ':', '2', '5', ':', '0', '1', '0', ':', '1', '2', ':', '3', '5', '0', ':', '1', '2', ':', '2', '3', '0', ':', '0', '1', ':', '2', '9', '0', ':', '1', '5', ':', '1', '5', '0', ':', '3', '4', ':', '1', '3', '0', ':', '2', '4', ':', '5', '4', '0', ':', '1', '6', ':', '2', '6', '0', ':', '1', '4', ':', '1', '4', '0', ':', '1', '6', ':', '3', '6', '0', ':', '0', '2', ':', '5', '4', '0', ':', '0', '2', ':', '4', '4', '0', ':', '2', '6']

How do I add the total duration of all outputs?

PSEdit:

The colleague's answer helped me to solve the problem. But another problem came up that might help other beginners like me in the future.

When adding the var video_time to array other than str, it returned the full value. So I had to send the seconds to the array as int and then convert to hours.

  • 4
    Why are you converting to `str`? The reason it's all individual characters is you used `+=` instead of `array.append`, but why you convert everything to strings is non-obvious. – ShadowRanger May 25 '22 at 19:48
  • 1
    What do you think `sum` is supposed to do on a list of strings? – Random Davis May 25 '22 at 19:50
  • @RandomDavis It's supposed to crash with a type error (and it [does](https://tio.run/##RcsxCoNAEEbhfk7xd7NL7GyC4C3ShSArKlnQnWWcCJ5@lVj46u/l3b6S6mfWUoJq2NHi/SGaRLHFYZTO4jIiJvAkwhW4D8oN4ezyjxarqbuxJ8oakzl@iYW5wXmtv8X9ufelHA) for me). – Kelly Bundy May 25 '22 at 19:54
  • What Python implementation/version are you using? – Kelly Bundy May 25 '22 at 19:55
  • See where you have the commented out debug line `print("duration in seconds:", seconds) `? What kind of values were you seeing when that was not commented out? Do you see a way that those values could be used, directly, in order to solve the problem? – Karl Knechtel May 25 '22 at 19:59
  • @ShadowRanger It was just a mistake. I was testing and ended up uploading it here. – Sandson Costa May 26 '22 at 17:36
  • @KarlKnechtel It just returns in seconds. Just it. – Sandson Costa May 26 '22 at 17:38
  • @SandsonCosta: Okay, but your demonstrated problem is related to the specific code you posted. If you have some other problem, in some other code, we can't help unless you post the [MCVE] corresponding to your *real* problem without introducing new problems. – ShadowRanger May 26 '22 at 17:38
  • @ShadowRanger your comment was the answer to my headache. Apparently the answer was quite simple, but my knowledge didn't allow it lol. Could you please comment as an answer for me to rate? I'll add an addition that I made to the code. Thank you sow much. – Sandson Costa May 26 '22 at 17:53
  • @SandsonCosta: If that's your problem, this is a dupe of [python list: append vs +=](https://stackoverflow.com/q/53112952/364696) (which has a bad answer, but a good answer in the comments, and is duped to a good explanation of the differences between `.append` and `.extend`, where `+=` is *almost* equivalent to `.extend`, aside from reassigning the left-hand side, which matters if the left-hand is immutable, e.g. a `list` stored inside a `tuple`). – ShadowRanger May 26 '22 at 17:57
  • What I'm suggesting is, since those seconds values are *numbers*, why not try adding them first, and then converting to a time span afterwards? – Karl Knechtel May 26 '22 at 20:30

0 Answers0