Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert .MTS files
#11
(Apr-15-2017, 02:38 PM)pberrett Wrote: I've amended my code per your instructions. It is now
Not really Wink
Copy all of my code,see that i have made changes here
f'{path_out+os.path.splitext(file.name)[0]}.mp4'])
And path_out need to end with a backslash(/).
path_out = 'C:/upload/completed/'
Reply
#12
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

Output:
>>> = RESTART: C:/Users/Peter/AppData/Local/Programs/Python/Python36-32/test2.py = >>>
Reply
#13
(Apr-15-2017, 02:50 PM)pberrett Wrote: I've now added the slashes
Only 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'])
Reply
#14
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}'])
Reply
#15
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}')
Reply
#16
(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.
Reply
#17
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python convert multiple files to multiple lists MCL169 6 1,620 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Separate text files and convert into csv marfer 6 2,933 Dec-10-2021, 12:09 PM
Last Post: marfer
  Convert a PDF files to HTML files Underground 4 12,218 Oct-25-2020, 09:12 PM
Last Post: Larz60+
  convert old excel files(xls) to xlsm zarize 1 3,410 Jul-14-2020, 02:12 PM
Last Post: DeaD_EyE
  How to convert python files from 32 bits tto 64 bits sylas 2 5,117 Oct-29-2017, 03:51 AM
Last Post: Larz60+
  Convert grib files to text files tuxman 6 9,353 Sep-15-2017, 03:26 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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