VideoHelp Forum

Try DVDFab Video Downloader and rip Netflix video! Or Try DVDFab and copy Blu-rays! or rip iTunes movies!
+ Reply to Thread
Results 1 to 4 of 4
  1. Member
    Join Date
    Sep 2018
    North America
    Search Comp PM
    As I delve deeper into Python, I am finding more things I like. I am not aware of any way to add a variable to an ffmpeg batch file, but it seems like it might be possible using Python. Let's say you've got a batch file like this:

    for %%a in ("*.mp4") do ffmpeg -y -i ^"%%a^" -c:v libx264 -b:v 2000k -filter:v scale=720:400 -pass 1 -an -f mp4 NU

    for %%a in ("*.mp4") do ffmpeg -i ^"%%a^" -c:v libx264 -b:v 2000k -filter:v scale=720:400 -pass 2 -c:a copy ^"converted/%%~na.mp4^"

    What I would like to do, if possible, is make the video bit rate a variable that would pop up when I opened the Python file (I use Python to automatically run both batch files back to back). Is there some way that I could use Python to ask for a value for the bit rate I want to use every time I run the Python file? Something similar to this:

    for %%a in ("*.mp4") do ffmpeg -y -i ^"%%a^" -c:v libx264 -b:v Xk -filter:v scale=720:400 -pass 1 -an -f mp4 NU

    for %%a in ("*.mp4") do ffmpeg -i ^"%%a^" -c:v libx264 -b:v Xk -filter:v scale=720:400 -pass 2 -c:a copy ^"converted/%%~na.mp4^"

    When I open the .py file, maybe a popup where I could type in the bit rate X I want to use for that particular video file. Would really appreciate any input.
    Thank you
    Quote Quote  
  2. I am PLEABS
    Join Date
    Apr 2018
    Search Comp PM
    there is input() in python to make it ask you before do a thing.
    here some simple code with input()
    a = input("input 1st command >")
    b = input("input 2nd command >")
    print("first variable is {0} second variable is {1}".format(a, b))
    Quote Quote  
  3. Or it could be passed as argument if you make your python as more general py script/modul

    But you can make it all in Python anyway ,do everything in Python, its subprocess etc. and open a ways toward other things, do not forget you can inject Vapoursynth commands in there if you wanted,

    this is the script that asks for 2pass average bitrate and encodes all mp4's to files in stated directory:
    encodes files with certain extensions from a directory to 2pass average bitrate
    define your correct directory with videofiles and ffmpeg.exe path below
    import os
    import subprocess
    directory   = r'F:\test'
    extensions  = (".mp4") #extensions to be encoded or (".mp4", ".mov") if more type of extensions 
    ffmpeg      = r'C:\path_to_exe\ffmpeg.exe'
    while True:
        bitrate = input('What is your 2pass average bitrate?: ')
            bitrate = int(bitrate)
            print('must be a number')
    output_dir_name = 'converted 2pass {}k'.format(bitrate)
    output = os.path.join(directory, output_dir_name)
    if not os.path.isdir(output):
        print('output directory created:\n', output)
    def run_process(cmd):
        with open(log_path, 'a') as log:   
            p = subprocess.Popen(cmd, stderr=subprocess.PIPE, universal_newlines=True, shell=True)
            for line in p.stderr:
                Printing all readouts live,
                take that comment out if debugging to see those ffmpeg's readouts
                but there is always a log available for those to see later if anything went wrong,
                this adds to log file
                text for that log file is added into log after this process ends and log is closed
                log.write(line)  #writing to log
            #p.terminate()    #might be needed       
    def encode_2pass(i, file_path):
        base = os.path.basename(file_path)
        name = os.path.splitext(base)[0]
        out_file_path = os.path.join(output, name + '.mp4')
        cmd1 = [ffmpeg, '-y', '-i', file_path, '-c:v', 'libx264', '-b:v', str(bitrate)+'k',  '-filter:v',  'scale=720:400', '-pass', '1', '-an', '-f', 'mp4', 'NUL']
        cmd2 = [ffmpeg, '-y', '-i', file_path, '-c:v', 'libx264', '-b:v', str(bitrate)+'k',  '-filter:v',  'scale=720:400', '-pass', '2', '-c:a',  'copy', '-f', 'mp4', out_file_path]
        print('\nprocessing 1pass: {} of {}:  {}'.format(i+1, total_files, file_path))
        print('\nprocessing 2pass: {} of {}:  {}'.format(i+1, total_files, file_path))
    '''get paths for all your files'''
    file_path_list = []
    for file in os.listdir(directory):
        if file.lower().endswith(tuple(extensions)):
            file_path = os.path.join(directory, file)
    total_files = len(file_path_list)
    print( 'number of files with {} extension: {}'.format(extensions, total_files))
    '''log file'''
    log_path = os.path.join(output, 'log.log')
    if os.path.isfile(log_path):
        #to clear log
        open(log_path, 'w').close()
    print into tkinter GUI, not Python console, if you import errorwindow3k modul to this script (that's it!), download  from:
    #import errorwindow3k
    '''encode files'''
    for i, file_path in enumerate(file_path_list):
        encode_2pass(i, file_path)
    print('done with all files')
    Last edited by _Al_; 30th Jun 2019 at 20:38. Reason: watch it , fixed '-i' instead of '-if' in ffmpeg cmd1 line
    Quote Quote  
  4. I just copy your ffmpeg example, but there is no passing or encoding of audio, I think you are aware of this, this was perhaps your tests.

    edit, sorry, audio is it is taken away just for the first pass because video track is analyzed only, I fixed that python script

    But you should go with CRF, 1pass encoding only, you'd need to do couple of movies and then you'd know what CRF value is for you. You might correct that value after encoding 5-10 movies calculating the average and watching for storage. You save lots of time. But the whole thing is suspicious , I mean encoding it again and using low resolution. The quality will suffer. Not sure if it is worthy to do it like that and degrade some collection of yours. It will look pretty bad on bigger screen.
    Last edited by _Al_; 30th Jun 2019 at 20:35.
    Quote Quote  

Similar Threads