Python을 사용하여 폴더의 모든 파일에서 명령을 실행합니다.
문제
나는 다음과 같은 파이썬 스크립트를 만들려고 노력하고있다.
- "/input"폴더를 살펴보십시오.
- 해당 폴더의 각 비디오에 대해 Mencoder 명령을 실행하십시오 (내 휴대 전화에서 재생할 수있는 것으로 트랜스 코드).
- Mencoder가 실행을 마치면 원래 비디오를 삭제하십시오.
그것은 너무 어려워 보이지 않지만 나는 파이썬을 빨아 먹는다 :)
대본이 어떻게 생겼는지에 대한 아이디어가 있습니까?
보너스 질문 : 사용해야합니다
OS.System
또는
하위 프로세스
?
Subprocess.call은 다음과 같은 명령을 쓸 수 있기 때문에보다 읽기 쉬운 스크립트를 허용하는 것 같습니다.
cmdline = [ 'mencoder', sourcevideo, '-ovc', 'copy', '-oac', 'copy', '-ss', '00 : 02 : 54 ','-endpos ', '00 : 00 : 54 ','-o ', DestinationVideo
편집 : 알겠습니다. 작동합니다.
import os, subprocess
bitrate = '100'
mencoder = 'C:\\Program Files\\_utilitaires\\MPlayer-1.0rc2\\mencoder.exe'
inputdir = 'C:\\Documents and Settings\\Administrator\\Desktop\\input'
outputdir = 'C:\\Documents and Settings\\Administrator\\Desktop\\output'
for fichier in os.listdir(inputdir):
print 'fichier :' + fichier
sourceVideo = inputdir + '\\' + fichier
destinationVideo = outputdir + '\\' + fichier[:-4] + ".mp4"
commande = [mencoder,
'-of',
'lavf',
[...]
'-mc',
'0',
sourceVideo,
'-o',
destinationVideo]
subprocess.call(commande)
os.remove(sourceVideo)
raw_input('Press Enter to exit')
명확성을 위해 Mencoder 명령을 제거했습니다.
입력 해 주신 모든 분들께 감사드립니다.
해결책
모든 파일 이름 사용을 찾으려면 os.listdir()
.
그런 다음 파일 이름을 반복합니다. 그렇게 :
import os
for filename in os.listdir('dirname'):
callthecommandhere(blablahbla, filename, foo)
하위 프로세스를 선호하는 경우 하위 프로세스를 사용하십시오. :-)
다른 팁
사용 OS. 워크 디렉토리 컨텐츠에 대해 재귀 적으로 반복하기 위해 :
import os
root_dir = '.'
for directory, subdirectories, files in os.walk(root_dir):
for file in files:
print os.path.join(directory, file)
OS.System과 Subprocess.Call 사이의 실제 차이는 없습니다. 이상하게 명명 된 파일 (공백, 견적 표시 등을 포함한 파일 이름)을 처리하지 않으면. 이 경우 파일 이름에 대한 쉘 인용을 할 필요가 없기 때문에 Subprocess.Call은 확실히 더 좋습니다. OS.System은 유효한 쉘 명령을 수락 해야하는 경우, 예를 들어 구성 파일에서 사용자로부터 수신 한 경우 더 좋습니다.
파이썬은 이것에 대해 과잉 일 수 있습니다.
for file in *; do mencoder -some options $file; rm -f $file ; done
AVI
에게 MPG
(확장 프로그램 선택) :
files = os.listdir('/input')
for sourceVideo in files:
if sourceVideo[-4:] != ".avi"
continue
destinationVideo = sourceVideo[:-4] + ".mpg"
cmdLine = ['mencoder', sourceVideo, '-ovc', 'copy', '-oac', 'copy', '-ss',
'00:02:54', '-endpos', '00:00:54', '-o', destinationVideo]
output1 = Popen(cmdLine, stdout=PIPE).communicate()[0]
print output1
output2 = Popen(['del', sourceVideo], stdout=PIPE).communicate()[0]
print output2
또는 os.path.walk 함수를 사용할 수 있습니다.이 기능은 OS.Walk보다 더 많은 효과가 있습니다.
어리석은 예 :
def walk_func(blah_args, dirname,names):
print ' '.join(('In ',dirname,', called with ',blah_args))
for name in names:
print 'Walked on ' + name
if __name__ == '__main__':
import os.path
directory = './'
arguments = '[args go here]'
os.path.walk(directory,walk_func,arguments)
나는 웹에서 많은 도움을 받아 비슷한 문제가 있었고이 게시물은 작은 응용 프로그램을 만들었고, 목표는 VCD와 SVCD이며 소스를 삭제하지는 않지만 자신의 적응이 상당히 쉽게 적응할 것이라고 생각합니다. 필요합니다.
비디오 1 개를 변환하고 자르거나 폴더의 모든 비디오를 변환하여 이름 바꾸고 Subfolder /VCD에 넣을 수 있습니다.
나는 또한 작은 인터페이스를 추가합니다. 다른 사람이 유용하다는 것을 알기를 바랍니다!
코드와 파일을 여기에 넣었습니다. btw : http://tequilaphp.wordpress.com/2010/08/27/learning-python-making-a-svcd-gui/