트윗 캡쳐 후 비디오 만들기 3 - ffmpeg-python
안녕하세요. 카이랏입니다.
아직 트윗터 개발자 계정에 접근을 할 수 없는 상태입니다.
따라서 접근이 완료되는 대로 모듈들을 전반적으로 연결하여 테스트를 할 예정입니다.
1. 기능 별 파일 분류
그래서 먼저 작업할 내용들을 작업하였습니다. 파일은 아래와 같은 총 3개의 파일로 구성되어 있습니다.
imagecapture.py - 트윗터에서 해쉬태그를 검색하고 화면을 캡쳐한 후 이미지 파일로 저장한다.
imagehandler.py - 저장된 이미지 파일들을 갖고 동영상을 만든다.
twitcapturetovideo.py - 위의 두 가지 기능을 조합한다. 이 모듈이 UI 모듈과 연결된다.
여기서 이미지를 캡쳐하는 기능을 담당하는 imagecapture.py 파일은 트위터 문제가 해결되면 진행할 예정이고 먼저 imagehandler.py 파일을 구현하였습니다.
2. ffmpeg-python 모듈 설치
기능은 간단합니다. ffmpeg 모듈을 이용한 ffmpeg-python 을 설치합니다. 터미널에 아래와 같이 입력하여 설치를 진행합니다.
pip3 install ffmpeg-python
(만약 pip3 명령이 동작하지 않는다면 pip를 이용해서 설치하면 됩니다.)
3. ffmpeg-python 모듈을 이용한 구현
이후 응용은 아래와 같은 코드를 이용하여 진행합니다.
현재 필요한 것은 이미지들을 영상으로 만들어 추출하는 것입니다.
이것을 구현하기 위해서 make_Video_from_images 라는 함수를 만들었습니다.
import os
import ffmpeg
def make_Video_from_images(images_path, frame_rate=1, output_filename="output") :
try:
workdir = os.path.dirname(os.path.realpath(__file__))
#### output path
if os.path.exists(workdir + "/output") == False:
os.mkdir(workdir + "/output")
output_path = workdir + "/output" + f"/{output_filename}" + ".mp4"
stream = ffmpeg.input(images_path, pattern_type='glob', framerate=frame_rate)
stream = ffmpeg.output(stream, output_path)
ffmpeg.run(stream)
except Exception as e:
print(e)
return
4. 간단한 오류 - 윈도우와 맥의 차이
여기서 하나 실수한 내용은 "/"와 "\"를 헷갈려서 작업하여 계속 "없는 파일 또는 디렉토리" 라는 메시지가 계속 나오는 것이었습니다.
이것을 찾느라 한참을 헤매였습니다. 이런 실수는 다시 하고 싶지 않습니다.
우리가 예제를 찾다가 때로는 맥에서 개발할 때 윈도우 예제를 그대로 가져와서 사용하면 서로 경로를 표시하는 방식이 다르기 때문에 이런 오류가 나오는 것 같습니다.
테스트 결과 안정적으로 파일들을 읽고 동영상을 만들었습니다.
이후로는 UI 작업과 더불어 트위터 모듈을 완료하고 서로 이어붙이는 작업을 할 예정입니다.
Hello. This is Kairat.
You do not have access to your Twitter developer account yet.
Therefore, as soon as access is complete, we will test by connecting the modules as a whole.
1. File classification by function
imagecapture.py - Search for hashtags on Twitter, capture the screen, and save it as an image file.
imagehandler.py - Creates a video with saved image files.
twitcapturetovideo.py - Combines the above two functions. This module connects to the UI module.
The imagecapture.py file, which is responsible for capturing images here, will be implemented when the Twitter problem is resolved, and the imagehandler.py file has been implemented first.
2. Install the ffmpeg-python module
The function is simple. Install ffmpeg-python using the ffmpeg module. Proceed with the installation by entering the following in the terminal.
pip3 install ffmpeg-python
(If the pip3 command doesn't work, you can use pip to install it.)
3. Implementation using the ffmpeg-python module
Then the application proceeds using the code below.
What we need now is to convert images to video and extract them.
I created a function called make_Video_from_images to implement this.
import os
import ffmpeg
def make_Video_from_images(images_path, frame_rate=1, output_filename="output") :
try:
workdir = os.path.dirname(os.path.realpath(__file__))
#### output path
if os.path.exists(workdir + "/output") == False:
os.mkdir(workdir + "/output")
output_path = workdir + "/output" + f"/{output_filename}" + ".mp4"
stream = ffmpeg.input(images_path, pattern_type='glob', framerate=frame_rate)
stream = ffmpeg.output(stream, output_path)
ffmpeg.run(stream)
except Exception as e:
print(e)
return
4. A simple error - the difference between Windows and Mac
One mistake here is to confuse "/" and "\" and continue to say "non-existing file or directory" The message kept coming.
I've been looking for this for a while. I don't want to make this mistake again.
Sometimes when we are looking for examples, it seems that this kind of error appears because the way of displaying the path is different when we import and use the Windows example as it is when developing on Mac.
As a result of the test, the files were read and the video was created stably.
From now on, we will complete the Twitter module along with the UI work and work on connecting them together.