긴 길이 중 특정 구간의 몇 초만 동영상으로 저장하고 싶을 때
다만 주의할 점은 대부분의 코드 모두 동영상을 전체 다운로드 해서 ffmpeg로 cut하는 방식이다.
즉 처음부터 특정 구간만 다운로드하는 방법은 없다.
1. postprocess-args를 사용해서 cut하기
How to download a portion of a video with youtube-dl OR something else?
I'd like to be able to download a portion of a video only. For example, being able to specify a start and/or end time for downloading. So, when a user inputs a start and end time of a video, it sho...
askubuntu.com
ffmpeg로 거치지 않아도 youtube dl 의 option을 사용해서 특정 구간을 자를 수 있다.
ffmpeg가 install 되어 있어야 한다.
명령어는 youtube-dl --postprocessor-args "-ss 0:0:15 -t 0:0:10" [video url]
처럼 사용하면 된다.
실제 예시는 다음과 같다
youtube-dl -f 'bestvideo[height<=1080]+bestaudio/best[height<=1080]' --postprocessor-args "-ss 00:00:11 -to 00:00:30" https://www.youtube.com/watch?v=LMDldyoU8hU
youtube-dl -f 'bestvideo[height<=1080]+bestaudio/best[height<=1080]' --postprocessor-args "-ss 00:00:11 -to 00:00:30" https://www.youtube.com/watch?v=LMDldyoU8hU
위 명령어를 치면 잘 저장되는 것을 알 수 있다.
11~29초 까지 18초 길이가 저장되는 것을 알 수 있다.
2. 자동화
ffmpeg로 자동화하는 코드를 참고했다.
How to download a portion of a video with youtube-dl OR something else?
I'd like to be able to download a portion of a video only. For example, being able to specify a start and/or end time for downloading. So, when a user inputs a start and end time of a video, it sho...
askubuntu.com
import subprocess
import csv
# Open the CSV file
with open('YoutubeAnnotation.csv', 'r') as file:
# Create a CSV reader object
csv_reader = csv.reader(file)
# Iterate over each row in the CSV file
for i, row in enumerate(csv_reader):
if (i == 50):
break
# Extract the YouTube URL from the desired column
if row[3] is None or row[3] == '':
continue
try :
youtube_url = row[1] # Adjust the index according to your CSV file structure
name = youtube_url.split('=')[1]
st_time = '00:' + row[3].split('-')[0]
resolution = row[4]
except:
continue
# Process the YouTube URL (e.g., download, convert, etc.)
print(i, youtube_url, name, st_time) # Replace with your desired processing logic
# Download the video using youtube-dl
subprocess.call(["youtube-dl", "-f", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best",
"-o", f"w{name}_{resolution}.mp4", youtube_url])
# Convert the downloaded video to MP3 using ffmpeg
subprocess.call(["ffmpeg", "-ss", st_time, "-i", f"{name}_{resolution}.mp4", "-t", "00:00:13.00", "-c:v", "copy", "-c:a", "copy", f"CUT_{name}_{resolution}.mp4"])
# Optionally, you can delete the downloaded video file
해당 코드로 자동화를 했다.
노트북에서 깨지는 것처럼 보였는데
모바일로 전송하고 다른 미디어 플레이어로 틀었을 때 잘 돌아가는 것으로 보였다.
참고로
libx264를 했을 때 소리랑 싱크가 안 맞는 문제가 발생해서 다시 copy로 바꿨다.
https://superuser.com/questions/458761/accurately-cut-video-files-from-command-line
Accurately cut video files from command line
I'm having trouble finding a cli application that can take a video file (avi, mkv, and mp4 preferably) and cut out very short clips (2-6 seconds) with precision time accuracy. I've tried ffmpeg, me...
superuser.com
'머신러닝,딥러닝' 카테고리의 다른 글
한글 pandas dataframe 읽기 문제 / UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 0: invalid start byte (0) | 2023.09.22 |
---|---|
Tensorflow 2.4.0을 설치할 때 라이브러리 계속 충돌 (0) | 2023.06.19 |
Graph Execution Error 해결하기 : cuda에 맞는 tensorflow 버전 재설치하기 (0) | 2023.06.18 |
youtube-dl 사용하기 / Error: Unable to extract uploader id 에러 해결하기 (0) | 2023.06.01 |
youtube 8m tf record의 동영상 접근해보기 (0) | 2023.05.17 |
AttributeError: module 'tensorflow' has no attribute 'python_io' 에러 해결하기 (0) | 2023.05.17 |
[Error] onnx importerror (0) | 2023.05.08 |
pip install insightface 오류날 때 (onnx install 오류 해결) (0) | 2023.05.04 |