머신러닝,딥러닝

youtube-dl 사용하기 / 구간 별로 cut할 때 여러 명령어

mcdn 2023. 6. 1. 16:43
반응형

 

긴 길이 중 특정 구간의 몇 초만 동영상으로 저장하고 싶을 때 

 

다만 주의할 점은 대부분의 코드 모두 동영상을 전체 다운로드 해서 ffmpeg로 cut하는 방식이다. 

즉 처음부터 특정 구간만 다운로드하는 방법은 없다. 

 

1. postprocess-args를 사용해서 cut하기 

 

https://askubuntu.com/questions/970629/how-to-download-a-portion-of-a-video-with-youtube-dl-or-something-else

 

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로 자동화하는 코드를 참고했다. 

https://askubuntu.com/questions/970629/how-to-download-a-portion-of-a-video-with-youtube-dl-or-something-else

 

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

 

 

 

 

 

반응형