Apr-15-2017, 02:44 PM
Convert .MTS files
Convert .MTS files
|
I've now added the slashes
import subprocess import os path_in = 'C:/upload/' path_out = 'C:/upload/completed/' for file in os.scandir(path_in): if file.name.endswith('mts'): #print(file.name) file_path = os.path.join(path_in, file.name) subprocess.run(['ffmpeg', '-i', f'{file_path}', '-c:v', 'copy', f'{path_out+os.path.splitext(file.name)[0]}.mp4'])result remains
(Apr-15-2017, 02:50 PM)pberrett Wrote: I've now added the slashesOnly needed for last one. path_in = 'C:/upload' path_out = 'C:/upload/completed/'Add . :endswith('.mts'):Folder complete most excites. You know that is test this with mkv to mp4? I don't have mts files,so not sure if that subprocess(ffmpeg) command work for mts. You most change subprocces command to what work for mts file on command line(ffmpeg). So based on you previous post. subprocess.run(['ffmpeg', '-i', f'{file_path}', '-r', '30', '-c:v', 'libx264', '-crf', '23', '-ac', '1', f'{path_out+os.path.splitext(file.name)[0]}.mp4'])
Here is function to make it cleaner.
import subprocess import os def convert_video(path_in, path_out, to_file_ext, org_ext): ''' Convert video files in a folder to different format Change ffpmeg command in subprocess to fit single ffmpeg command line run ''' for file in os.scandir(path_in): if file.name.endswith(f'.{org_ext}'): file_path_in = os.path.join(path_in, file.name) path_out = f'{path_out}/' file_path_out = f'{path_out+os.path.splitext(file.name)[0]}.{to_file_ext}' subprocess.run(['ffmpeg', '-i', f'{file_path_in}', '-c:v', 'copy', f'{file_path_out}']) if __name__ == '__main__': path_in = 'C:/ffmpeg_new/bin' path_out = 'C:/ffmpeg_new/old' org_ext = 'mkv' to_file_ext = 'mp4' convert_video(path_in, path_out, to_file_ext, org_ext)So beside obvious changes like path and format. Change ffmpeg command to what you run before from command line. Eg: subprocess.run(['ffmpeg', '-i', f'{file_path_in}', '-c:v', 'libx264', '-vf', 'scale=1280:720', '-crf', '23', f'{file_path_out}']) subprocess.run(['ffmpeg', '-i', f'{file_path_in}', '-r', '30', '-s', '1280x720', '-c:v', 'libx264', '-crf', '23', '-ac', '1', f'{file_path_out}'])
Apr-15-2017, 07:50 PM
Just a question about using of f-strings (as I am still on 3.5.x, its use occasionally confuse me). Is there any benefit in using
path_out = f'{path_out}/' instead of path_out += "/" or using f'{file_path_in}' instead of file_path_in ?path_out = f'{path_out}/' file_path_out = f'{path_out+os.path.splitext(file.name)[0]}.{to_file_ext}' file_path_out = os.path.join(path_out, f'{os.path.splitext(file.name)[0]}.{to_file_ext}')
Apr-15-2017, 08:12 PM
(Apr-15-2017, 07:50 PM)zivoni Wrote: or using f'{file_path_in}' instead of file_path_in?I just forget to remove f after i moved expression of the subprocess line.So it should like this. import subprocess import os def convert_video(path_in, path_out, to_file_ext, org_ext): ''' Convert video files in a folder to different format Change ffpmeg command in subprocess to fit single ffmpeg from command line ''' for file in os.scandir(path_in): if file.name.endswith(f'.{org_ext}'.upper()): file_path_in = os.path.join(path_in, file.name) path_out = f'{path_out}/' file_path_out = f'{path_out+os.path.splitext(file.name)[0]}.{to_file_ext}' subprocess.run(['ffmpeg', '-i', file_path_in, '-c:v', 'copy', file_path_out]) #subprocess.run(['ffmpeg', '-i', file_path_in, '-c:v', 'libx264', '-vf', 'scale=1280:720', '-crf', '23', file_path_out]) if __name__ == '__main__': path_in = 'C:/ffmpeg_new/bin' path_out = 'C:/ffmpeg_new/old' org_ext = 'MTS' to_file_ext = 'mp4' convert_video(path_in, path_out, to_file_ext, org_ext)@pberrett i have tested with MTS file downloaded from here. Both subprocess lines in script over work, first line just copy video format. Second line re scale and use H.264 video(libx264') format.
Apr-16-2017, 03:54 PM
Hi all
I have finished my program and it works fine. Thanks to everyone who helped. Here is the finished result. #Program for converting .mts files to .mp4 files and uploading these to a server. import subprocess import os, sys import ftplib # function for converting videos to specified format def convert_video(path_in, path_out, to_file_ext, org_ext): ''' Convert video files in a folder to different format Change ffpmeg command in subprocess to fit single ffmpeg from command line ''' for file in os.scandir(path_in): if file.name.endswith(f'.{org_ext}'.upper()): file_path_in = os.path.join(path_in, file.name) path_out = f'{path_out}/' file_path_out = f'{path_out+os.path.splitext(file.name)[0]}.{to_file_ext}' subprocess.run(['ffmpeg', '-i', file_path_in, '-r', '30', '-s', '1280x720', '-c:v', 'libx264', '-crf', '22', '-ac', '1', file_path_out]) print ("Starting...") path_in = 'C:/upload' # files for conversion and uploading are stored here path_out = 'C:/uploadtemp' # converted files are stored here temporarily #call videos to be processed if __name__ == '__main__': org_ext = 'MTS' to_file_ext = 'mp4' convert_video(path_in, path_out, to_file_ext, org_ext) print ("Conversions finished...") # FTP sessiona and uploading os.chdir("/uploadtemp") session = ftplib.FTP('myserver.com','mylogin','mypassword') path = "/uploadtemp" dirs = os.listdir( path ) #upload each file for eachfile in dirs: file = open(eachfile,'rb') # file to send session.storbinary('STOR ' +eachfile, file) # send the file file.close() # close file and FTP session.quit() print ("converted files uploaded...") #clear temporary directory for eachfile in dirs: os.remove(eachfile) print ("Temporary directory emptied...") #clear directory with files for conversion path= "/upload" dirs = os.listdir( path ) os.chdir("/upload") for eachfile in dirs: os.remove(eachfile) print ("Upload directory emptied...") print ("Finished")The way this works is that lets' say I have a bunch of mts files I want to convert to mp4 files and then upload the results to a certain ftp server. This script converts the files into mp4 files which are put into the c:\uploadtemp directory. they are then uploaded to the ftp server and both c:\upload and c:\uploadtemp are both emptied of all files. Any comments are welcome. cheers Peter |
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
Convert Xls files into Csv in on premises sharpoint | Andrew_andy9642 | 3 | 1,019 |
Aug-30-2024, 06:41 PM Last Post: deanhystad |
|
python convert multiple files to multiple lists | MCL169 | 6 | 3,253 |
Nov-25-2023, 05:31 AM Last Post: Iqratech |
|
Separate text files and convert into csv | marfer | 6 | 4,740 |
Dec-10-2021, 12:09 PM Last Post: marfer |
|
Convert a PDF files to HTML files | Underground | 4 | 14,569 |
Oct-25-2020, 09:12 PM Last Post: Larz60+ |
|
convert old excel files(xls) to xlsm | zarize | 1 | 4,086 |
Jul-14-2020, 02:12 PM Last Post: DeaD_EyE |
|
How to convert python files from 32 bits tto 64 bits | sylas | 2 | 5,892 |
Oct-29-2017, 03:51 AM Last Post: Larz60+ |
|
Convert grib files to text files | tuxman | 6 | 11,117 |
Sep-15-2017, 03:26 PM Last Post: nilamo |
Users browsing this thread: 1 Guest(s)