Python Forum
Help with for loop and return
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with for loop and return
#1
Hi all,

I've been learning python for a few weeks now and am working on my first, very simple program. To preface this... I've taken inspiration from a couple sources for this in the use of the os and subprocess libraries and some of the logic. I do not copy/paste. I type every line in an effort to learn and understand.

The goal of the program is to simply throw a command at ffmeg via the command line or terminal to generate and save screenshots.

I'm struggling with outputting a list of files in a directory, having a user select from the output, and then taking that selection's file location (currently derived through existing objects) and using it as part of the save location for the screenshots.

In taking inspiration from other scripts, I have the made some mistakes. The return is outside of the function and am probably missing something else.

Any help in identifying where I need to make adjustments would be much appreciated.

# Importing all necessary libraries
import os
import subprocess

# Create location objects and set working directory
source = input("Enter Video Location: ")
ssDest = str(source) + "/screenshots"
os.chdir(source)
files = os.listdir(source)

# Identify target video file
for f in files:
    print("#(0): {1}".format(n, f['title']))
    choice = ""
    n = 1
    n += 1
    if n > 8:
        break
    while True:
        choice = input("Pick a file: ")
        if choice.isdigit():
            choice = int(choice)
            break
        else:
            return 0

# Throw command at ffmeg
subprocess.call(['ffmeg', '-i', str(source) + str(n) + '.png', str(ssDest)])
P.S. The goal is to make this part of a larger program with more functionality as I learn. I'll create classes and methods once I start working on the second piece of the project. I really need more practice with the basics (loops, functions, standard libraries, etc).
Reply
#2
look through the following packages for screenshot capture. https://pypi.org/search/?q=screenshot&o=
You may find what you're looking fore there.
Reply
#3
(Jul-17-2020, 05:40 PM)pythonnewbie138 Wrote: P.S. The goal is to make this part of a larger program with more functionality as I learn. I'll create classes and methods once I start working on the second piece of the project. I really need more practice with the basics (loops, functions, standard libraries, etc).
The fist step is to start small and make sure stuff work,now your code dos not run at all.
Can not have have return when there is no function,and is not ffmeg but ffmpeg .

So as first step is to test subprocess with FFmpeg,to give a example.
import subprocess

video_file = 'Komodo.mp4'
subprocess.run(['ffmpeg', '-i', video_file, '-ss', '00:00:02', '-vframes', '1', 'output.png'])
So when this work can write a loop like this.
This loop over all .mp4, .mkv' in current folder and take screenshot when 2-sec in.
import os
import subprocess

for index, file in enumerate(os.scandir('.'), 1):
    if file.name.endswith(('.mp4', '.mkv')):
        print(f'Make screenshot of <{file.name}>')
        subprocess.run(['ffmpeg', '-i', file.name, '-ss', '00:00:02', '-vframes', '1', f'output{index}.png'])
Reply
#4
(Jul-17-2020, 06:49 PM)snippsat Wrote: So as first step is to test subprocess with FFmpeg,to give a example.
import subprocess

video_file = 'Komodo.mp4'
subprocess.run(['ffmpeg', '-i', video_file, '-ss', '00:00:02', '-vframes', '1', 'output.png'])

Thank you for your help! Here's what's happening when I try to run the above.

Error:
Traceback (most recent call last): File "C:\Users\tlilyquist\Python Apps\screenshot.test1.py", line 4, in <module> subprocess.run(['ffmpeg', '-i', video_file, '-ss', '00:00:02', '-vframes', '1', 'output.png']) File "C:\Users\tlilyquist\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 489, in run with Popen(*popenargs, **kwargs) as process: File "C:\Users\tlilyquist\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 854, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "C:\Users\tlilyquist\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 1307, in _execute_child hp, ht, pid, tid = _winapi.CreateProcess(executable, args, FileNotFoundError: [WinError 2] The system cannot find the file specified
Is it not finding the target file or the ffmpeg exe? I have test.mp4 located in the same folder as the .py file. I added ffmpeg/bin to my PATH in environment variables. Code below.

import subprocess

video_file = 'test.mp4'
subprocess.run(['ffmpeg', '-i', video_file, '-ss', '00:00:02', '-vframes', '1', 'output.png'])
Reply
#5
Do you have screenshot.test1.py and test.mp4 in same folder Python Apps?
If not do so,as i do not use a specif path to video file in my script
Reply
#6
Yes they are both in Python Apps.

Edit. I re-wrote 'test.mp4' as "test.mp4" and it works. On to the next part...
Reply
#7
Put ffmpeg.exe in same folder to see if it work.
Reply
#8
As I mentioned in the edit above, putting the filename in "" fixed that part.

In running the second snippet, it's not generating a .png. It prints:
Output:
Making screenshot of <test.mp4>
import os
import subprocess

for index, file in enumerate(os.scandir('.'), 1):
    if file.name.endswith(('.mp4', '.mkv')):
        print(f'Making screenshot of <{file.name}>')
        subprocess.run(['ffmpeg', '-i', file.name, '-ss-', '00:00:02', '-vframes', '1', f'output{index}.png'])
It's including the <>. I tried it without the <> and it prints the following but does not generate a screenshot.
Output:
Making screenshot of test.mp4
import os
import subprocess

for index, file in enumerate(os.scandir('.'), 1):
    if file.name.endswith(('.mp4', '.mkv')):
        print(f'Making screenshot of {file.name}')
        subprocess.run(['ffmpeg', '-i', file.name, '-ss-', '00:00:02', '-vframes', '1', f'output{index}.png'])
Reply
#9
(Jul-17-2020, 09:38 PM)pythonnewbie138 Wrote: In running the second snippet, it's not generating a .png. It prints:
Try with some other files,the loop should make no diffidence,it's just what the single file scripts dos to more files.
I can do it new environment that's not mine like Colab and it still work,here a link click on Files left side.
Reply
#10
Also here a improved version,as using index to make new number images is not so smart,
when can use real video names to also make image screenshot name.
import os
import subprocess

for file in os.scandir('.'):
    if file.name.endswith(('.mp4', '.mkv')):
        print(f'Make screenshot of <{file.name}>')
        subprocess.run(['ffmpeg', '-i', file.name, '-ss', '00:00:02', '-vframes', '1', f'{file.name.split(".")[0]}.png'])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to change from printFacts ( ) to return a list & Loop over list when writing CSV Ivan1 14 8,290 Aug-30-2017, 12:14 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020